MST least Spanning Tree and Prim pulm Algorithm

Source: Internet
Author: User

MST has learned the Kruskal algorithm. Another algorithm is called Prim. The difference between the two is that the Prim algorithm is suitable for dense graphs. For example, almost all vertices, such as the Bird's Nest, have connected graphs. The time complexity is O (n ^ 2), and the time complexity is independent of the number of edges. the time complexity of the kruskal algorithm is O (eloge), which is related to the number of edges, suitable for sparse graphs. Basic Idea of the prim algorithm: Assume that G = (V, E) is connected, and TE is the set of edges in the Minimum Spanning Tree on G. The algorithm starts with U = {u0} (u0 ε V) and TE = {empty set. Repeat the following operations: 1. in all u U, v, V-U edge (u, v) in E to find the smallest Edge Weight (u0, v0) into the set TE, while v0 into U, until V = U; next, start with v0 as the edge, continue to find the edge with the smallest weight value into the set TE, reciprocating in turn; 2. finally, there must be n-1 edges in TE, and T = (V, TE) is the minimum spanning tree of G. The core of the Prim algorithm: always keep the edge set in the TE to form a spanning tree, that is, the main difference between it and the Kruskal algorithm is, prim is a greedy algorithm that keeps the connection in series and does not follow the whole process. In fact, you can select the initial point uo at will. Generally, the question-making condition will give or take the smallest weight edge. The implementation code is as follows:

#include <stdio.h>  #include <string.h>  #include <stdlib.h>  

 

#define infinity 1000000  #define max_vertexes 6    

 

 
typedef int Graph[max_vertexes][max_vertexes];   

 

Void prim (Graph G, int vcount, int father []) {int I, j, k; int lowcost [max_vertexes]; int closeset [max_vertexes], used [max_vertexes]; int min; for (I = 0; I <vcount; I ++) {/* the shortest distance initialized to another node to node 1 */lowcost [I] = G [0] [I]; /* indicate that all nodes are attached to the default Node 1 */closeset [I] = 0; used [I] = 0; father [I] =-1 ;} used [0] = 1; /* The first node is in the s set * // * The vcount node requires at least vcount-1 edge to constitute the minimum spanning tree */for (I = 1; I <= vcount-1; I ++) {j = 0; min = infinity;/* find the matching condition K */for (k = 1; k <vcount; k ++) /* The Edge Weight is small and not in the Spanning Tree */if ((! Used [k]) & (lowcost [k] <min) {min = lowcost [k]; j = k;} father [j] = closeset [j]; printf ("% d \ n", j + 1, closeset [j] + 1); // print the side used [j] = 1 ;; // merge vertex j into U for (k = 1; k <vcount; k ++)/* find smaller weights */if (! Used [k] & (G [j] [k] <lowcost [k]) {lowcost [k] = G [j] [k]; /* update the minimum weight */closeset [k] = j;/* record the new attachment point */} 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_vertexes; j ++) G [I] [j] = infinity; fr = fopen ("prim.txt", "r"); if (! Fr) {printf ("fopen failed \ n"); exit (1);} while (fscanf (fr, "% d", & I, & j, & weight )! = EOF) {G [I-1] [J-1] = weight; G [J-1] [I-1] = weight;} prim (G, max_vertexes, fatheer); return 0 ;} implementation of the storage in the form of an adjacent matrix:
<Pre name = "code" class = "html"> # include <stdio. h> # define n 6 # define MaxNum 10000/* defines a maximum integer * // * defines the adjacent matrix type */typedef int adjmatrix [n + 1] [n + 1]; /* unit 0 is useless */typedef struct {int fromvex, tovex; int weight;} Edge; typedef Edge * EdgeNode; int arcnum; /* Number of edges * // * establish the graph's Adjacent matrix */void CreatMatrix (adjmatrix GA) {int I, j, k, e; printf ("% d vertices \ n", n); for (I = 1; I <= n; I ++) {for (j = 1; j <= n; j ++) {if (I = j) {GA [I] [j] = 0;/* set the diagonal Value 0 */} else {GA [I] [j] = MaxNum; /* initialize the value at other locations to a maximum integer */} printf ("Enter the number of edges:"); scanf ("% d", & arcnum ); printf ("Enter the edge information in the form of start point, end point, and weight: \ n"); for (k = 1; k <= arcnum; k ++) {scanf ("% d, % d, % d", & I, & j, & e ); /* read edge information */GA [I] [j] = e; GA [j] [I] = e ;}} /* initialize the number of edge sets of the graph */void InitEdge (EdgeNode GE, int m) {int I; for (I = 1; I <= m; I ++) {GE [I]. weight = 0 ;}/ * generate a number group of edge Sets Based on the graph's Adjacent matrix */void GetEdgeSet (adjmatrix GA, EdgeNode GE) {int I, j, k = 1; f Or (I = 1; I <= n; I ++) {for (j = I + 1; j <= n; j ++) {if (GA [I] [j]! = 0 & GA [I] [j]! = MaxNum) {GE [k]. fromvex = I; GE [k]. tovex = j; GE [k]. weight = GA [I] [j]; k ++ ;}}}/* Number Groups of edge sets in ascending order */void SortEdge (EdgeNode GE, int m) {int I, j, k; Edge temp; for (I = 1; I <m; I ++) {k = I; for (j = I + 1; j <= m; j ++) {if (GE [k]. weight> GE [j]. weight) {k = j ;}} if (k! = I) {temp = GE [I]; GE [I] = GE [k]; GE [k] = temp ;}}} /* use the Prim algorithm to obtain the minimum spanning tree of the graph represented by the adjacent matrix from the initial vertex v */void Prim (adjmatrix GA, EdgeNode T) {int I, j, k, min, u, m, w; Edge temp;/* assign initial values to T, corresponding to the Edge from v1 to other vertices */k = 1; for (I = 1; I <= n; I ++) {if (I! = 1) {T [k]. fromvex = 1; T [k]. tovex = I; T [k]. weight = GA [1] [I]; k ++;}/* For n-1 cycles, obtain the k edge in the Minimum Spanning Tree */for (k = 1; k <n; k ++) {min = MaxNum; m = k; for (j = k; j <n; j ++) {if (T [j]. weight <min) {min = T [j]. weight; m = j;}/* adjust the shortest side to the K-1 subscript position */temp = T [k]; T [k] = T [m]; T [m] = temp;/* assign the vertex sequence number in the newly added minimal spanning tree T to j */j = T [k]. tovex;/* modify the related edge so that each vertex from T to T maintains a shortest edge so far */for (I = k + 1; I <n; I ++) {u = T [I]. tovex; w = GA [j] [u]; if (w <T [I]. weight) {T [I]. weight = w; T [I]. fromvex = j ;}}}/* each edge in the number of output edge sets */void OutEdge (EdgeNode GE, int e) {int I; printf ("Start Point, the smallest Spanning Tree output in the form of weights is \ n "); for (I = 1; I <= e; I ++) {printf (" % d, % d, % d \ n ", GE [I]. fromvex, GE [I]. tovex, GE [I]. weight) ;}} void main () {adjmatrix GA; Edge GE [n * (n-1)/2], T [n]; CreatMatrix (GA); InitEdge (GE, arcnum); GetEdgeSet (GA, GE); SortEdge (GE, arcnum); Prim (GA, T); printf ("\ n"); OutEdge (T, n-1 );} </pre> <br> <p> </p> <pre> </pre> <p> </p>

 

 

Related Article

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.