-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_search.py
97 lines (74 loc) · 2.54 KB
/
graph_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
##########################
### Depth-First Paths. ###
##########################
class DepthFirstPaths:
# Find vertices connected to v.
def __init__(self, graph, s):
self.marked = [False] * graph.V()
# edgeTo[w] == v means that edge v-w was taken to visit w for first time.
self.edgeTo = [None] * graph.V()
self.s = s
self.__depthFirstSearch(graph, s)
# Return true if there is a path from s to v.
def hasPathTo(self, v):
return self.marked[v]
# Return the all the vertices on the path from s to v.
# If no such path exists, return None.
def pathTo(self, v):
if not self.hasPathTo(v):
return None
path = []
while v != self.s:
path.append(v)
v = self.edgeTo[v]
path.append(self.s)
return iter(path[::-1])
###
### HELPER FUNCTIONS
###
def __depthFirstSearch(self, graph, v):
self.marked[v] = True
for w in graph.adj(v):
if self.marked[w]:
continue
self.edgeTo[w]= v
self.__depthFirstSearch(graph, w)
############################
### Breadth-First Paths. ###
############################
class BreadthFirstPaths:
# Find the shortest paths from s to all vertices connected to s.
def __init__(self, graph, s):
# edgeTo[w] == v means that edge v-w was taken to visit w for first time.
self.edgeTo = [None] * graph.V()
# Distances(shortest as this is bfs) from vertices to s. None if not connected.
self.dist = [None] * graph.V()
self.marked = [False] * graph.V()
self.queue = ResizingArrayQueue()
self.s = v
self.queue.enqueue(v)
self.marked[v] = True
self.dist[v] = 0
while not self.queue.isEmpty():
w = self.queue.dequeue()
for q in graph.adj(w):
if self.marked[q]:
continue
self.queue.enqueue(q)
self.marked[q] = True
self.dist[q] = self.dist[w] + 1
self.edgeTo[q] = w
# Return true if there is a path from s to v.
def hasPathTo(self, v):
return self.marked[v]
# Return the all the vertices on the path from s to v.
# If no such path exists, return None.
def pathTo(self, v):
if not self.hasPathTo(v):
return None
path = []
while v != self.s:
path.append(v)
v = self.edgeTo[v]
path.append(self.s)
return iter(path[::-1])