-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopologicalSort.py
54 lines (46 loc) · 1.49 KB
/
topologicalSort.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
# ==========================================================
# topologicalSort
# ==========================================================
from collections import defaultdict
class Graph:
def __init__(self,vertices):
self.graph = defaultdict(list)
self.V = vertices
def add_edge(self,u,v):
self.graph[u].append(v)
def set_keys_station(self):
keyStation = {}
key = list(self.graph.keys())
if len(key) < self.V:
for i in key:
for j in self.graph[i]:
if j not in key:
key.append(j)
for ele in key:
keyStation[ele] = False
return keyStation
def topological_sort(self):
queue = []
station = self.set_keys_station()
for i in range(self.V):
for elem in station:
if not station[elem]:
self.topological_sort_util(elem, queue, station)
return queue
def topological_sort_util(self, elem, queue, station):
station[elem] = True
for i in station:
if elem in self.graph[i] and not station[i]:
station[elem] = False
if station[elem]:
queue.append(elem)
if __name__ == '__main__':
g= Graph(6)
g.add_edge(5, 2);
g.add_edge(5, 0);
g.add_edge(4, 0);
g.add_edge(4, 1);
g.add_edge(2, 3);
g.add_edge(3, 1);
print ("result:")
print(g.topological_sort())