-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsp.py
176 lines (165 loc) · 6.27 KB
/
sp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from tf import *
from itertools import chain
###concatenating cycles to paths
def sP(r,gr):
flag=0
synchronising=[]
synchronisingP=[]
for key in gr:
if key==r: #synchronising word for r
c=gr[key][key] #list of cycles of key
for i in range(0,len(c)):
cycle=c[i] #taking cycles of key one by one
for j in range(1,13):
if j!=key: #since we're only considering paths here
p=gr[j][key] #list of all paths from j to key
for m in range(0,len(p)):
path=p[m] #taking paths one by one
pc=path+cycle
## print(m,"th path for",i,"th cycle",pc)
flag=syncP(pc)
if flag==1:
seq=color(pc)
## if seq not in synchronising: #prevent repetition of synchronising paths
synchronising.append(seq)
synchronisingP.append(pc)
## synchronising.append(seq)
## print(pc,"Synchronising path found")
break
else: #path not synchronising
#adding cycle twice
pc=path+cycle+cycle
## print(pc)
flag=syncP(pc)
if flag==1:
seq=color(pc)
## if seq not in synchronising:
synchronising.append(seq)
synchronisingP.append(pc)
## synchronising.append(seq)
## print(pc,"Synchronising path found")
break
print("Synchronising paths for",r)
for z in range(0,len(synchronising)):
print(synchronising[z],"\n")
print("shortest synchronising path",min(synchronising,key=len))
## ch1=['B','B','R','B','B','R','B','B','R','B']
## ch2=['B','R','R','B','R','R','B','R','R']
## NOch3=['B','B','R','B','B','R','R','B','R','R','R']
## ch4=['B','B','R','B','B','R','B','B','R']
## NOch5=['R','B','B','R','B','B','R','B','B']
## NOch6=['B','R','B','R','B','R','R','B','R']
## NOch7=['B','B','B','R','B','B','R','B','B','R','R']
## Noch8=['R','R','B','R','R','B','R','R','B']
## if ch in synchronising:
## print("YEAH!")
#checking whether the concatenated path+cycle is synchronising
def syncP(pc):
fv=pc[len(pc)-1]
synchronisingP=[]
setseq=[]
adj=[
[0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,3,0,2,0,0,0,1,0,0,0,0],
[0,1,0,2,0,3,0,0,0,0,0,0,0],
[0,0,3,0,1,0,2,0,0,0,0,0,0],
[0,0,0,1,0,0,0,3,0,0,0,0,2],
[0,0,1,0,0,0,1,0,0,3,0,0,0],
[0,0,0,1,0,3,0,2,0,0,0,0,0],
[0,0,0,0,2,0,1,0,0,0,0,0,3],
[0,1,0,0,0,0,0,0,0,2,3,0,0],
[0,0,0,0,0,1,0,0,3,0,0,2,0],
[0,1,0,0,0,0,0,0,2,0,0,3,0],
[0,0,0,0,0,0,0,0,0,1,3,0,2],
[0,0,0,0,0,1,0,0,0,0,3,2,0],
]
seq=color(pc)
flag=1
#checking whether all vertices (except r) following the same color sequence reach r
for i in range(1,13): #origin vertex
if i!=fv:
v=i
counter=0
pathF=[]
pathF.append(v)
while counter<len(seq):
for m in range(1,13):#column number
if seq[counter]=='R':
if adj[v][m]==1:
pathF.append(m)
v=m
counter+=1
break
elif seq[counter]=='B':
if adj[v][m]==2:
pathF.append(m)
v=m
counter+=1
break
elif seq[counter]=='G':
if adj[v][m]==3:
pathF.append(m)
v=m
counter+=1
break
if pathF[len(pathF)-1]!=fv:
flag=0 #not asynchronising path
## print(flag,"=>",pathF)
break
## else:
## print(flag,"=>",pathF)
synchronisingP.append(pathF)
## if flag==1:
## print(synchronisingP)
## print(seq)
return flag
#assign colors to the path from adj
def color(pc):
adj=[
[0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,3,0,2,0,0,0,1,0,0,0,0],
[0,1,0,2,0,3,0,0,0,0,0,0,0],
[0,0,3,0,1,0,2,0,0,0,0,0,0],
[0,0,0,1,0,0,0,3,0,0,0,0,2],
[0,0,1,0,0,0,1,0,0,3,0,0,0],
[0,0,0,1,0,3,0,2,0,0,0,0,0],
[0,0,0,0,2,0,1,0,0,0,0,0,3],
[0,1,0,0,0,0,0,0,0,2,3,0,0],
[0,0,0,0,0,1,0,0,3,0,0,2,0],
[0,1,0,0,0,0,0,0,2,0,0,3,0],
[0,0,0,0,0,0,0,0,0,1,3,0,2],
[0,0,0,0,0,1,0,0,0,0,3,2,0],
]
seq=[]
for m in range(0,len(pc)-1):
v1=pc[m]
v2=pc[m+1]
if adj[int(v1)][int(v2)]==1:
seq+='R'
elif adj[int(v1)][int(v2)]==2:
seq+='B'
elif adj[int(v1)][int(v2)]==3:
seq+='G'
return seq
def main():
print("Take input of AGW graph as adjacency matrix")
## print("Tree Formation")
gr=graph()
for i in range(1,13):
rootName=i
print("Printing root-leaf paths for tree",rootName)
rootNode=treeFormation(rootName)
r=rootNode
print("Tree Paths")
l=rootNode.treePaths(rootNode)
r.rtlpaths=l
print(r.rtlpaths)
## r.counter(r,r)
## cycles(r,gr)
## paths(r,gr)
## pprint.pprint(gr)
## for j in range(1,9):
## sP(j,gr)
## print(color(['1','2','2','3','1','2']))
if __name__=="__main__":
main()