Total Complexity | 8 |
Total Lines | 32 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | from abc import ABCMeta, abstractmethod |
||
20 | class DepthFirstSearch(Paths): |
||
21 | marked = None |
||
22 | edgesTo = None |
||
23 | s = None |
||
24 | |||
25 | def __init__(self, G, s): |
||
26 | if isinstance(G, EdgeWeightedGraph): |
||
27 | G = G.to_graph() |
||
28 | self.s = s |
||
29 | vertex_count = G.vertex_count() |
||
30 | self.marked = [False] * vertex_count |
||
31 | self.edgesTo = [-1] * vertex_count |
||
32 | self.dfs(G, s) |
||
33 | |||
34 | def dfs(self, G, v): |
||
35 | self.marked[v] = True |
||
36 | for w in G.adj(v): |
||
37 | if not self.marked[w]: |
||
38 | self.edgesTo[w] = v |
||
39 | self.dfs(G, w) |
||
40 | |||
41 | def hasPathTo(self, v): |
||
42 | return self.marked[v] |
||
43 | |||
44 | def pathTo(self, v): |
||
45 | x = v |
||
46 | path = Stack.create() |
||
47 | while x != self.s: |
||
48 | path.push(x) |
||
49 | x = self.edgesTo[x] |
||
50 | path.push(self.s) |
||
51 | return path.iterate() |
||
52 | |||
89 | return path.iterate() |