Evaluate the Minimum Spanning Tree MST using the prim algorithm and compare it with the Kruskal Algorithm

Source: Internet
Author: User
1. Resolution

PrimAlgorithmSimilar to the Dijkstra algorithm, their pseudo code is almost similar, except that the key values sorted by their priority queue are different. The key value of the prim algorithm is the weight of the lightest edge between the node and the vertex in the Set S. In the Dijkstra algorithm, the key value is the full path length from the start point to a node.

The following blog will show the differences between the minimum spanning tree (MST) and the shortest path.

2. Code Instance

# Include <iostream> <br/> # include <malloc. h> <br/> # include <queue> <br/> # include <algorithm> <br/> # include <stdlib. h> <br/> # include <functional> <br/> using namespace STD; </P> <p> # define maxnum 100 // defines the maximum number of vertices in the adjacent area. <br/> # define maxweight 1000000 // maximum edge weight <br/> int cost [maxnum ]; // The weight between the user's storage node and the Minimum Spanning Tree MST. The initial state is cost [I] = maxweight <br/> int prev_elem [maxnum]; // The pre-order of the storage node of this array. For example, if Prev [v] = u, the pre-order of U is V. <br/> int set [maxnum]; // set In combination with S, it is null at the beginning, and a vertex is determined during initialization. Subsequent vertices are found through the prim algorithm. Set [I] = 1 indicates that vertex I belongs to the set S <br/> // vertex Information <br/> typedef struct <br/>{< br/> int ID; // vertex number <br/> int cost; // The minimum weight used to save the vertex to the MST <br/>} node; <br/> // The structure of the graph's Adjacent matrix <br/> typedef struct <br/> {<br/> // char V [maxnum]; // vertex information of the graph <br/> node V [maxnum]; <br/> int e [maxnum] [maxnum]; // vertex information of the graph <br/> int vnum; // Number of vertices <br/> int Enum; // Number of edges <br/>} graph; <br/> // function declaration <br/> void creategraph (graph * g); // create a graph G <br/> void prim (graph * g ); // core algorithm, BF S, but changed the normal queue to the priority queue. <Br/> int CMP (node A, Node B); // defines whether priority queues are listed in ascending or descending order. <br/> int CMP (node A, Node B) <br/>{< br/> return. cost <B. cost; // Ascending Order <br/>}< br/> // prim algorithm <br/> void prim (graph * g) <br/>{< br/> node Q [maxnum]; // defines the struct array <br/> int front; // queue header <br/> int rear; // end of the queue <br/> int count; // queue count <br/> front = rear = COUNT = 0; // indicates that the queue is empty <br/> int K, i, J; <br/> // initialize the cost value <br/> for (I = 1; I <= G-> vnum; I ++) <br/>{< br/> G-> V [I]. cost = maxweight ;/ /Cost is the maximum value <br/> G-> V [I]. id = I; <br/> prev_elem [I] =-1; <br/> set [I] = 0; <br/>}< br/> G-> V [1]. cost = 0; // 1 is the first vertex of the MST, whose cost is initialized to 0 <br/> // initialize the priority queue <br/> for (I = 1; I <= G-> vnum; I ++) <br/> {<br/> q [++ rear] = G-> V [I]; <br/> count ++; // The vertex enters the queue q <br/>}</P> <p> while (count> 0) // The queue is not empty and keeps repeating, also, the front <rear, indicating that the queue is not empty, Count = rear-front <br/>{< br/> sort (q + front + 1, q + rear + 1, CMP); // sort queue Q in ascending order of cost. <Br/> // The following two lines are the queue departure Operations <br/> node n1 = Q [++ front]; // retrieve the point closest to S in the current non-s set <br/> count --; // the outbound queue operation <br/> K = n1.id; // <br/> cout <k <Endl; <br/> set [k] = 1; // Add the vertex obtained from the queue to the set S <br/> for (j = 1; j <= G-> vnum; j ++) <br/>{< br/> If (G-> E [k] [J]! = Maxweight & set [J] = 0) // I-> edge exists between J, and when vertex J does not belong to set S <br/>{< br/> // if the distance from vertex J to set S is greater than the distance from a K to J in the set, update the J-point distance weight <br/> // and set the pre-order of the J to K. <br/> If (G-> V [J]. cost> G-> E [k] [J]) <br/> // If (G-> V [J]. cost> G-> E [k] [J]) <br/> {<br/> G-> V [J]. cost = G-> E [k] [J]; <br/> prev_elem [J] = K; <br/>}< br/> // update the queue <br/> for (I = 1; I <= G-> vnum; I ++) <br/> {<br/> q [I] = G-> V [I]; <br/>}< br/> cout <"Update cost value of each vertex after cost" <Endl; <br/> for (I = 1; I <= G-> vnum; I ++) <br/> cout <G-> V [I]. cost <""; <br/> cout <Endl; <br/>}< br/> void creategraph (graph * g) // create graph G <br/>{< br/> cout <"creating undirected graph... "<Endl; <br/> cout <" Enter the number of vertices vnum: "; <br/> CIN> G-> vnum; <br/> int I, j; <br/> // construct an adjacent matrix. The distance from a vertex to itself is infinite. <Br/> cout <"input Matrix Weight:" <Endl; <br/> for (I = 1; I <= G-> vnum; I ++) <br/> for (j = 1; j <= G-> vnum; j ++) <br/>{< br/> CIN> G-> E [I] [J]; <br/> If (G-> E [I] [J] = 0) <br/> G-> E [I] [J] = maxweight; <br/>}</P> <p> int main () <br/>{< br/> int I; <br/> graph * g; <br/> G = (graph *) malloc (sizeof (graph); <br/> creategraph (g ); <br/> prim (g); </P> <p> // For (int K = 1; k <= G-> vnum; k ++) <br/> // {<br/> // cout <G-> V [K]. cost <"; <br/> //} <Br/>/* cout <Endl; */<br/> cout <"output each edge of the MST" <Endl; <br/> for (I = 1; I <= G-> vnum; I ++) <br/> {<br/> // cout <prev_elem [I] <""; <br/> If (prev_elem [I]! =-1) <br/> cout <"Edge:" <prev_elem [I] <"->" <I <", with the weight: "<G-> E [prev_elem [I] [I] <Endl; <br/>}< br/> // cout <Endl; <br/> system ("pause"); <br/> return 0; <br/>}< br/>/* <br/> creating an undirected graph... <br/> enter the number of vertices (vnum: 6) <br/> enter the right of the adjacent matrix: <br/> 0 5 6 4 0 0 <br/> 5 0 1 2 0 0 <br/> 6 1 0 2 5 3 <br/> 4 2 0 0 4 <br/> 0 0 5 0 0 4 <br/> 0 0 3 4 4 0 <br/> output edge of the MST <br/> edge: 3-> 2, weight: 1 <br/> edge: 4-> 3, weight: 2 <br/> edge: 1-> 4, the weight is 4 <br/> edge: 6-> 5, the weight is 4 <br/> edge: 3-> 6, and the weight is: 3 <br/> press any key to continue... <br/> */

3. Summary of the Kruskal algorithm and prim algorithm

In short, Kruskal is an edge search algorithm, and prim Is a vertex search algorithm.

3.1 basic concepts of the Kruskal algorithm:

The greedy principle used by the Kruskal algorithm to select n-1 edges each time is as follows:Select one from the remaining edgeNo LoopsWithMinimum edge consumptionAdd to the set of selected edges. Note that if the selected edge generates a loop, it is impossible to form a spanning tree.

The Kruskal algorithm is divided into steps E, where E is the number of edges in the network. This e-edge is considered in an ascending order of consumption (edge weight), and each time an edge is considered. When you consider an edge, if it is added to the selected edge set, a loop will appear, then it will be discarded; otherwise, it will be selected.

 

3.2 The basic idea of the prim algorithm is:
1) In Figure G = (V, E) (V indicates vertex, e Indicates edge), any vertex (such as vertex V0) from set V is put into set U, at this time, u = {v0}, the set T (e) is empty.
2) starting from v0, find another vertex V1 that is adjacent to the vertex in U (another vertex is in V) and add V1 to u. That is, u = {v0, V1}, and add the edge to the T (e) set.
3) Repeat (2) until u = v.
T (e) Has n-1 edges, and t = (u, T (e) is a minimal spanning tree.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.