-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtop_sort.py
43 lines (33 loc) · 1.11 KB
/
top_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
31
32
33
34
35
36
37
38
39
40
41
42
43
print('top_sort')
def top_sort(graph):
'''
Topological sort of a directed acyclic graph (DAG)
Topological sort is useful when you need to perform a series of tasks
where each task has some dependencies on other tasks.
Time complexity: :math:`O(V + E)`, where V is the number of vertices and E is the number of edges
Space complexity: :math:`O(V)`
:param graph: dict
:return: list
'''
seen = set()
current_path = set()
order = []
def dfs(vert):
nonlocal order
seen.add(vert)
current_path.add(vert)
for adj in graph[vert]:
if adj not in seen:
dfs(adj)
elif adj in current_path: # check if graph has a cycle
print('Cycle detected')
order = ['Cycle detected']
return # if graph has a cycle, then there is no topological sort
current_path.discard(vert)
order.append(vert)
for vert in graph:
if vert in seen: continue
dfs(vert)
return order[::-1]
graph = {0: [1, 2], 1: [3], 2: [3], 3: [4], 4: [0]}
print(top_sort(graph))