forked from kumailn/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopological_Sort.py
More file actions
28 lines (23 loc) · 848 Bytes
/
Copy pathTopological_Sort.py
File metadata and controls
28 lines (23 loc) · 848 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
from typing import List
def topSort(numEdges: int, edges: List[int]) -> List[int]:
graph = {}
def depthFirst(current, visited, visitedNodes, graph):
visited.add(current)
for edge in graph[current]:
if not (edge in visited):
depthFirst(edge, visited, visitedNodes, graph)
visitedNodes.append(current)
# Populate the graph
for i in range(numEdges): graph[i] = []
for x, y in edges: graph[y] = [x]
visited = set(); result = []
for node in graph:
if not (node in visited):
visitedNodes = []
depthFirst(node, visited, visitedNodes, graph)
for node2 in visitedNodes: result.insert(0, node2)
return result
def main():
graph = [[0, 5], [5, 0], [0, 4], [1, 4], [2, 5], [3, 2], [1, 3]]
print(topSort(6, graph))
main()