| Total Complexity | 8 |
| Total Lines | 36 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | from abc import ABCMeta, abstractmethod |
||
| 54 | class BreadthFirstSearch(Paths): |
||
| 55 | |||
| 56 | marked = None |
||
| 57 | s = None |
||
| 58 | edgeTo = None |
||
| 59 | |||
| 60 | def __init__(self, G, s): |
||
| 61 | if isinstance(G, EdgeWeightedGraph): |
||
| 62 | G = G.to_graph() |
||
| 63 | self.s = s |
||
| 64 | vertex_count = G.vertex_count() |
||
| 65 | self.marked = [False] * vertex_count |
||
| 66 | self.edgeTo = [-1] * vertex_count |
||
| 67 | |||
| 68 | queue = Queue.create() |
||
| 69 | |||
| 70 | queue.enqueue(s) |
||
| 71 | while not queue.is_empty(): |
||
| 72 | v = queue.dequeue() |
||
| 73 | self.marked[v] = True |
||
| 74 | for w in G.adj(v): |
||
| 75 | if not self.marked[w]: |
||
| 76 | self.edgeTo[w] = v |
||
| 77 | queue.enqueue(w) |
||
| 78 | |||
| 79 | def hasPathTo(self, v): |
||
| 80 | return self.marked[v] |
||
| 81 | |||
| 82 | def pathTo(self, v): |
||
| 83 | x = v |
||
| 84 | path = Stack.create() |
||
| 85 | while x != self.s: |
||
| 86 | path.push(x) |
||
| 87 | x = self.edgeTo[x] |
||
| 88 | path.push(self.s) |
||
| 89 | return path.iterate() |