-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtopological_sort.py
More file actions
43 lines (36 loc) · 1.17 KB
/
topological_sort.py
File metadata and controls
43 lines (36 loc) · 1.17 KB
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
"""
Compute a topological ordering of a given directed acyclic graph (DAG) with n vertices and m edges.
"""
def topological_sort(g):
"""
Returns tuple for a directed graph where:
- First element is a boolean that is set to True if graph contains a cycle
- Second element is an array of vertices in topological order if graph has no cycles,
or None otherwise
"""
visited = set()
removed = set()
result = []
def __remove_if_sink(x):
edges = g.get_edges(x)
if not edges or set(map(lambda e: e.end, edges)).issubset(removed):
result.append(x)
removed.add(x)
return True
return False
def __topological_sort(x):
if x in visited:
return True
visited.add(x)
for edge in g.get_edges(x):
if edge.end in removed:
continue
if __topological_sort(edge.end):
return True
__remove_if_sink(x)
return False
for v in g.get_vertices():
if v not in visited and v not in removed:
if __topological_sort(v):
return (True, None)
return (False, result)