最小生成树:Prim算法(两种方法)(java)

[img]http://dl.iteye.com/upload/attachment/0073/0811/1d623560-baf1-3137-9a07-3f84f61e8c74.jpg[/img]
import java.util.Scanner;
方法一:时间复杂度O(n^3)
class Edge {
/**
* 边的起点
*/
char vexa;
/**
* 边的终点
*/
char vexb;
/**
* 边的权植
*/
int weight;
Edge(char vexa, char vexb, int weight) {
this.vexa = vexa;
this.vexb = vexb;
this.weight = weight;
}
}
public class MST {
int n;
int m;
Edge[] e ;

public MST(int n,int m,Edge[] e){
this.n=n;
this.m=m;
this.e=e;
}


/**
* w函数
* @param x 起点序号
* @param y 终点序号
* @return 返回起点序号为x,终点序号为y的边的权植。如果没有这条边就返回无穷大
*/
private int w(int x, int y) {
char from = (char) (x + 97);
char to = (char) (y + 97);
for (int i = 0; i < m; i++) {
if (e[i].vexa == from && e[i].vexb == to) {
return e[i].weight;
}
if (e[i].vexa == to && e[i].vexb == from) {
return e[i].weight;
}
}
return Integer.MAX_VALUE; // 用Integer.MAX_VALUE代表无穷大
}


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();//图的顶点数
int m = sc.nextInt(); //图的边数

if (n == 0 && m == 0)
break;
Edge[] e=new Edge[m];

for (int i = 0; i < m; ++i) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
e[i]=new Edge((char)(u+97),(char)(v+97),w);
}
MST mst=new MST(n,m,e);

Edge[] ee =mst.prime(); //存放最小生成树的n-1条边

for (int i = 0; i < n - 1; ++i) {
System.out.println(ee[i].vexa + " " + ee[i].vexb + " "
+ ee[i].weight);
}
}
}


public Edge[] prime(){
Edge[] e=new Edge[n];//存放最小生成树的n-1条边
// 表示已经加入最小生成树(mst)的结点, 数组元素从0到n-1分别对应结点a到(char)(n-1+97)
// 如果vex_mst[i]=0,表示对应结点没有加入到mst
// 如果vex_mst[i]=1,表示对应结点已经加入到mst
int[] vex_mst = new int[n];
for (int i = 0; i < n; i++)
// 初始化
vex_mst[i] = 0;
vex_mst[0] = 1; // 设置初始结点为a
// 将n-1条边加入到最小生成树
for (int i = 0; i < n-1; i++) {
// 加入一条边。
// 这条边的两个结点一个在mst中,而另一个不在mst中而且具有最小权植
int add_vex = 0; // 选中的结点
int min_weight = Integer.MAX_VALUE; // 最小权植,初始值为Integer.MAX_VALUE
Edge adde = new Edge(' ', ' ', 0);
for (int j = 0; j < n; j++)
if (vex_mst[j] == 1) { // j是mst中的结点
for (int k = 0; k < n; k++) {
if (vex_mst[k] == 0 && w(j, k) < min_weight) {
add_vex = k;
min_weight = w(j, k);
adde.vexa = (char) (j + 97);
adde.vexb = (char) (k + 97);
adde.weight = w(j,k);
}
}
}
vex_mst[add_vex] = 1; // 将选择的结点加入mst
e[i]=adde;
}
return e;
}
}


运行:
C:\aaa>java MST
5 8
0 1 2
1 4 9
4 3 7
3 0 10
0 2 12
2 4 3
1 2 8
2 3 6

a b 2
b c 8
c e 3
c d 6

方法二:时间复杂度O(n^2)

import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Arrays;


public class PrimTest{

private int[][] arr;//邻接矩阵
private boolean flag[]; //用来标记节点i是否已加入到MST
private int n; //顶点数
private int sum;//最小权值和
static final int maxInt = Integer.MAX_VALUE;

public PrimTest(int[][] arr,int n){
this.arr=arr;
this.n=n;
flag=new boolean[n];
}

public static void main(String[] args) {
Scanner s = new Scanner(new BufferedInputStream(System.in));

int n = 7;
int arr[][] =
{{maxInt,28, maxInt,maxInt,maxInt, 10, maxInt},
{28, maxInt,16, maxInt,maxInt, maxInt,14 },
{maxInt,16, maxInt,12, maxInt, maxInt,maxInt},
{maxInt,maxInt,12, maxInt,22, maxInt,18 },
{maxInt,maxInt,maxInt,22, maxInt, 25, 24 },
{10, maxInt,maxInt,maxInt,25, maxInt,maxInt},
{maxInt,14, maxInt,18, 24, maxInt,maxInt}};
System.out.println(new PrimTest(arr,n).prim());

}


public int prim(){
sum = 0;
flag[0] = true; //选取第一个节点
int mst[]=new int[n];//存储最小权值边的起点
Arrays.fill(mst,0);//最小权值边的起点默认为0



for(int k=1; k<n; k++){ //循环n-1次
int min = maxInt,min_i = 0;
for(int i=0; i<n; i++){//选一条权值最小的。
if( !flag[i] && arr[0][i] < min){
min = arr[0][i];
min_i = i;
}
}

flag[min_i] = true; //加入
System.out.print("边"+mst[min_i]+"-"+min_i);

for(int i=0; i<n; i++){ //更新
if( !flag[i] && arr[0][i] > arr[min_i][i]){//若同一个未加入点与多个已加入点相连接,取权值较小的。
arr[0][i] = arr[min_i][i];
mst[i] = min_i;//更新最小权值边的起点
}


}
System.out.println("--"+arr[0][min_i]);
sum += arr[0][min_i];//加上权值
}


return sum;

}

}


运行:
边0-5--10
边5-4--25
边4-3--22
边3-2--12
边2-1--16
边1-6--14
99

下载源码:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值