Kruskal algorithm for minimum spanning tree (Java version) __ algorithm

Source: Internet
Author: User

1 Figure data is as follows



Two Java code

Package leaning.graph;
Import java.util.ArrayList;

Import java.util.List;

Import Leaning.graph.entity.Edge; 
	
	* * * Kruskal minimum spanning tree */public class Kruskalminicostspanningtree {//counter private int counter = 0;
	
	Private int[] parent = new INT[9];
	
	Side set Private list<edge> edgeslist = new arraylist<edge> ();
	
	Maximum int integer, representing infinity private int max_value = Integer.max_value;

	Define node private String nodes[] = {"Vo", "v1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"}; 
	
	Define map variable private int map[][] = new INT[9][9];
		Public Kruskalminicostspanningtree () {this.initmap ();
	This.initedages (); }//Initialize map public void Initmap () {this.map[0] = new int[]{0, 10,max_value,max_value,max_value, 1
	  1,max_value,max_value,max_value};
	  THIS.MAP[1] = new int[]{, 0, 18,max_value,max_value,max_value, 16,max_value, 12};
THIS.MAP[2] = new Int[]{max_value,max_value, 0, 22,max_value,max_value,max_value,max_value, 8};	  THIS.MAP[3] = new Int[]{max_value,max_value, 0, 20,max_value,max_value, 16, 21};
	  THIS.MAP[4] = new Int[]{max_value,max_value,max_value, 0, 26,max_value, 7,max_value};
	  THIS.MAP[5] = new int[]{11,max_value,max_value,max_value, num, 0, 17,max_value,max_value};
	  THIS.MAP[6] = new Int[]{max_value, 16,max_value,max_value,max_value, 0, 19,max_value};
	  THIS.MAP[7] = new Int[]{max_value,max_value,max_value, 7,max_value, 0,max_value};
	
	THIS.MAP[8] = new Int[]{max_value, 8, 21,max_value,max_value,max_value,max_value, 0};
		}//initialization edges and rights of the list set public void Initedages () {Edge V0 = new Edge (4,7,7);
		Edge V1 = new Edge (2,8,8);
		Edge v2 = new Edge (0,1,10);
		Edge v3 = new Edge (0,5,11);
		Edge v4 = new Edge (1,8,12);
		Edge v5 = new Edge (3,7,16);
		Edge V6 = new Edge (1,6,16); Edge V7 = New Edge (5,6,17);
		Edge V8 = new Edge (1,2,18);
		Edge V9 = new Edge (6,7,19);
		Edge v10 = new Edge (3,4,20);
		Edge v11 = new Edge (3,8,21);
		Edge V12 = new Edge (2,3,22);
		Edge V13 = new Edge (3,6,24);
		
		Edge V14 = new Edge (4,5,26);
		This.edgesList.add (V0);
		This.edgesList.add (v1);
		This.edgesList.add (v2);
		This.edgesList.add (v3);
		This.edgesList.add (v4);
		This.edgesList.add (v5);
		This.edgesList.add (V6);
		This.edgesList.add (V7);
		This.edgesList.add (V8);
		This.edgesList.add (V9);
		This.edgesList.add (V10);
		This.edgesList.add (V11);
		This.edgesList.add (V12);
		This.edgesList.add (V13);
	This.edgesList.add (V14);
		}//Kruskal algorithm public void Kruskal () {int n =-1, m =-1, begin,end;
			for (int i=0 i < this.edgesList.size (); i++) {//traversal gets each edge begin = This.edgesList.get (i). Getbegin ();
			End = This.edgesList.get (i). Getend ();
			n = this.find (begin);
			m = This.find (end);
				if (n!=m) {//description no closed line this.counter++;
				This.parent[n] = m; System.out.println ("First" +this.countEr+ "Edge is: (" +this.nodes[this.edgeslist.get (i). Getbegin ()]+ "," +this.nodes[this.edgeslist.get (i). Getend ()]+ ") =" +
			This.edgesList.get (i). Getweight () + "");
		}} public int find (int index) {while (This.parent[index] > 0) {index = This.parent[index];
	} return index; public static void Main (string[] args) {Kruskalminicostspanningtree kruskalminicostspanningtree = new Kruskalmi
		Nicostspanningtree ();
		
	Kruskalminicostspanningtree.kruskal ();
 }

}

Package leaning.graph.entity;

*
 * * Kruskal-> minimum spanning Tree edge entity
 * *
/public class Edge {
	
   private int begin;
   private int end;
   private int weight;
   
   public Edge (int begin, int end, int weight) {
	    this.begin = begin;
		This.end = end;
		This.weight = weight;
   }
	
	public int Getbegin () {return
		begin;
	}
	
	public void SetBegin (int begin) {
		this.begin = begin;
	}
	
	public int getend () {return end
		;
	}
	
	public void setend (int end) {
		this.end = end;
	}
	
	public int getweight () {return
		weight;
	}
	
	public void setweight (int weight) {
		this.weight = weight;
	}
	   
   
   
}

3 The results of the operation are as follows



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.