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