Minimum spanning Tree ~prim algorithm

Source: Internet
Author: User

The ~ Primm algorithm of the minimum spanning tree

The minimum spanning tree is the selection of n-1 bars from a well-connected graph (with n points, more than (n-1) edges) to connect n points to each other and to minimize the total weight of the tree. There are two famous ways to complete this construction, one is the Kruskal algorithm, which is to sort the weights of each edge, then select small edges to add to the tree, and ensure that it is a tree (that is, it cannot produce loops). The other one is the prim algorithm, which is considered from the point of view. First use map[][] two-dimensional arrays to hold weights between two points, and a one-dimensional array lowcost[] to hold the weights associated with the selected points, and also use a mark[] array to mark the points that have been used. The basic idea is very similar to the Dijkstra algorithm in the shortest path.

Basic idea of PRIM algorithm:

Select a point S from the connected network n={v,e}, choose the side (S,V) associated with it and the least weighted value, and add its vertices to the vertex collection U of the spanning tree. Each subsequent step selects a point that is not in the vertex collection U, minimizing its weight to u, and then adding it to the vertex collection U. This continues until all vertices in the network are joined to the vertex collection of the spanning tree.

The process of constructing a minimal spanning tree using the prim algorithm:

Suppose that in the construction process, the tree's vertex set U and the edge set are red, the points outside the U set are blue, the red and blue edges are purple, and the shortest purple edge is the one currently looking for. Each vertex is selected to be adjusted to the remaining vertices, updating its distance to the tree's vertex set U. Please refer to the code for details:

#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fint map[1010][1010];int lowcost[1010];int Mark [1010];int N,m;int Prim () {int vir,min,sum=0;for (int i=1;i<=n;i++)//Initialize the tag array to a 0,lowcost array to hold the weight of the point associated with the start 1 {mark[i]=0; Lowcost[i]=map[1][i];} mark[1]=1;//will start mark for (int i=1;i<n;i++)//have n points, so you need to find n-1 times {min=inf;for (int j=1;j<=n;j++)//Find the point {if (!mark) of the tree collection Weights least [j]&&lowcost[j]<min) {min=lowcost[j];vir=j;}} if (min==inf) break;sum+=min;//The minimum weight accumulation mark[vir]=1;//mark the points that have been found//update the distance for each point from the tree collection for (int j=1;j<=n;j++) {if (!mark[j]& &lowcost[j]>map[vir][j]) {lowcost[j]=map[vir][j];}}} Return sum;//returns the weight value of the minimum spanning tree}int main () {while (scanf ("%d%d", &n,&m)!=eof) {for (int i=1;i<=n;i++) for (int j=1;j <=n;j++)//Initialize the map array to a larger value map[i][j]=inf;for (int i=0;i<m;i++) {int x,y,cost;scanf ("%d%d%d",&x,&y,& Cost); if (map[x][y]>cost)///record points and weights, if there is a duplicate take the weight of the small {Map[x][y]=cost;map[y][x]=cost;}} printf ("%d\n", Prim ());//Output the weight of this minimum spanning tree}return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Minimum spanning Tree ~prim algorithm

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.