def MST_Kruskal(g): """Compute a minimum spanning tree of a graph using Kruskal's algorithm. Return a list of edges that comprise the MST. The elements of the graph's edges are assumed to be weights. """ tree = [] # list of edges in spanning tree pq = HeapPriorityQueue() # entries are edges in G, with weights as key forest = Partition() # keeps track of forest clusters position = {} # map each node to its Partition entry for v in g.vertices(): position[v] = forest.make_group(v) for e in g.edges(): pq.add(e.element(), e) # edge's element is assumed to be its weight size = g.vertex_count() while len(tree) != size - 1 and not pq.is_empty(): # tree not spanning and unprocessed edges remain weight, edge = pq.remove_min() u, v = edge.endpoints() # (self._origin, self._destination) # line 65 ~ 69 are used for cycle checking. a = forest.find(position[u]) b = forest.find(position[v]) if a != b: tree.append(edge) forest.union(a, b) return tree
def MST_Kruskal(g): """Compute a minimum spanning tree of a graph using Kruskal's algorithm. Return a list of edges that comprise the MST. The elements of the graph's edges are assumed to be weights. """ tree = [] # list of edges in spanning tree pq = HeapPriorityQueue() # entries are edges in G, with weights as key forest = Partition() # keeps track of forest clusters position = {} # map each node to its Partition entry for v in g.vertices(): position[v] = forest.make_group(v) for e in g.edges(): pq.add(e.element(), e) # edge's element is assumed to be its weight size = g.vertex_count() while len(tree) != size - 1 and not pq.is_empty(): # tree not spanning and unprocessed edges remain weight,edge = pq.remove_min() u,v = edge.endpoints() a = forest.find(position[u]) b = forest.find(position[v]) if a != b: tree.append(edge) forest.union(a,b) return tree
def min_span_tree(self): """Use the Kruskal's algorithm to find the minimum spanning tree.""" result = [] forest = Partition(self.nodes) # greedily pick lighter edges first sorted_edges = sorted(self.edges, key=lambda edge: edge.weight) for edge in sorted_edges: start, end = edge.pair # Add the edge to the tree if its nodes belong to different groups if forest.find(start) != forest.find(end): result.append(edge) forest.union(start, end) # the mst must have (self.node_count - 1) edges if len(result) == self.node_count - 1: break return result