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≪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);
}
}