Prim (Primm) algorithm to find the minimum spanning tree and C language example to explain _c language

Source: Internet
Author: User

Prim algorithm Idea:
starting from any vertex V0 selects its most recent vertex v1 tree T1, then joins and T1 the most recent vertex v2 form tree T2, so repeated until all vertices are in the constituent tree.
Minimum spanning Tree (MST): The spanning tree with the smallest weight.
The application of spanning tree and minimum spanning tree: To connect N cities requires n-1 line. The weight on the edge can be interpreted as the cost of the line. The minimum spanning tree represents the spanning tree that makes it the least expensive.
The minimum spanning tree of a construction network must address the following two issues:
1, as far as possible, select the edge of small weights, but can not constitute a circuit;
2, select the appropriate edge of n-1 to connect n vertices;
MST property: Suppose G= (v,e) is a connected network, and U is a non-empty subset of vertex V. if (u,v) is an edge with a minimum weight, where u∈u,v∈v-u, there must be a minimum spanning tree containing edges (u,v).
The prim algorithm assumes that g= (v,e) is connected, and Te is the set of edges in the smallest spanning tree on G. The algorithm starts with u={u0} (U0∈V), te={}. Repeat the following actions:
In all u∈u,v∈v-u edges (u,v) ∈e, an edge (u0,v0) with the smallest weight value is merged into the set TE, and V0 is merged into U until V=u.
At this point, the TE must have n-1 bars, t= (v,te) is the minimum spanning tree of G.
The core of the prim algorithm: Always keep the edges set in te form a spanning tree.
Note: The prim algorithm is suitable for dense graphs, whose time complexity is O (n^2), its time complexity is independent of the number of edges, and the Kruskal algorithm has the time complexity of O (Eloge) with the number of edges, suitable for sparse graphs.
Give a simple example to illustrate the specific implementation method:

G: Graph, expressed by adjacency matrix
Vcount: Represents the number of vertices of a graph
Max_vertexes: Maximum number of nodes in graphs
Infinity: For Infinity
Array storage starts from 0
Because the minimum spanning tree contains each vertex, the selection of vertices can be labeled used[max_vertexes with an array directly (we use the variable definitions in the program code directly, which is also easy to understand); When an array is selected, it is marked, and now there is a problem. How to select the minimum weight edge, note that there is a limit to the minimum weight edge, one vertex of the edge must be in the selected vertex, the other vertex of course is in the set of the selected vertices. One of my first thoughts was a poor search, is to select a vertex in a collection to find the smallest value in another set, which, while easy to understand, is obviously not very efficient, and provides a better way to solve the MIN data structure: Set two secondary arrays lowcost[ Max_vertexes] and closeset[max_vertexes],lowcost[max_vertexes] arrays record edges with the lowest cost from u to v-u. For each vertex v∈v-u,closedge[v], closeset[max_vertexes] records the vertices of the side attached to the U.

Prim algorithm steps:
T0 The edge of the build tree, the initial value is empty
Weighted adjacency Matrix C = (CIJ) nxn (with infinite connection between two points) for input weighting graphs
Add a property L (v) to each vertex V: the minimum direct distance of table V to T0
(1) t0←∅, v1={v0}, C (T0) =0
(2) for arbitrary v∈v,l (v) ←c (V, V0)
(3) If v==v1 then stop else goto next.
(4) Find the point u in the V-v1 L (U) =min{l (V) | v∈ (V−V1)}, and the adjacent point in the V1 is W.
(5) t0←t0∪{(U, W)}, C (T0) ←c (T0) +c (U, W), V1←v1∪{u}
(6) to any v∈ (V−V1) if C (V, u) <l (v) then L (v) = C (V, u) Else L (v) unchanged.
(7) Go to 3.

C + + Implementation Example
What's in Prim.txt:

1 2 6
1 3 1 1 4 5 2 3 5 2 5 3 3 4 5 3 5 6 3 6 4 5 6 6 4 6 2


Program code:

#include <stdo.h> #include <string.h> #include <stdlib.h> #define INFINITY 1000000//define two not directly adjacent one step to the vertex  Distance #define Max_vertexes 6//define the number of vertices in the graph the weight void prim on the edge of the graph[max_vertexes][max_vertexes];//int (graph G,int
  Vcount,int father[]) {int i,j,k;
  int lowcost[max_vertexes];//The vertex of the weighted int closeset[max_vertexes],used[max_vertexes];//attached to u at the minimum cost edge; whether the tag has been selected int min; 
      int result=0;//records the shortest distance weights and for (i=0;i<vcoun;k++)//Initializes all arrays, initializing the shortest distance to the distance of other vertices to 1 nodes {Lowcost[i]=g[0][i];   
   closeset[i]=0;  
   used[i]=0;   
  Father[i]=-1;
 
 
  
  } used[0]=1;
    for (i=1;i<=vcount-1;i++) {j=0;
      
   min = infinity;
    for (k=1;k<count;k++)//for loops get the nearest vertex J if (!used[k) && (lowcost[k] {min = lowcost[k];
   J=k; 
   } Father[j]=closeset[j];
   printf ("%d%d\n", j+1,father[j]+1);//output The currently found node, the vertex attached to the last node Result=result+g[j][closeset[j]]; used[j]=1;;/ /The J Vertex is incorporated into the U-k=1;k if (!used[k]&& (G[j][k] to the shortest path to K {lowcost[k]=g[j][k];
    Closeset[k]=j;
printf ("%d", result);
  int main () {FILE *fr;
  int i,j,weight;
  Graph G;
  int fatheer[max_vertexes];
  For (i=0, i<max_vertexes;i++) for (j=0 j<max_vertexer;i++) g[i][j] = infinity;
  FR = fopen ("Prim.txt", "R");
   if (!FR) {printf ("fopen failed\n");
  Exit (1);
   while (Fscanf (FR, "%d%d%d", &i, &j, &weight)!= EOF) {g[i-1][j-1] = weight;
  G[J-1][I-1] = weight;
  } prim (G,max_vertexes,fatheer);
 
return 0;
 }

The results of the

test 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.