def _minimum_spanning_tree_kruskal_adjacency_list(graph): mst = _generate_mst_object(graph) dsf = DisjointSetForest() for v in graph.vertices: dsf.make_set(v) for _, edge in _sort_edges(graph): u, v = edge.source.name, edge.target.name if dsf.find_root(u) is not dsf.find_root(v): mst.add_edge(u, v, edge.value) dsf.union(u, v) return mst
def _minimum_spanning_tree_kruskal_adjacency_matrix(graph): mst = Graph(*[getattr(graph, str(v)) for v in graph.vertices]) sort_key = lambda item: item[1].value dsf = DisjointSetForest() for v in graph.vertices: dsf.make_set(v) for _, edge in sorted(graph.edge_weights.items(), key=sort_key): u, v = edge.source.name, edge.target.name if dsf.find_root(u) is not dsf.find_root(v): mst.add_edge(u, v, edge.value) dsf.union(u, v) return mst