-
Notifications
You must be signed in to change notification settings - Fork 119
/
dfs.py
57 lines (44 loc) · 1.25 KB
/
dfs.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
from graph import *
class DFSResults:
def __init__(self):
self.parent = dict()
self.time = dict()
self.vertices = list()
self.t = -1
def dfs(g):
results = DFSResults()
for vertex in g.itervertices():
if vertex not in results.parent:
dfs_visit(g, vertex, results)
return results
def dfs_visit(g, v, results, parent = None):
results.vertices.append(v)
results.parent[v] = parent
for n in g.neighbors(v):
if n not in results.parent:
results.parent[n] = v
dfs_visit(g, n, results, v)
results.t += 1
results.time[v] = results.t
#results.t += 1
#results.time[v] = results.t
def topological_sort(g):
r = dfs(g)
top = [None for i in r.vertices]
for vertex in r.time:
top[r.time[vertex]] = vertex
return top
if __name__ == '__main__':
g = ALDirectedGraph()
g.add_edge('u', 'x')
g.add_edge('u', 'v')
g.add_edge('v', 'y')
g.add_edge('w', 'y')
g.add_edge('w', 'z')
g.add_edge('x', 'v')
g.add_edge('y', 'x')
g.add_edge('z', 'z')
r = dfs(g)
print 'vertices: ', r.vertices
print 'time: ', r.time
print 'topological sort: ', topological_sort(g)