Kruskal algorithm of minimum spanning tree

Source: Internet
Author: User

#include "iostream" #include "algorithm" using namespace std;struct edge {int begin;//edge starting point int end;//edge end point int weight;// The weight of the edge};//is used to sort all edges by the size of the weights bool Comp (const edge& E1, const edge& E2) {return e1.weight < e2.weight;} /* In each connected block, a node is used to represent the other nodes in the block, so if the two nodes represent the same node, then the two nodes are in the same connected block, so if the edges of the two nodes are added to the minimum spanning tree, the loop */int find (int e[], int n  ) {while (E[n] > 0) {n = e[n]; } return n;} E[] is based on the weight of the size from small to large order the void fun (Edge e[], int n) {int edge[20];//Save the representative node for each node for (int i = 0; i <; i++) Edge[i] =  0;    for (int i = 0; i < n; i++) {int begin = E[i].begin;    int end = E[i].end; int n = find (edge, begin);  n is the node to begin the representation of node int m = Find (edge, end); M is the node end's representative node if (n! = m) {///two represents the node is not the same, the description does not form a loop edge[n] = m;//Modify Node N representative node cout << begin << ""    << end << "" << e[i].weight << Endl;  }}}int Main () {Edge edges[15];  Test Diagram Edges[0].begin = 4, Edges[0].end = 5, edges[0].weight = 26; Edges[1].begin = 3, Edges[1].end = 6, edges[1].weight = 24;  Edges[2].begin = 2, Edges[2].end = 3, edges[2].weight = 22;  Edges[3].begin = 3, Edges[3].end = 8, edges[3].weight = 21;  Edges[4].begin = 3, Edges[4].end = 4, edges[4].weight = 20;  Edges[5].begin = 6, Edges[5].end = 7, edges[5].weight = 19;  Edges[6].begin = 1, edges[6].end = 2, edges[6].weight = 18;  Edges[7].begin = 5, Edges[7].end = 6, edges[7].weight = 17;  Edges[8].begin = 1, edges[8].end = 6, edges[8].weight = 16;  Edges[9].begin = 3, Edges[9].end = 7, edges[9].weight = 16;  Edges[10].begin = 1, edges[10].end = 8, edges[10].weight = 12;  Edges[11].begin = 0, Edges[11].end = 5, edges[11].weight = 11;  Edges[12].begin = 0, Edges[12].end = 1, edges[12].weight = 10;  Edges[13].begin = 2, Edges[13].end = 8, edges[13].weight = 8;  Edges[14].begin = 4, Edges[14].end = 7, edges[14].weight = 7; Sort (edges, edges + +, comp);  Sort by the weight size of the edges fun (edges,15); return 0;}

Kruskal algorithm of 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.