示例#1
0
def minimum_spanning_tree(graph):
    """
    Find the minimum spanning tree in the given graph using 
    Kruskal's algorithm
    :param graph: the graph to find the MST in
    :return: the set of all edges in the MST
    """
    d = DisjointSet()  # initialize disjoint set data structure
    d.make_sets(graph.get_vertices())
    edges = graph.get_edges()  # All edges in graph
    solution = set()  # Set of edges in MST
    quick_sort(edges, 0, len(edges) - 1)  # Sort by edge weight in asc

    for e in edges:
        if d.find(e[0]) != d.find(e[1]):  # if the vertices wont make a cycle
            d.union(e[0], e[1])  # union them
            solution.add(e)  # add the edge to the solution
    return solution