Prim minimum spanning tree algorithm and Java implementation of the source code __ algorithm

Source: Internet
Author: User
Tags new set
1.Prim algorithm Core ideaPrim algorithm is also a typical example of greedy algorithm, somewhat similar to the Dijkstra algorithm. Core idea: Divides the point into two dials, has joined the smallest spanning tree, has not joined, finds the point which is not joined the nearest set of distances, adds the point, modifies the distance from the other point to the collection, until all nodes are added to the minimum spanning tree.
2.java Source Code
Import java.util.*;

	public class Prim {static int MAX = Integer.max_value;
				public static void Main (string[] args) {int[][] map = new int[][] {0, max, Max, Max, one, Max, Max, Max}, {Ten, 0, Max, Max, Max, Max, Max, {max, Maxie, 0, Max, Max, Maxie, Max, 8}, {max, max, 0, MA X, Max, N, {max, max, Max, 0, Max, 7, Max}, {one, Max, MAX, Max, num, 0, x, max, Max}, {max  , Max, Max, Max, {MAX, Max, Max, 7, max, 0, Max}, {Max, A, 8, N, Max, Max, Max,
		MAX, 0}};
	Prim (map, map.length); } public static void Prim (int[][] graph, int n) {char[] c = new char[]{' A ', ' B ', ' C ', ' D ', ' e ', ' F ', ' G ', ' E        
	        ', ' F '};  int[] lowcost = new Int[n]; To the minimum weight of the new set int[] mid= new int[n];//Access precursor node list<character> list=new arraylist<character> (
	        )//used to store the order of joining nodes int I, J, Min, minid, sum = 0; Initializes the auxiliary array for (I=1;I&Lt;n;i++) {Lowcost[i]=graph[0][i];
	        mid[i]=0;
            } list.add (C[0]);
	        	 Altogether need to join N-1 point for (i=1;i<n;i++) {Min=max;
	        	 Minid=0;
	        		 Find the closest point for the collection for (j=1;j<n;j++) {if (lowcost[j]!=0&&lowcost[j]<min) each time
	        			 {MIN=LOWCOST[J];
	        		 Minid=j;
	        	 } if (minid==0) return;
	        	 List.add (C[minid]);
	        	 lowcost[minid]=0;
	        	 Sum+=min;
	        	 System.out.println (C[mid[minid]] + "to" + C[minid] + "Weight:" + min); After joining the point, update the distance for the other point to the collection for (j=1;j<n;j++) {if lowcost[j]!=0&&lowcost[j]>graph[
	        			 Minid][j]) {lowcost[j]=graph[minid][j];
	        		 Mid[j]=minid;
	        
	    }} System.out.println ("sum:" + sum);
 }
	    
}


Related Article

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.