Kruskal minimum Spanning tree algorithm

Source: Internet
Author: User

    1. All edges are sorted by weight non-descending;
    2. Select the edge of the minimum weight to determine whether it forms a loop in the current spanning tree. If the loop is not formed, the edge is added to the tree, otherwise it is discarded.
    3. Repeat step 2 until you have a v–1 edge in the spanning tree.
  1. http://blog.jobbole.com/83939/
  2. Using system;using system.collections.generic;using System.Linq;      Namespace graphalgorithmtesting{class Program {static void Main (string[] args) {graph G = new Graph (9);      G.addedge (0, 1, 4);      G.addedge (0, 7, 8);      G.addedge (1, 2, 8);      G.addedge (1, 7, 11);      G.addedge (2, 3, 7);      G.addedge (2, 5, 4);      G.addedge (8, 2, 2);      G.addedge (3, 4, 9);      G.addedge (3, 5, 14);      G.addedge (5, 4, 10);      G.addedge (6, 5, 2);      G.addedge (8, 6, 6);      G.addedge (7, 6, 1);       G.addedge (7, 8, 7);      Console.WriteLine ();      Console.WriteLine ("Graph Vertex Count: {0}", G.vertexcount);      Console.WriteLine ("Graph Edge Count: {0}", G.edgecount);       Console.WriteLine ();      Console.WriteLine ("Was there cycle in graph: {0}", G.hascycle ());       Console.WriteLine ();      edge[] mst = G.kruskal ();      Console.WriteLine ("MST Edges:");      foreach (var edge in MST) {Console.WriteLine ("\t{0}", Edge);  } console.readkey ();  } class Edge {public edge (int begin, int end, int. weight) {this.        begin = BEGIN; This.        end = END; This.      Weight = Weight;      } public int Begin {get; private set;}      public int End {get; private set;}       public int Weight {get; private set;} public override string ToString () {return string.      Format ("begin[{0}], end[{1}], weight[{2}]", Begin, End, Weight);      }} class Subset {public int Parent {get; set;}    public int Rank {get; set;}  } class Graph {Private Dictionary<int, list<edge>> _adjacentedges = new Dictionary<int,       List<edge>> (); Public Graph (Int. vertexcount) {this.      Vertexcount = Vertexcount;       } public int Vertexcount {get; private set;}       Public ienumerable<int> Vertices {get {return _adjacentedges.keys;}} Public ienumerable<edge> Edges {get {return} _adjacentedges.values.selectmany (e = e); }} public int Edgecount {get {return this. Edges.count ();        }} public void Addedge (int begin, int end, int weight) {if (!_adjacentedges.containskey (begin))          {var edges = new List<edge> ();        _adjacentedges.add (begin, edges); } _adjacentedges[begin].      ADD (new Edge (begin, end, weight)); } private int Find (subset[] subsets, int i) {//Find root and make root as parent of I (Path Compressio N) if (Subsets[i]. Parent = i) subsets[i]. Parent = Find (subsets, Subsets[i].         Parent); Return subsets[i].      Parent;        } private void Union (subset[] subsets, int x, int y) {int xroot = Find (subsets, X);         int yroot = Find (subsets, y); Attach smaller rank tree under root of high rank tree//(Union by rank) if (Subsets[xroot]. Rank < Subsets[yroot]. Rank) Subsets[xroot].  Parent = Yroot;      else if (Subsets[xroot]. Rank > Subsets[yroot]. Rank) Subsets[yroot].         Parent = Xroot; If ranks is same, then make one as root and increment//it rank by one else {subsets[ Yroot].          Parent = Xroot; Subsets[xroot].        rank++;        }} public bool Hascycle () {subset[] subsets = new Subset[vertexcount]; for (int i = 0; i < subsets. Length;          i++) {Subsets[i] = new subset (); Subsets[i].          Parent = i; Subsets[i].        Rank = 0;  }//Iterate through all edges of graph, find subset of both//vertices of every edge, if both subsets is        Same,//Then there are cycle in graph. foreach (var edge in this.) Edges) {int x = Find (subsets, Edge.          Begin); int y = Find (subsets, Edge.           END);          if (x = = y) {return true;        } Union (subsets, X, y);      } return false;    }   Public edge[] Kruskal () {//This would store the resultant MST edge[] mst = new edge[vertexcount-1         ]; Step 1:sort all the edges in non-decreasing order of their weight//If We is not allowed to change the given Graph, we can create a copy of//array of edges var sortededges = this.        Edges.orderby (t = t.weight);         var enumerator = Sortededges.getenumerator (); Allocate memory for creating v ssubsets//Create V subsets with single elements subset[] subsets = new S        Ubset[vertexcount]; for (int i = 0; i < subsets. Length;          i++) {Subsets[i] = new subset (); Subsets[i].          Parent = i; Subsets[i].        Rank = 0;        }//number of edges to be taken are equal to V-1 int e = 0; while (E < VertexCount-1) {//Step 2:pick the smallest edge.          and increment the index//for next iteration Edge Nextedge; if (Enumerator. MoveNext ()) {Nextedge = enumerator.             Current;            int x = Find (subsets, Nextedge.begin);             int y = Find (subsets, Nextedge.end); If including this edge does ' t cause cycle, include it//in result and increment the index of result for NEX              T edge if (x! = y) {mst[e++] = Nextedge;            Union (subsets, X, y); } else {//else discard the Nextedge}}} return M      Qty }    }  }}

      

Kruskal minimum Spanning tree algorithm

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.