Minimal spanning tree (cruise Karl algorithm)

Source: Internet
Author: User

Minimal spanning tree (cruise Karl algorithm)

/* Name: Copyright: Author: Date: 01-12-14 20: 17 Description, see a simple and interesting data structure -- Querying sets http://blog.csdn.net/qiaoruozhuo/article/details/39674991 */# include
 
  
# Include
  
   
# Define MAXN 1000 // maximum number of vertices # define MAX 20000 // maximum number of edges # define INFINITY 999999 // infinite int map [MAX] [MAX] = {0 }; // The adjacent matrix stores the Graph Information typedef struct EdgeNode {// triples edge table set int u, v; // arc tail and arc header int w; // weight, for non-network charts, you do not need to} EdgeNode; void CreatGraph (EdgeNode * E, int m); // create a triple edge table set map void CreatGraph_2 (EdgeNode * E, int m, int n); // create an adjacent matrix (Random Graph) int Locate (EdgeNode * E, int n, int u, int v); // judge u, whether v is the adjacent contact void PrintGraph (EdgeNode * E, int m); // output graph int cmp (const void * a, const void * B ); // The matching function int FindFatherAndReducePath (int father [], int pos) of the Quick Sort; // search for the parent and compress the path: after finding the parent, point all previous nodes in the path to the parent family int UnionBySize (int father [], int posI, int posJ); // calculate and merge by size: merge member posI and posJ to void KRSL (EdgeNode * E, int m, int n) of the same family; // find the Minimum Spanning Tree int main () using the KRSL (EdgeNode * E, int m, int n () {EdgeNode E [MAX]; int I, m, n; printf ("Enter the number of vertices:"); scanf ("% d", & n ); printf ("\ n enter the number of edges:"); scanf ("% d", & m); CreatGraph_2 (E, m, n ); // create a triple edge table set graph PrintGraph (E, m); // output graph qsort (E, m, sizeof (E [0]), cmp ); // PrintGraph (E, m) is sorted progressively by weight. // output graph KRSL (E, m, n ); // return 0;} void CreatGraph (EdgeNode * E, int m); // create a triple edge table set graph {int I; printf ("\ n please input edge information in a B c format: \ n"); for (I = 0; I
   
    
= % D \ t ", E [I]. u, E [I]. v, E [I]. w);} printf ("\ n");} int cmp (const void * a, const void * B) // complete the function {return (EdgeNode *) a)-> w> (EdgeNode *) B)-> w? 1:-1);} void KRSL (EdgeNode * E, int m, int n) // find the minimum spanning tree using the kriskar algorithm {int I, min, top = 1; int father [MAXN] = {0}; EdgeNode minTree [MAXN] = {0}; for (I = 0; I
    
     
= % D \ t ", minTree [I]. u, minTree [I]. v, minTree [I]. w); min + = minTree [I]. w;} printf ("\ n Minimum Spanning Tree Length (weight): % d \ n", min);} int FindFatherAndReducePath (int father [], int pos) // search for the Patriarch and compress the path: after finding the patriarch, point all the node nodes passing through to the patriarch {if (father [pos] <= 0) return pos; // if you are not a leader, after finding the leader, point all the nodes passing through to the leader return father [pos] = FindFatherAndReducePath (father, father [pos]);} int UnionBySize (int father [], int posI, int posJ) // calculate the sum by size: merge members posI and posJ into the same family {// first look for their parent family int fI = FindFatherAndReducePath (father, posI); int fJ = FindFatherAndReducePath (father, posJ ); if (fI = fJ) // if it is under the same clan, it does not need to be merged, that is, the merger fails to return 0; if (father [fI] <father [fJ]) {// if the leader fI is more powerful than fJ, that is, | fI |> | fJ |, then fI is the leader, modify father [fI] And father [fJ] father [fI] + = father [fJ]; father [fJ] = fI ;} else // otherwise, fJ becomes the parent {father [fJ] + = father [fI]; father [fI] = fJ;} return 1 ;}
    
   
  
 

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.