这是用Eclipse做的Prime算法编程实现
package Prime; public class PrimeSF { public static void main(String[] args) { float s=Float.MAX_VALUE-1000; float c[][] = {{s,s,s,s,s,s,s}, {s,s,6,1,5,s,s}, {s,6,s,5,s,3,s}, {s,1,5,s,5,6,4}, {s,5,s,5,s,s,2}, {s,s,s,6,s,s,6}, {s,s,s,4,2,6,s}}; prim(7,c); } /** * 解释文档 * c[u][v]表示两者之间的权值 * closest[j]是邻接结点,C[u][v],s=float.MAX_VALUE * 0 1 2 3 4 5 6 * ---------------- * 0 * 1 s 6 1 5 s s * 2 6 s 5 s 3 s * 3 1 5 s 5 6 4 * 4 5 s 5 s s 2 * 5 s s 6 s s 6 * 6 s s 4 2 6 s * ---------------- * */ /* * @param n * @param c */ public static void prim(int n,float [][]c){ //prime算法 float []lowcost = new float [n+1]; //最小代价 lowcost[n]=Float.MAX_VALUE-1000; int []closest = new int [n+1]; closest[n]=(int)lowcost[n]; //邻接结点的数组 boolean []s = new boolean [n+1]; //判断在不在s中,即有没有在已连接的图中 s[1]= true; for(int i=2;i<=n-1;i++){ lowcost[i]=c[1][i]; closest[i]=1; s[i]=false; } for(int i=1;i<n-1;i++){ float min = Float.MAX_VALUE; int j=1; for(int k=2;k<=n-1;k++){ if((lowcost[k]<min)&&(!s[k])){ min = lowcost[k]; j=k; } } System.out.println(j+", "+closest[j]); s[j]=true; for(int k=2;k<=n-1;k++){ if((c[j][k]<lowcost[k])&&(!s[k])){ lowcost[k]=c[j][k]; closest[k]=j; } } } } }