Prim algorithm and Kruskal algorithm (minimum spanning tree algorithm in graph theory)

Source: Internet
Author: User

The minimum spanning tree can have multiple in a graph, but if the weights of the edges in a graph are different, then the smallest spanning tree may only exist, and it is easy to prove it with contradiction.

Of course the minimum spanning tree is also a graph that contains the weights and the lowest sub-graphs of all nodes.

In a graph of the least weight of the edge must be in the smallest spanning tree, if a graph contains a ring, the ring of the largest weight of the edge must not be in the smallest spanning tree, there is a connection graph of any of the two-divided edge of the minimum weight of the value must be in the smallest spanning tree.

The following is a description of the two algorithms.

Prim algorithm

The prim algorithm takes any point as the source point, divides all the points into two groups, a point that is already on the smallest spanning tree, and another set of points that are not yet on the smallest spanning tree, initially with only the source point on the smallest spanning tree. There must be such an edge in the diagram: one of their vertices is on the smallest spanning tree, the other is not on the smallest spanning tree, the shortest of the weights in those edges and the connected points are added to the minimum spanning tree, continuing the loop until all points are on the smallest spanning tree. In a specific implementation, a secondary array containing all nodes can be recorded, each element of this array contains the point to the smallest spanning tree of the shortest edge of the weight and the other end of the edge (that is, the point already in the tree), if the point is already in the smallest spanning tree, the weight is 0, So each time a new point is added to the minimum spanning tree, it is checked for all points adjacent to it, and if those adjacent points to the newly added distance are less than the distance from the midpoint of the original tree, the information of those points in the auxiliary array is updated.

intPrim (structGraph *g,structVertex vertex_list[],intS) {    intI, J, Min_cost, tmp_index, sum =0;  for(i =0; I < g->vertex_num; i++){        if(S! =i) {Vertex_list[i].adjacent_vertex=S; Vertex_list[i].low_cost= g->Map[s][i]; }        Else{Vertex_list[i].adjacent_vertex= -1; Vertex_list[i].low_cost=0; }    }     for(j =1; J < g->vertex_num; J + +) {Min_cost=Infinity;  for(i =0; I < g->vertex_num; i++){            if(0! = vertex_list[i].low_cost&&min_cost >vertex_list[i].low_cost) {Min_cost=Vertex_list[i].low_cost; Tmp_index=i; }} sum+=Min_cost; Vertex_list[tmp_index].low_cost=0;  for(i =0; I < g->vertex_num; i++){            if(0! = vertex_list[i].low_cost&&vertex_list[i].low_cost > g->Map[tmp_index][i]) {Vertex_list[i].adjacent_vertex=Tmp_index; Vertex_list[i].low_cost= g->Map[tmp_index][i]; }        }    }        returnsum;}

Kruskal algorithm

The Kruskal algorithm is to add all the edges to the minimum spanning tree from small to large, and ensure that no ring is formed, and that the way of not forming a ring is similar to and looked up.

intKruskal (structGraph *G) {    intindex =0, I, j, sum =0, TMP, Tmp_1, tmp_2; intVertex_list[g->Vertex_num]; structEdge edge_list[g->Edge_num];  for(i =0; I < g->vertex_num; i++) {Vertex_list[i]=i; }     for(i =0; I < g->vertex_num; i++){         for(j = i; J < g->vertex_num; J + +){            if(Infinity! = g->Map[i][j]) {Edge_list[index].start=i; Edge_list[index].end=J; Edge_list[index].weight= g->Map[i][j]; Index++; }}} qsort (Edge_list, G->edge_num,sizeof(structEdge), comp);  for(i =0; I < g->edge_num; i++){        if(Vertex_list[edge_list[i].start]! =Vertex_list[edge_list[i].end]) {             for(j =0; J < g->vertex_num; J + +){                if(Vertex_list[j] = =Vertex_list[edge_list[i].end]) {Vertex_list[j]=Vertex_list[edge_list[i].start]; }} sum+=Edge_list[i].weight; printf ("%d%d\n", Edge_list[i].start, edge_list[i].end); }    }        returnsum;}

Here is the complete code, and I haven't measured it.

////main.c//Smallest Spanning Tree////Created by Yunanrong on 2016/11/27.//Copyright 2016 Yunanrong. All rights reserved.//#include<stdio.h>#include<stdlib.h>#defineMAXV 100#defineInfinity 10000000structvertex{intAdjacent_vertex; intlow_cost;};structedge{intstart; intend; intweight;};structgraph{intMAP[MAXV][MAXV]; intEdge_num; intVertex_num;};intCompConst void*a,Const void*b) {    structEdge *pa = (structEdge *) A; structEdge *PB = (structEdge *) b; if(Pa->weight < pb->weight) {        return-1; }    Else{        return 1; }}intPrim (structGraph *g,structVertex vertex_list[],intS) {    intI, J, Min_cost, tmp_index, sum =0;  for(i =0; I < g->vertex_num; i++){        if(S! =i) {Vertex_list[i].adjacent_vertex=S; Vertex_list[i].low_cost= g->Map[s][i]; }        Else{Vertex_list[i].adjacent_vertex= -1; Vertex_list[i].low_cost=0; }    }     for(j =1; J < g->vertex_num; J + +) {Min_cost=Infinity;  for(i =0; I < g->vertex_num; i++){            if(0! = vertex_list[i].low_cost&&min_cost >vertex_list[i].low_cost) {Min_cost=Vertex_list[i].low_cost; Tmp_index=i; }} sum+=Min_cost; Vertex_list[tmp_index].low_cost=0;  for(i =0; I < g->vertex_num; i++){            if(0! = vertex_list[i].low_cost&&vertex_list[i].low_cost > g->Map[tmp_index][i]) {Vertex_list[i].adjacent_vertex=Tmp_index; Vertex_list[i].low_cost= g->Map[tmp_index][i]; }        }    }        returnsum;}intKruskal (structGraph *f) {    intindex =0, I, j, sum =0, TMP, Tmp_1, tmp_2; intVertex_list[g->Vertex_num]; structEdge edge_list[g->Edge_num];  for(i =0; I < g->vertex_num; i++) {Vertex_list[i]=i; }     for(i =0; I < g->vertex_num; i++){         for(j = i; J < g->vertex_num; J + +){            if(Infinity! = g->Map[i][j]) {Edge_list[index].start=i; Edge_list[index].end=J; Edge_list[index].weight= g->Map[i][j]; Index++; }}} qsort (Edge_list, G->edge_num,sizeof(structEdge), comp);  for(i =0; I < g->edge_num; i++){        if(Vertex_list[edge_list[i].start]! =Vertex_list[edge_list[i].end]) {             for(j =0; J < g->vertex_num; J + +){                if(Vertex_list[j] = =Vertex_list[edge_list[i].end]) {Vertex_list[j]=Vertex_list[edge_list[i].start]; }} sum+=Edge_list[i].weight; printf ("%d%d\n", Edge_list[i].start, edge_list[i].end); }    }        returnsum;}voidInit (structGraph *f) {    intI, J, start, end, weight; scanf ("%d%d", & (G->edge_num), & (g->vertex_num));  for(i =0; I < g->vertex_num; i++){         for(j =0; J < g->vertex_num; J + +) {G-&GT;MAP[I][J] =Infinity; }    }     for(i =0; I < g->edge_num; i++) {scanf ("%d%d%d", &start, &end, &weight); G->map[start][end] = G->map[end][start] =weight; }}voidOutput (structVertex vertex_list[],intsize) {    inti;  for(i =0; i < size; i++) {printf ("%d%d\n", I, Vertex_list[i].adjacent_vertex); }}intMain () {structGraph G; structVertex *vertex_list; intsum; Init (&G); Vertex_list= (structvertex*)malloc(sizeof(structVertex) *g.vertex_num); Sum= Prim (&g, Vertex_list,3); printf ("%d\n", sum);    Output (Vertex_list, g.vertex_num); Sum= Kruskal (&G); printf ("%d\n", sum);}

Prim algorithm and Kruskal algorithm (minimum spanning tree algorithm in graph theory)

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.