-
Notifications
You must be signed in to change notification settings - Fork 77
/
depth_first_paths.py
84 lines (72 loc) · 2 KB
/
depth_first_paths.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""
Execution: python depth_first_paths.py G s
Data files: https://algs4.cs.princeton.edu/41graph/tinyCG.txt
https://algs4.cs.princeton.edu/41graph/tinyG.txt
https://algs4.cs.princeton.edu/41graph/mediumG.txt
https://algs4.cs.princeton.edu/41graph/largeG.txt
Run depth first search on an undirected graph.
Runs in O(E + V) time.
% python grapy.py tinyCG.txt
6 8
0: 2 1 5
1: 0 2
2: 0 1 3 4
3: 5 4 2
4: 3 2
5: 3 0
% python depth_first_paths.py tinyCG.txt 0
0 to 0: 0
0 to 1: 0-2-1
0 to 2: 0-2
0 to 3: 0-2-3
0 to 4: 0-2-3-4
0 to 5: 0-2-3-5
"""
from algs4.stack import Stack
from algs4.graph import Graph
class DepthFirstPaths:
def __init__(self, G, s):
self.marked = [False for _ in range(G.V)]
self.edge_to = [0 for _ in range(G.V)]
self.s = s
self.dfs(G, s)
def dfs(self, G, v):
self.marked[v] = True
for w in G.adj[v]:
if not self.marked[w]:
self.edge_to[w] = v
self.dfs(G, w)
def has_path_to(self, v):
return self.marked[v]
def path_to(self, v):
if not self.has_path_to(v):
return
path = Stack()
x = v
while x != self.s:
path.push(x)
x = self.edge_to[x]
path.push(s)
return path
if __name__ == '__main__':
import sys
f = open(sys.argv[1])
s = int(sys.argv[2])
V = int(f.readline())
E = int(f.readline())
g = Graph(V)
for i in range(E):
v, w = f.readline().split()
g.add_edge(v, w)
dfs = DepthFirstPaths(g, s)
for v in range(g.V):
if dfs.has_path_to(v):
print("%d to %d: " % (s, v), end='')
for x in dfs.path_to(v):
if x == s:
print(x, end='')
else:
print('-%s' % x, end='')
print()
else:
print("%d and %d: not connected" % (s, v))