-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopological_sort.py
More file actions
40 lines (29 loc) · 838 Bytes
/
Copy pathtopological_sort.py
File metadata and controls
40 lines (29 loc) · 838 Bytes
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
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 topological_sort_util(v, visited, graph, stack):
visited[v] = True
for i in g.graph[v]:
if visited[i] is False:
topological_sort_util(i, visited, graph, stack)
stack.insert(0, v)
def topological_sort(graph):
visited = [False]*(graph.V)
stk = []
for i in range(graph.V):
if visited[i] is False:
topological_sort_util(i, visited, graph, stk)
print(stk)
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)
topological_sort(g)