-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf.py
236 lines (194 loc) · 7.39 KB
/
tf.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
from bt import *
from collections import defaultdict
import pprint
def treeFormation(rootName):
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],
]
#Formation of trees
currNode=treeNode(rootName,None)
r=currNode
r.tree(currNode)
r.rv=rootName
flag=[0]*13
counter=0
while(sum(flag)!=36):
counter+=1
cn=currNode.get()
if flag[cn]<3:
for j in range(1,13):
if adj[cn][j]==flag[cn]+1:
flag[cn]+=1
if flag[j]==0:
child=treeNode(j,currNode)
currNode.addnode(child)
temp=currNode.getchildren()
print("children",temp,"of",cn,currNode)
currNode=temp[len(temp)-1]
else :
child=treeNode(j,currNode)
currNode.addnode(child)
break
else:
currNode=currNode.getparent() #returns the parent object,
return r #returns the root
def cycles(r,gr):
count=[0]*13
path=[]
index=[]
stack=[]
i=r.cnode
#determine the first straight path
currNode=r #receive root from main
path.append(currNode.cnode)
index.append(currNode)
while(len(set(path))==len(path)): #there is no repetition in the path isRep(path)):
stack=push(stack,1)
ch=[]
ch=currNode.getchildren()
currNode=ch[0] #make currNode, first child to the left
path.append(currNode.cnode) #adding on to the path
index.append(currNode)
if currNode.isleaf():
currNode=skipNode(r,currNode)
if path[0]==path[len(path)-1]:
count[i]=count[i]+1
j=r.cnode
cp(r,gr,j,path)
#determining remaining paths
flag1=0
while True:
while stack[len(stack)-1]==3:
stack.pop()
path.pop()
index.pop()
if len(stack)==0:
flag1=1
break
if flag1==1:
break
val=stack.pop()
stack=push(stack,val+1)
#if the current node is a leaf node =>jump to the node with same value parent node
ob=index[len(index)-2]
if ob.isleaf():
ob=skipNode(r,ob)
temp=ob.getchildren()
path[len(path)-1]=temp[val].get()
index[len(index)-1]=temp[val]
currNode=temp[val]
while(len(set(path))==len(path)): #there is no repetition in the path isRep(path)):
if currNode.isleaf():
currNode=skipNode(r,currNode)
stack=push(stack,1)
l=currNode.getchildren()
currNode=l[0]
path.append(currNode.cnode) #adding on to the path
index.append(currNode)
## #if obtained path is required solution B
if path[0]==path[len(path)-1]:
count[i]+=1
j=r.cnode
cp(r,gr,j,path)
#Determine no loop paths from node 1->2,3,...n & 2->1,3,4,..n & 3->1,2,4,.. n-1
## for i in range(1,2):
def paths(r,gr):
for j in range(1,13):
if r.cnode!=j:
stack=[] #python equivalent here
path=[]
index=[]
currNode=r
#determine the first straight path C
path.append(currNode.cnode)
index.append(currNode)
while(len(set(path))==len(path) and path[len(path)-1]!=j): #there is no repetition in the path isRep(path)):
stack=push(stack,1)
ch=currNode.getchildren()
currNode=ch[0]
path.append(currNode.cnode) #adding on to the path
index.append(currNode)
if currNode.isleaf():
currNode=skipNode(r,currNode)
if path[len(path)-1]==j:
cp(r,gr,j,path)
#Determining remaining paths
while True:
flag1=0
while stack[len(stack)-1]==3:
stack.pop()
path.pop()
index.pop()
if len(stack)==0:
flag1=1
break
if flag1==1:
break
val=stack.pop()
stack=push(stack,val+1)
#if the current node is a leaf node =>jump to the node with same value parent node
ob=index[len(index)-2]
if ob.isleaf():
ob=skipNode(r,ob)
temp=ob.getchildren()
path[len(path)-1]=temp[val].cnode
index[len(index)-1]=temp[val]
currNode=temp[val]
while len(set(path))==len(path) and path[len(path)-1]!=j: #there is no repetition in the path isRep(path) && path(length(path))!=j):
if currNode.isleaf():
currNode=skipNode(r,currNode)
stack=push(stack,1)
ch=[]
ch=currNode.getchildren()
currNode=ch[0]
path.append(currNode.cnode) #adding on to the path
index.append(currNode)
if path[len(path)-1]==j:
cp(r,gr,j,path)
def graph():
gr=defaultdict(list)
for key in range(1,13):
gr[key]={}
for k in range(1,13):
gr[key][k]=[]
return gr
def cp(r,gr,j,path):
flag=0
for key in gr:
if key==r.cnode:
for k in gr[key]:
if k==j:
gr[key][k].append(path.copy())
flag=1
break
if flag==1:
break
def skipNode(r,Node):#function to skip to same value parent node
flag=0
for key in r.d:
if key==Node.cnode:
list1=r.d[key]
l=len(list1)
for i in (0,l):
if not list1[i].isleaf():
Node=list1[i]
flag=1
break
if flag==1:
break
return Node
def push(stack,k):
stack.append(k)
return stack