Adjacency_Matrix_Swaps

296 days ago by Ben.LeVeque

x=var('x') K.<a> = NumberField(x^2-x-1);K 
       
Number Field in a with defining polynomial x^2 - x - 1
Number Field in a with defining polynomial x^2 - x - 1
def rc_permutations(L_old,L_new): P_list=[] L=[] for i in range(len(L_old)): if L_new[i] != L_old[i]: L.append((L_new[i],L_old[i])) H = [] done = [] #done is a list of indices that have been used already j=0 while j<len(L): if j in done: j+=1 else: t = L[j] if H==[]: H=[t[0],t[1]] done.append(j) continue if t[0]==H[-1]: done.append(j) if t[1]==H[0]: P_list.append(H) j=0 H=[] else: H.append(t[1]) j=0 j+=1 return P_list def exec_rcp(L_old,L_new,adj): P_list = rc_permutations(L_old,L_new) for H in P_list: Hr = list(H[1:]) Hr.reverse() for i in Hr: adj.swap_rows(H[0],i) adj.swap_columns(H[0],i) return adj 
       
L_old=[0,1,2,3,4] #Test case L_new=[0,4,1,3,2] M = Matrix(ZZ,[[0,2,5,0,0],[2,0,3,11,0],[5,3,0,0,7],[0,11,0,0,0],[0,0,7,0,0]]);M #old adj matrix 
       
[ 0  2  5  0  0]
[ 2  0  3 11  0]
[ 5  3  0  0  7]
[ 0 11  0  0  0]
[ 0  0  7  0  0]
[ 0  2  5  0  0]
[ 2  0  3 11  0]
[ 5  3  0  0  7]
[ 0 11  0  0  0]
[ 0  0  7  0  0]
exec_rcp(L_old,L_new,M) #new adj matrix 
       
[ 0  5  0  0  2]
[ 5  0  7  0  3]
[ 0  7  0  0  0]
[ 0  0  0  0 11]
[ 2  3  0 11  0]
[ 0  5  0  0  2]
[ 5  0  7  0  3]
[ 0  7  0  0  0]
[ 0  0  0  0 11]
[ 2  3  0 11  0]