| Total Complexity | 10 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | from abc import ABCMeta, abstractmethod |
||
| 75 | class EagerPrimMST(MST): |
||
| 76 | path = None |
||
| 77 | pq = None |
||
| 78 | marked = None |
||
| 79 | |||
| 80 | def __init__(self, G): |
||
| 81 | vertex_count = G.vertex_count() |
||
| 82 | self.pq = IndexMinPQ(vertex_count) |
||
| 83 | self.path = Bag() |
||
| 84 | self.marked = [False] * vertex_count |
||
| 85 | |||
| 86 | self.visit(G, 0) |
||
| 87 | |||
| 88 | while not self.pq.is_empty() and self.path.size() < vertex_count - 1: |
||
| 89 | e = self.pq.min_key() |
||
| 90 | w = self.pq.del_min() |
||
| 91 | self.path.add(e) |
||
| 92 | if not self.marked[w]: |
||
| 93 | self.visit(G, w) |
||
| 94 | |||
| 95 | def visit(self, G, v): |
||
| 96 | self.marked[v] = True |
||
| 97 | for e in G.adj(v): |
||
| 98 | w = e.other(v) |
||
| 99 | if not self.marked[w]: |
||
| 100 | if self.pq.contains_index(w): |
||
| 101 | old_e = self.pq.get(w) |
||
| 102 | if less(e, old_e): |
||
| 103 | self.pq.decrease_key(w, e) |
||
| 104 | else: |
||
| 105 | self.pq.insert(w, e) |
||
| 106 | |||
| 107 | def spanning_tree(self): |
||
| 108 | return self.path.iterate() |
||
| 109 |