-
Notifications
You must be signed in to change notification settings - Fork 586
/
P68_TopologicalSort.py
66 lines (54 loc) · 2.07 KB
/
P68_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
55
56
57
58
59
60
61
62
63
64
65
66
# Author: OMKAR PATHAK
# Time Complexity: O(|V| + |E|)
# One important point to remember is that topological sort can be applied only to acyclic graph.
class Graph():
def __init__(self, count):
self.vertex = {}
self.count = count # vertex count
# for printing the Graph vertexes
def printGraph(self):
for i in self.vertex.keys():
print(i,' -> ', ' -> '.join([str(j) for j in self.vertex[i]]))
# for adding the edge beween two vertexes
def addEdge(self, fromVertex, toVertex):
# check if vertex is already present,
if fromVertex in self.vertex.keys():
self.vertex[fromVertex].append(toVertex)
else:
# else make a new vertex
self.vertex[fromVertex] = [toVertex]
self.vertex[toVertex] = []
def topologicalSort(self):
visited = [False] * self.count # Marking all vertices as not visited
stack = [] # Stack for storing the vertex
for vertex in range(self.count):
# Call the recursive function only if not visited
if visited[vertex] == False:
self.topologicalSortRec(vertex, visited, stack)
print(' '.join([str(i) for i in stack]))
# print(stack)
# Recursive function for topological Sort
def topologicalSortRec(self, vertex, visited, stack):
# Mark the current node in visited
visited[vertex] = True
# mark all adjacent nodes of the current node
try:
for adjacentNode in self.vertex[vertex]:
if visited[adjacentNode] == False:
self.topologicalSortRec(adjacentNode, visited, stack)
except KeyError:
return
# Push current vertex to stack which stores the result
stack.insert(0,vertex)
if __name__ == '__main__':
g= Graph(6)
g.addEdge(5, 2)
g.addEdge(5, 0)
g.addEdge(4, 0)
g.addEdge(4, 1)
g.addEdge(2, 3)
g.addEdge(3, 1)
# g.printGraph()
g.topologicalSort()
# OUTPUT:
# 5 4 2 3 1 0