forked from LeslieK/Algorithms-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch.py
More file actions
37 lines (30 loc) · 833 Bytes
/
DepthFirstSearch.py
File metadata and controls
37 lines (30 loc) · 833 Bytes
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
class DepthFirstSearch(object):
"depth first search graph processing class"
def __init__(self, G, source):
self._source = source
self._marked = [ False for i in range(G.V()) ]
self._edgeTo = [-1 for i in range(G.V()) ]
self._dfs(G, source)
def _dfs(self, Graph, v):
"""
process graph using depth-first search
create marked[] and edgeTo[] data structures
"""
self._marked[v] = True
for w in Graph.adj(v):
if (not self._marked[w]):
self._dfs(Graph, w)
self._edgeTo[w] = v
def hasPathTo(self, v):
"True if source hasPathTo a vertex v"
return self._marked[v]
def pathTo(self, v):
"returns path from source to v"
if not self.hasPathTo(v): return None
i = v
path = []
while i != self._source:
path.append(i)
i = self._edgeTo[i]
path.append(self._source)
return path[::-1]