| Total Complexity | 6 |
| Total Lines | 21 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | from abc import ABCMeta, abstractmethod |
||
| 16 | class KruskalMST(MST): |
||
| 17 | tree = None |
||
| 18 | |||
| 19 | def __init__(self, G): |
||
| 20 | minpq = MinPQ.create() |
||
| 21 | self.tree = Bag() |
||
| 22 | for e in G.edges(): |
||
| 23 | minpq.enqueue(e) |
||
| 24 | |||
| 25 | uf = UnionFind.create(G.vertex_count()) |
||
| 26 | |||
| 27 | while not minpq.is_empty() and self.tree.size() < G.vertex_count() - 1: |
||
| 28 | e = minpq.del_min() |
||
| 29 | v = e.start() |
||
| 30 | w = e.end() |
||
| 31 | if not uf.connected(v, w): |
||
| 32 | uf.union(v, w) |
||
| 33 | self.tree.add(e) |
||
| 34 | |||
| 35 | def spanning_tree(self): |
||
| 36 | return self.tree.iterate() |
||
| 37 |