-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoptry.py
71 lines (60 loc) · 1.51 KB
/
toptry.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
class Graph:
def __init__(self, g):
self.g = g
self.n = len(g)
self.list = []
def adj_list(self):
g = self.g
al = {}
for i in range(self.n):
al[i] = []
for i in range(self.n):
for j in range(self.n):
if g[i][j] != 0:
al[i].append(j)
return al
def print_graph(self):
g = self.g
n = self.n
for i in range(n):
print(" ", i, end="")
print()
for i in range(n):
print(i, g[i])
def top_sort(self, al=None):
g = self.g
n = self.n
x = 0
if al is None:
al = self.adj_list()
for i in al:
if len(al[i]) == 0 and i not in self.list:
self.list.append(i)
for i in al.values():
for j in self.list:
if j in i:
i.remove(j)
if len(self.list) != n:
self.top_sort(al)
else:
print("TOP SORT : ", self.list[::-1])
def main():
gt = [[0, 1, 0, 1, 0, 0],
[0, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0],
]
g = [[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0],
[0, 1, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0],
[1, 0, 1, 0, 0, 0],
]
m = Graph(gt)
m.top_sort()
n = Graph(g)
n.top_sort()
main()