-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathnonrecursive_depth_first_search.py
62 lines (48 loc) · 1.5 KB
/
nonrecursive_depth_first_search.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
"""
Execution: python nonrecursive_depth_first_search.py filename.txt s
Data files: https: // algs4.cs.princeton.edu / 41graph / tinyG.txt
https: // algs4.cs.princeton.edu / 41graph / mediumG.txt
Run nonrecurisve depth-first search on an undirected graph.
Runs in O(E + V) time using O(V) extra space.
% python nonrecursive_depth_first_search.py tinyG.txt 0
0 1 2 3 4 5 6
NOT connected
% python nonrecursive_depth_first_search.py tinyG.txt 9
9 10 11 12
NOT connected
"""
from algs4.graph import Graph
from algs4.stack import Stack
class NRDepthFirstSearch:
def __init__(self, G, s):
self.marked = [False for _ in range(G.V)]
self.count = 0
stack = Stack()
stack.push(s)
while not stack.is_empty():
v = stack.peek()
stack.pop()
if not self.marked[v]:
self.marked[v] = True
self.count += 1
for node in G.adj[v]:
if not self.marked[node]:
stack.push(node)
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)
search = NRDepthFirstSearch(g, s)
for v in range(g.V):
if search.marked[v]:
print(str(v) + " ")
if search.count == g.V:
print("connected")
else:
print("not connected")