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