# Prim algorithm -- Minimum Spanning Tree

Source: Internet
Author: User

# Include <stdio. h> <br/> # define N 6 <br/> # define TRUE 1 <br/> # define FALSE 0 <br/> # define MAX_VALUE 9999 <br/> typedef struct edge {<br/> int from; <br/> int to; <br/> float weight; <br/>} edge; <br/> edge path [N]; // save steps 1st to N-1 <br/> float c [N + 1] [N + 1]; // Save the information of the adjacent matrix <br/> int closest [N + 1]; // ID of the vertex closest to j in S <br/> float lowcost [N + 1]; // lowcost [j] = c [j] [closest [j] is the distance from the closest vertex of j to S <br/> int s [N + 1]; // indicates whether a vertex is added. <Br/> void prim () {<br/> int I; <br/> s [1] = TRUE; // Add vertex 1 to the Minimum Spanning Tree <br/> for (I = 2; I <= N; I ++) {// initialization <br/> lowcost [I] = c [1] [I]; // minimum distance from any point to the S set, this is equivalent to the distance to vertex 1, because there is only one vertex in S <br/> closest [I] = 1; // any point is closest to vertex 1 in the S set (because only vertex 1 in the S set) <br/> s [I] = FALSE; // No vertex is added to the S collection <br/>}< br/> int pf = 1; <br/> for (I = 1; I <N; I ++) {<br/> float min = MAX_VALUE; <br/> int j = 1; <br/> for (int k = 2; k <= N; k ++) {// select a vertex that is not selected into the minimum spanning tree. Min distance from vertex in S <br/> if (lowcost [k] <min &&(! S [k]) {<br/> min = lowcost [k]; <br/> j = k; <br/>}< br/> printf ("% d, % d/n", j, closest [j]); <br/> path [pf]. from = j; <br/> path [pf]. to = closest [j]; <br/> path [pf]. weight = c [j] [closest [j]; <br/> pf ++; </p> <p> s [j] = TRUE; <br/> for (k = 2; k <= N; k ++) {// for all vertices that do not have the smallest Spanning Tree selected, update <br/> if (c [j] [k] <lowcost [k] & (! S [k]) {<br/> lowcost [k] = c [j] [k]; <br/> closest [k] = j; <br/>}</p> <p> void main () {<br/> c [1] [1] = MAX_VALUE; // vertex 1 <br/> c [1] [2] = 6; <br/> c [1] [3] = 1; <br/> c [1] [4] = 5; <br/> c [1] [5] = MAX_VALUE; <br/> c [1] [6] = MAX_VALUE; <br/> c [2] [1] = 6; // vertex 2 <br/> c [2] [2] = MAX_VALUE; <br/> c [2] [3] = 5; <br/> c [2] [4] = MAX_VALUE; <br/> c [2] [5] = 3; <br/> c [2] [6] = MAX_VALUE; <br/> c [3] [1] = 1; // vertex 3 <br/> c [3] [2] = 5; <br/> c [3] [3] = MAX_VALUE; <br/> c [3] [4] = 5; <br/> c [3] [5] = 6; <br/> c [3] [6] = 4; <br/> c [4] [1] = 5; // vertex 4 <br/> c [4] [2] = MAX_VALUE; <br/> c [4] [3] = 5; <br/> c [4] [4] = MAX_VALUE; <br/> c [4] [5] = MAX_VALUE; <br/> c [4] [6] = 2; <br/> c [5] [1] = MAX_VALUE; // vertex 5 <br/> c [5] [2] = 3; <br/> c [5] [3] = 6; <br/> c [5] [4] = MAX_VALUE; <br/> c [5] [5] = MAX_VALUE; <br/> c [5] [6] = 6; <br/> c [6] [1] = MAX_VALUE; // vertex 6 <br/> c [6] [2] = MAX_VALUE; <br/> c [6] [3] = 4; <br/> c [6] [4] = 2; <br/> c [6] [5] = 6; <br/> c [6] [6] = MAX_VALUE; </p> <p> prim (); </p> <p> printf ("-------------------------------------------/n"); <br/> printf ("The path is:/n "); <br/> for (int I = 1; I <N; I ++) {<br/> printf ("% d ---- % d Weigth: %. 2f/n ", path [I]. from, path [I]. to, path [I]. weight); <br/>}< br/>}

Related Keywords:

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.