Minimum Spanning Tree-prim algorithm

Source: Internet
Author: User

The minimal spanning tree is an important application in the data structure graph. It requires a full graph with no permission
Select n-1 edges in the graph and make the graph still connected (that is, a spanning tree is also obtained). At the same time, we also need to minimize the weight of the tree.

 

The following code includes the complete steps for the prim algorithm to calculate the Minimum Spanning Tree to generate an undirected network chart. Please watch [animation demonstration prim algorithm
] To verify that the program is correct.

Code Description:

Lowcost [] is used to save the weight of the shortest edge of each vertex in the Set V-U and the set U. lowcost [v] = 0 indicates that vertex V has been added to the minimal spanning tree;

Adjvex [] is used to save the vertex attached to the edge in the set U.

The initial undirected weighted diagram is as follows:

 

 

The complete code is as follows:

# Include <iostream> <br/> using namespace STD; <br/> # define maxvertexnum 100 // maximum number of vertices <br/> # define INF 32767 // you can set the unattainable weight to 32767 infinitely. <br/> typedef char vertextype; <br/> typedef int edgetype; <br/> typedef struct <br/>{< br/> vertextype vertex [maxvertexnum]; // vertex table <br/> edgetype edges [maxvertexnum] [maxvertexnum]; // adjacent matrix, which can be viewed as an edge table <br/> int N, E; // Number of vertices and edges in the graph <br/>} mgraph; <br/> void createmgraph (mgraph & G) <br/> {<Br/> int I, J, K, M; <br/> cout <"Enter the number of vertices and edges :"; <br/> CIN> G. n> G. e; <br/> cout <"Enter the vertex element:"; <br/> for (I = 0; I <G. n; I ++) <br/>{< br/> CIN> G. vertex [I]; <br/>}< br/> for (I = 0; I <G. n; I ++) <br/>{< br/> for (j = 0; j <G. n; j ++) <br/>{< br/> G. edges [I] [J] = inf; // initialize unlimited power <br/> if (I = J) <br/>{< br/> G. edges [I] [J] = 0; // The diagonal line is 0 <br/>}< br/> // create an undirected network chart <br/> cout <"please enter the vertex numbers at both ends of the edge and the corresponding weights: /n "; <br/> for (k = 0; K <G. e; k ++) <br/>{< br/> CIN> I >>j> m; <br/> G. edges [I] [J] = m; <br/> G. edges [J] [I] = m; <br/>}< br/> int minedge (INT lowcost [], int N) <br/>{< br/> int I, K, min = inf; <br/> for (I = 0; I <n; I ++) <br/>{< br/> If (lowcost [I] <min & lowcost [I]! = 0) <br/>{< br/> min = lowcost [I]; <br/> K = I; <br/>}< br/> return K; <br/>}< br/> void prim (mgraph g, int lowcost [], char adjvex []) <br/>{< br/> int I, j, k; <br/> for (I = 1; I <G. n; I ++) // initialize two secondary arrays <br/>{< br/> lowcost [I] = G. edges [0] [I]; <br/> adjvex [I] = 0; <br/>}< br/> lowcost [0] = 0; // Add vertex 0 to set U <br/> for (I = 1; I <G. n; I ++) <br/>{< br/> K = minedge (lowcost, G. n); <br/> cout <"(" <(char) (adjvex [k] + '0') <"--->" <(char) (K + '0') <")" <lowcost [k] <Endl; <br/> lowcost [k] = 0; <br/> for (j = 0; j <G. n; j ++) // different from that in the book. Here, J must start from 0 and adjust the array lowcost and adjvex <br/>{< br/> If (G. edges [k] [J] <lowcost [J] & G. edges [k] [J]! = 0) <br/>{< br/> lowcost [J] = G. edges [k] [J]; <br/> adjvex [J] = K; <br/>}< br/> int main () <br/>{< br/> mgraph g; <br/> int lowcost [25]; <br/> char adjvex [25]; <br/> memset (lowcost, 0, sizeof (lowcost )); <br/> memset (adjvex, '/0', sizeof (adjvex); <br/> createmgraph (g); <br/> prim (G, lowcost, adjvex); <br/> return 0; <br/>}< br/>

 

The program test results are as follows:

 

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.