Algorithm 5.1 Computing a 0/1 Permanent via Recursion

import numpy as np
#import random
#import math
#import time
#import matplotlib.pyplot as plt

# mat is an NxN 0/1 matrix
# used is an N-dim. boolean array
def permrecurse(row,used,mat):
  N = len(mat[0])
  v = 0
  if( row == N):
    #print("end this permutation, adding 1")
    return 1
  else:
    for j in range(N):
      if( not used[j] and  mat[row][j] != 0):
        #print("non-zero for row= ",row," j= ",j)
        used[j]=True
        #print("to next row: ",row+1)
        v += permrecurse(row+1,used,mat);
        #print("back to row: ",row," v= ",v)
        used[j]=False
  return v

"""
mat = [[1,1,0],[1,1,0],[0,0,1]]
N = len(mat[0])
used = [False]*N
v = permrecurse(0,used,mat)
print(v)
"""

mat = [
   [0,0,0,0,0,0,1,0,0,0,1,0,0,1],
   [0,0,0,0,0,0,1,0,1,1,0,0,0,1],
   [0,0,0,0,0,0,0,0,1,0,1,1,0,0],
   [0,0,0,0,0,0,0,0,1,1,0,1,0,0],
   [1,0,0,0,0,0,0,1,0,0,0,0,1,0],
   [0,1,0,0,0,0,0,0,0,0,0,0,0,0],
   [0,0,1,1,0,0,0,0,0,0,0,0,0,0],
   [0,0,1,0,1,1,0,0,0,0,0,0,0,0],
   [0,0,1,1,1,0,0,0,0,0,0,0,0,0],
   [0,0,0,0,1,1,0,0,0,1,0,0,0,0],
   [0,0,0,0,0,0,1,0,0,0,1,1,0,1],
   [0,0,0,0,0,0,0,1,0,1,0,0,1,0],
   [0,0,0,0,0,0,1,0,0,1,0,1,0,0],
   [1,0,0,0,0,0,0,1,0,0,0,0,0,0]
];
N = len(mat[0])
used = [False]*N
v = permrecurse(0,used,mat)
print(v)

"""
N = 11
mat = np.zeros([N, N], dtype=float)
for i in range(N):
  for j in range(N):
    if( i<4 and j<4 ):
      mat[i][j] =1;
    elif( 4<=i and i<8 and 4<=j and j<8 ):
      mat[i][j] = 1;
    elif( 8<=i and i<10 and 8<=j and j<10 ):
      mat[i][j] = 1;
    elif( i==j and 10<=i):
      mat[i][j] = 1;
    else:
      mat[i][j] = 0;

for i in range(N):
  for j in range(N):
    print("",mat[i][j],end="");
  print("");

N = len(mat[0])
used = [False]*N
v = permrecurse(0)
print(v)
"""