Algorithm Introduction Learning-prim algorithm

Source: Internet
Author: User

I. About the minimum spanning tree

For undirected connected graph g= (v,e), where V represents the vertex of the graph, E represents the edge of the graph, there is a weight for each edge, which can be understood as the weight of the edge a->b C is the distance from a to B to go for C. Now we want to find a non-loop subset of T, and T is a subset of E, and T connects all the vertices, with their weights and minimums. So a sub-figure g ' = (v,t) is called the smallest spanning tree of Figure G.

Two. Basic properties of the minimum spanning tree

Minimum number of edges of spanning tree | T| must obey | t|=| V|-1.

Minimum spanning tree can not have loops

The minimum spanning tree does not have to be unique.

Three. Prim algorithm

There are two algorithms for the minimum spanning tree: The prim algorithm and the Kruskal algorithm, only the prim algorithm is mentioned here. The core of the prim algorithm is two dynamic sets U and V-u. Here to illustrate the more vivid, I use the military invasion of the way to illustrate the prim algorithm operation process. Suppose there is a military base of ours, assuming that the base number is 1 (which can be changed according to different circumstances), the other n-1 military bases are the location of the enemy's armed forces. And assuming that our military power is unprecedented and powerful, who will kill who, but even so, we do not want to expend unnecessary force (here can be understood as not to take the unnecessary road), our Army wants to design a set of marching route, yes, the total March route is the smallest and eliminate all the local military forces. The following is an example of how the prim algorithm performs:

As shown there are 6 military bases, except the first one is occupied by us, the rest are enemy forces. Based on the prim algorithm, we first found a military attack on the nearest military base of 1th, 3 (1-3 distance is 1,,1-2 distance of 6, 1-4 distance is 5). After occupying base 3rd, we continued to find the nearest base from the red mark Base, and found that base 6th was located at the distance of {1,3} 4, the nearest base. So we are going to use base 6th as the next hit target. After occupying number 6th, it was found that base 4th was located at the distance of {1,3,6} 2, the nearest base, so the base 4th was the next occupying base. Similarly, the e,f diagram is followed by analogy.

The complete code is attached below:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intMax_size= -;6 Const intinf=1<< -;7 intMap[max_size][max_size];8 structedge{9     intC,flag;Ten}edge[max_size*max_size/2]; One intn,m; A intPrim () { -     ints=1, sum=0; -      for(intI=1; i<=n;i++){ the         if(i==s)Continue; -Edge[i].c=Map[s][i]; -edge[i].flag=0; -     } +edge[s].flag=1; -Edge[s].c=0; +      A      for(intk=1; k<=n-1; k++) {//Loop n-1 times at         intmmin=inf,flag=0, nearest; -          for(intI=1; i<=n;i++){ -             if(!edge[i].flag&&edge[i].c<mmin) { -mmin=edge[i].c; -flag=1;  -nearest=i; in             } -         } to         if(!flag)return-1; +edge[nearest].flag=1; -sum+=mmin;  the          for(intI=1; i<=n;i++){ *             if(!edge[i].flag&&edge[i].c>Map[nearest][i]) { $Edge[i].c=Map[nearest][i];Panax Notoginseng             } -         }         the     } +     returnsum; A } the intMain () { +      while(SCANF ("%d%d", &n,&m)! =EOF) { -         inta,b,c; $memset (Map,inf,sizeof(map)); $          for(intI=1; i<=m;i++){ -scanf"%d%d%d",&a,&b,&c); -map[a][b]=map[b][a]=C; the         } -         if(Prim ()) printf ("%d\n", Prim ());Wuyi         Elseprintf"fail\n"); the     } -}

Algorithm Introduction Learning-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.