Java data structure and algorithm------diagram (minimum spanning tree Kruskal)

Source: Internet
Author: User

1  PackageiYou.neugle.graph;2 3 ImportJava.util.Set;4 ImportJava.util.TreeSet;5 6 //The code to create the diagram procedure is in the blog post of the diagram, which is used directly here7  Public classKruskal {8     PrivateMYGRAPH1 graph;9     Private int[] A;//using arrays (storing the predecessor nodes)Ten     PrivateSet<edge> Edgeset =NewTreeset<> ();//The collection of edges is sorted in ascending order of W One  A     classEdgeImplementsComparable<object> { -          Public intStart//Start Side -          Public intEnd//End Edge the          Public intW//Weighted Value -  -         //sort the collection of edges in ascending order of W - @Override +          Public intcompareTo (Object o) { -             if(OinstanceofEdge) { +Edge Edge =(Edge) o; A                 if( This. W >=edge.w) { at                     return1; -}Else { -                     return-1; -                 } -             } -             return0; in         } -     } to  +     //and check Set -     //Initialize each node first the     Private voidMakeSet (intN) { *          for(inti = 0; I < n; i++) { $A[i] =i;Panax Notoginseng         } -     } the  +     //Find root node A     Private intFind (intN) { the         if(A[n] = =N) { +             returnN; -}Else { $             returnFind (A[n]); $         } -     } -  the     //Merging Nodes -     Private voidUnionset (intXinty) {Wuyi         if(A[x]! =A[y]) { thea[ This. Find (y)] = This. Find (x); -         } Wu     } -  About      PublicKruskal (MyGraph1 graph) { $          This. Graph =graph; -A =New int[ This. Graph.getgraph (). Maxnum]; -     } -  A     //initializing the Edgeset collection +     Private voidInit () { the         //If the graph is not -         if( This. Graph.getgraph (). Type = = 0) { $              for(inti = 0; I < This. Graph.getgraph (). Maxnum; i++) { the                  for(intj = 0; J < I; J + +) { the Function (J, i); the                 } the             } -         } in         //if there is a direction graph the         Else { the              for(inti = 0; I < This. Graph.getgraph (). Maxnum; i++) { About                  for(intj = 0; J < This. Graph.getgraph (). Maxnum; J + +) { the Function (i, j); the                 } the             } +         } -     } the Bayi     //function Function the     Private voidFunction (intIintj) { the         intW = This. Graph.getgraph (). Edge[i][j]; -         if(W! = 0) { -Edge Edge =NewEdge (); theEdge.start =i; theEdge.end =J; theEDGE.W =W; the              This. Edgeset.add (Edge); -         } the     } the  the      Public voidKruskalcore () {94          This. Init (); the         intMaxnum = This. Graph.getgraph (). Maxnum; the         //Initialize a the          This. MakeSet (maxnum);98edge[] Edgearr = This. Edgeset.toarray (Newedge[] {}); About         intsum = edgearr[0].W; -         //Merge two nodes of an edge101          This. Unionset (Edgearr[0].start, edgearr[0].end);102SYSTEM.OUT.PRINTLN ("minimum spanning tree is--------");103 System.out104. println ((Edgearr[0].start + 1) + "+" + (Edgearr[0].end + 1)); the         //determine if the edge generates a loop by checking the set106         intn = 1;107          for(inti = 1; I < Edgearr.length && N < maxnum; i++) {108             if( This. Find (edgearr[i].start)! = This. Find (Edgearr[i].end)) {109                  This. Unionset (Edgearr[i].start, edgearr[i].end); theSystem.out.println ((Edgearr[i].start + 1) + "-"111+ (Edgearr[i].end + 1)); theSum + =EDGEARR[I].W;113             } then++; the         } theSystem.out.println ("----------------");117System.out.println ("The minimum spanning tree weight is:" +sum);118     }119  -      Public Static voidMain (string[] args) {121MYGRAPH1 graph =NewMYGRAPH1 (5, 0);122Graph. Createmaxtrixgraph (1, 2, 2);123Graph. Createmaxtrixgraph (1, 3, 5);124Graph. Createmaxtrixgraph (1, 5, 3); theGraph. Createmaxtrixgraph (2, 4, 4);126Graph. Createmaxtrixgraph (3, 5, 5);127Graph. Createmaxtrixgraph (4, 5, 2); - graph. Outputmaxtrixgraph ();129Kruskal Kruskal =NewKruskal (graph); the Kruskal. Kruskalcore ();131     } the}
  1 2 3 4 5 1 0 2 5 0 3 2 2 0 0 4 0 3 5 0 0 0 5 4 0 4 0 0 2 5 3 0 5 2 0 The minimum spanning tree is--------1->24->51->51->3---------- The weight of the------minimum spanning tree is: 12

Java data structure and algorithm------diagram (minimum spanning tree Kruskal)

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.