-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfs_topsort.py
29 lines (27 loc) · 961 Bytes
/
dfs_topsort.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
#!/bin/env python
def dfs_topsort(G):
S, res = set(), [] # History & result
def recurse(u): # Traversal subroutine
if u in S: return # Already visited, Ignore
S.add(u) # Visit node
for v in G[u]: # Explore its neighbours
recurse(v) # Traverse the neighbour as parent
res.append(u) # Finished with u: Append to result
for u in G:
print ("u in ts {}".format(u))
recurse(u) # Cover the entire graph
res.reverse() # dfs ends up backwards for desc finish times so
return res
# Main Program
#a, b, c, d, e, f, g, h = range(8)
#G = [
# {b, c, d, e, f}, #a
# {c, e}, #b
# {d}, #c
# {e}, #d
# {f}, #e
# {c, g, h}, #f
# {f, h}, #g
# {f, g} #h
# ]
#print (dfs_topsort(G))