/**********************************************************
*
*       Kruskal's minimum spanning tree algorithm
*
**********************************************************/
public Edge[] MST_Kruskal() 
{
    int ei, i = 0;
    Edge e;
    int uSet, vSet;
    UnionFindSets partition;
	int wgt_sum = 0;
    
    // create edge array to store MST
    // Initially it has no edges.
    mst = new Edge[V-1];

    // priority queue for indices of array of edges
    Heap h = new Heap(E, edge);

    // create partition of singleton sets for the vertices
    partition = new UnionFindSets(V);
	
    partition.showSets();
	//Comment this code out if you want to see what is inside the heap at each step
	//Console.WriteLine("Heap content is:");
	//h.display();
	
    while(i < V-1) 
	{
		ei = h.remove();
		//I used the commented code to see how are elements removed from the heap each time
		//Console.WriteLine("\nRemoving element {0} from heap", ei);
		//h.display();
		e = edge[ei];
		uSet = partition.findSet(e.u);
		vSet = partition.findSet(e.v);
		if(uSet != vSet)
		{
			partition.union(uSet, vSet);
			mst[i++] = e;
			wgt_sum += e.wgt;
		}
    }
	Console.WriteLine("Weight of MST is: {0}", wgt_sum);
    return mst;
	
}
示例#2
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;
    }