示例#1
0
    /**********************************************************
    *
    *       Kruskal's minimum spanning tree algorithm
    *
    **********************************************************/
    public Edge[] MST_Kruskal()
    {
        int ei, i = 0;//i tracks the current position in the mst array
        Edge e;
        int uSet, vSet;//unused
        UnionFindSets partition;

        // create edge array to store MST
        // Initially it has no edges.
        mst = new Edge[V - 1];

        // priority queue for indices of array of edges

        Edge[] sorted = sortEdges();

        // create partition of singleton sets for the vertices

        partition = new UnionFindSets(V);

        partition.showSets();//at this point, each vertex will be in its own set

        foreach (Edge cur in sorted)
        {
            if (partition.valid(cur))//if the current edge wouldn't create a cycle
            {
                mst[i++] = cur;//add the edge to the mst
                partition.unionByEdge(cur);//union the sets of the two vertices in the current edge
            }

            if (partition.spanning())//if a spanning tree has been formed, the work is complete
            {
                break;
            }
        }

        partition.showSets();//all the vertices will now be in the same set

        return mst;
    }
    /**********************************************************
    *
    *       Kruskal's minimum spanning tree algorithm
    *
    **********************************************************/
    public Edge[] MST_Kruskal()
    {
        int i = 0;//i tracks the current position in the mst array
        UnionFindSets partition;

        // create edge array to store MST
        // Initially it has no edges.
        mst = new Edge[V - 1];

        // priority queue for indices of array of edges

        Edge[] sorted = sortEdges();

        // create partition of singleton sets for the vertices

        partition = new UnionFindSets(V);

        Console.WriteLine("\nSets before creating MST:");
        partition.showSets();//at this point, each vertex will be in its own set

        foreach (Edge cur in sorted)
        {
            if (partition.valid(cur))//if the current edge wouldn't create a cycle
            {
                mst[i++] = cur;//add the edge to the mst
                partition.unionByEdge(cur);//union the sets of the two vertices in the current edge

                //outputting information about the current step to the console
                Console.Write("\nAdded ");
                cur.show();
                Console.Write("\nCurrent contents of sets:\n");
                partition.showSets();
            }

            if (partition.spanning())//if a spanning tree has been formed, the work is complete
            {
                Console.Write("\nMinimum Spanning Tree has been formed\n");
                break;
            }
        }

        Console.WriteLine("\nSets after creating MST:");
        partition.showSets();//all the vertices will now be in the same set

        return mst;
    }