-
Notifications
You must be signed in to change notification settings - Fork 134
/
TopologicalSort.py
54 lines (38 loc) · 1.22 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
# Program to perform topological sort in a graph
from collections import defaultdict
class Graph:
def __init__(self, directed=False):
self.graph = defaultdict(list)
self.directed = directed
def addEdge(self, frm, to):
self.graph[frm].append(to)
if self.directed is False:
self.graph[to].append(frm)
else:
self.graph[to] = self.graph[to]
def topoSortUtil(self, s, visited, sortList):
visited[s] = True
for i in self.graph[s]:
if not visited[i]:
self.topoSortUtil(i, visited, sortList)
sortList.insert(0, s)
def topologicalSort(self):
visited = {i: False for i in self.graph}
sortList = []
# traverse for all the vertices in other components of graph
for v in self.graph:
if not visited[v]:
self.topoSortUtil(v, visited, sortList)
print(sortList)
if __name__ == '__main__':
# make an directed graph
g = Graph(directed=True)
g.addEdge(5, 2)
g.addEdge(5, 0)
g.addEdge(4, 0)
g.addEdge(4, 1)
g.addEdge(2, 3)
g.addEdge(3, 1)
# call topologicalSort()
print("Topological Sort:")
g.topologicalSort()