forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topological Sort.py
32 lines (27 loc) · 1013 Bytes
/
Topological Sort.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
class Graph:
def __init__(self,nodes):
self.graph=[]
for i in range(0,nodes):
self.graph.append([])
def addedge(self,initial,final):
(self.graph[initial]).append(final)
def TopoSort(self):
# To create an array which keeps track of every node's degree
degree=[]
for i in range(0,len(self.graph)):
degree.append(0)
for j in self.graph:
for k in j:
degree[k] += 1
# To remove one element one by one to get a topological order
final=[]
while len(final)<len(self.graph):
for l in range(0,len(degree)):
if degree[l] == 0 and l not in final:
final.append(l)
for m in self.graph[l]:
# To incorporate the change in degree array
degree[m] -= 1
# To reduce number of unnecessary loops
break
print(final)