[Explain]prim algorithm < minimum spanning tree >

Source: Internet
Author: User

The method of minimum spanning tree is commonly used Kruskal and prim algorithm.

One is by the edge from small to large plus, one is by point from small to large plus, two methods are more commonly used, are not difficult ...

Kruskal algorithm in this article I will not talk about, the focus of this article is to talk about prim algorithm, has never learned, just understand the idea, the original thought it is difficult, the result thought that good understanding

Prim that can use the adjacency matrix can also be used adjacent linked list, but the time to optimize the adjacency list is not much, but still can optimize a lot of space

The prim algorithm first enumerates the first points, adds the selected points to the point set V, the selected points in the point set U, and then sets the nearest point in the U-set for the distance V, and then joins it to the U-set

We're still using a diagram to illustrate

Let's simulate this process again ...

First, lowcost represents the minimum distance from the current point to the V-set, and the MST represents the point from the current point to the smallest V-set of the V-set

We'll start at 1 o'clock.

Judging and 1 points connected to the point, lowcost[2]=6,lowcost[3]=1,lowcost[4]=5,lowcost[5]=lowcost[6]=inf

Mst[2]=1,mst[3]=1,mst[4]=1;

Then run the whole point of the graph, find the smallest lowcost and add this point to the V-set, removed from the U-set (the delete operation is to assign the Lowcost to 0)

Since the V-set is 3 more, the whole graph lowcost[2]=5,lowcost[4]=5,lowcost[5]=6,lowcost[6]=4 is updated;

mst[2]=3,mst[4]=1,mst[5]=3,mst[6]=3;

Then find the entire U-set found Lowcost minimum is 6 points, V Set added 6 points, update u set

Lowcost[2]=5,lowcost[4]=2,lowcost[5]=6

Mst[2]=3,mst[4]=6,mst[5]=3

Then find Lowcost the smallest 4 points, join the V-set, update the U-set

Lowcost[2]=5,lowcost[5]=6

Mst[2]=3,mst[5]=3

Find Lowcost minimum point 2, join V-Set, update U-set

lowcost[5]=3,mst[5]=2

Join V-Set, all points have been added, complete operation, output ans=15

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <cmath>5#include <queue>6#include <algorithm>7#include <cstdlib>8 #defineMAXN 10059 using namespacestd;Ten  One intN,m,dis[maxn][maxn],ans; A intLOWCOST[MAXN],MST[MAXN]; -  - intRead () { the     intxx=0, ff=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') ff=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {xx=xx*Ten+ch-'0'; ch=GetChar ();} -     returnxx*ff; + } -  + voidPrimintu) { A      for(intj=1; j<=n;j++){ at         if(dis[u][j]>0){ -lowcost[j]=dis[u][j];mst[j]=u; -         } -     } -mst[u]=0; lowcost[u]=0; -     intMinid=0, minn=0x3f3f3f, tot=1; in      while(tot<N) { -Minid=0, minn=0x3f3f3f; to          for(intI=1; i<=n;i++){ +             if(lowcost[i]!=0&&lowcost[i]<Minn) { -minn=Lowcost[i]; theMinid=i; *             }         $         }Panax Notoginsengtot++; -ans+=Minn; themst[minid]=0; lowcost[minid]=0; +          for(intI=1; i<=n;i++){ A             if(lowcost[i]!=0&&dis[minid][i]<Lowcost[i]) { thelowcost[i]=Dis[minid][i]; +mst[i]=MiniD; -             } $         }             $     }     - } -  the intMain () { -N=read (); m=read ();Wuyimemset (DIS,0x3f3f3f,sizeof(DIS)); the      for(intI=1; i<=m;i++){ -         intx,y,v; WuX=read (); Y=read (); v=read (); -dis[x][y]=dis[y][x]=v; About     } $Prim1); -printf"%d\n", ans); - } - /* A 6 Ten + 1 3 1 the 1 2 6 - 1 4 5 $ 2 3 5 the 3 4 5 the 2 5 3 the 3 5 6 the 5 6 6 - 3 6 4 in 4 6 2 the */
Prim

[Explain]prim algorithm < minimum 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.