利用prim算法得到最小生成树MST

生成树来源于连通图

对于一个连通图而言,若存在子图包含原图全部顶点且边的条数为顶点数减1

那么这个子图就叫做原图的生成树

什么叫做最小生成树呢 ,就是说在生成树的前提下,其边上的权值之和最小的生成树就叫做最小生成树


如图所示 有连通图如下所示

其最小生成树为

prim算法的逻辑关系是这样的

基于现有的图去寻找当前与现有图有邻接关系的顶点中边最短的那一条

每加入一个点扩展一次现有图,直到所有的点全部都被访问

即可以得到最短生成树


以下为java代码实现:

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author zero
 *
 */
public class PrimMST {
	private List<Edge> edgeList = new ArrayList<Edge>();
	private List<Edge> edgeVisitedList = new ArrayList<Edge>();
	
	private List<Integer> pointVisitedList = new ArrayList<Integer>();
	private Boolean[] visitedStatus = {false,false,false,false,false,false,false,false};
	private int minWeight = 0;
	
	public void initEdgeList() {
		edgeList.add(new Edge(1, 2, 6));
		edgeList.add(new Edge(1, 3, 11));
		edgeList.add(new Edge(1, 4, 12));
		edgeList.add(new Edge(2, 3, 23));
		edgeList.add(new Edge(3, 4, 3));
		edgeList.add(new Edge(2, 5, 26));
		edgeList.add(new Edge(2, 6, 17));
		edgeList.add(new Edge(3, 6, 9));
		edgeList.add(new Edge(3, 7, 15));
		edgeList.add(new Edge(4, 7, 21));
		edgeList.add(new Edge(5, 6, 5)); 
		edgeList.add(new Edge(6, 7, 31));
		edgeList.add(new Edge(6, 8, 13));
		edgeList.add(new Edge(7, 8, 8));
	}
	
	public void setInitPoint(int point) {
		pointVisitedList.add(point);
		visitedStatus[point-1] = true; 
		primMST(pointVisitedList);
	}
	
	@SuppressWarnings("rawtypes")
	public void primMST(List pointVisitedList) {
		int currentMinWeight = 100;
		int minWeightNumIndex = -1;
		for(int i=0; i<pointVisitedList.size(); i++) {
			Point point = new Point((int) pointVisitedList.get(i));
			for(int j=0; j<edgeList.size(); j++) {
				Edge edge = edgeList.get(j);
				if(
					((edge.getStartPoint() == point.getIndex()) && (!pointVisitedList.contains(edge.getEndPoint()))&&(!visitedStatus[edge.getEndPoint()-1])) || 
					((edge.getEndPoint() == point.getIndex()) && (!pointVisitedList.contains(edge.getStartPoint()))&&(!visitedStatus[edge.getStartPoint()-1]))
				  ) {
					if(edge.getEdgeWeight() < currentMinWeight) {
						currentMinWeight = edge.getEdgeWeight();
						minWeightNumIndex = j;
					}
				}else {}
			}
		}
		
		System.out.println("minWeightNumIndex:" + minWeightNumIndex);
		
		if(minWeightNumIndex != -1) {
			Edge minEdge = edgeList.get(minWeightNumIndex);
			System.out.println("current Minium size edge : " + currentMinWeight);
			minWeight += currentMinWeight;
			if(!pointVisitedList.contains(minEdge.getEndPoint())) {
				pointVisitedList.add(minEdge.getEndPoint());
				System.out.println("find a new point : " + minEdge.getEndPoint());
				visitedStatus[minEdge.getEndPoint()-1] = true;
			}
			if(!pointVisitedList.contains(minEdge.getStartPoint())) {
				pointVisitedList.add(minEdge.getStartPoint());
				System.out.println("find a new point : " + minEdge.getStartPoint());
				visitedStatus[minEdge.getStartPoint()-1] = true;
			}
			
			edgeVisitedList.add(minEdge);
			edgeList.remove(minWeightNumIndex);
			
		}
		
		if(!allVisited()) {
			primMST(pointVisitedList);
		}
		
	}
	
	
	public Boolean allVisited() {
		Boolean result = true;
		for(int i=0; i<visitedStatus.length; i++) {
			if(!visitedStatus[i]) {
				result = false;
			}
		}
		return result;
	}
	
	public void outMST() {
		System.out.println("The edges in the MST are : ");
		for(int i=0; i<edgeVisitedList.size(); i++) {
			Edge edge = edgeVisitedList.get(i);
			System.out.println(edge.getStartPoint() + "  ------->  " + edge.getEndPoint());
		}
		System.out.println("The min weight is : " + minWeight);
	}
	
	public static void main(String[] args) {
		PrimMST pmst = new PrimMST();
		pmst.initEdgeList();
		pmst.setInitPoint(1);
		pmst.outMST();
	}
	
}


class Point {
	private int index;
	
	public Point(int index) {
		this.index = index;
	}

	public int getIndex() {
		return index;
	}
	

}

class Edge {
	private int startPoint;
	private int endPoint;
	private int edgeWeight;
	
	public Edge(int startPoint, int endPoint, int edgeWeight) {
		super();
		this.startPoint = startPoint;
		this.endPoint = endPoint;
		this.edgeWeight = edgeWeight;
	}
	
	public int getStartPoint() {
		return startPoint;
	}
	public int getEndPoint() {
		return endPoint;
	}
	public int getEdgeWeight() {
		return edgeWeight;
	}
	
}

写的不太好  感觉太冗余了  不过条理还是比较清晰的


输出结果为:

minWeightNumIndex:0
current Minium size edge : 6
find a new point : 2
minWeightNumIndex:0
current Minium size edge : 11
find a new point : 3
minWeightNumIndex:2
current Minium size edge : 3
find a new point : 4
minWeightNumIndex:4
current Minium size edge : 9
find a new point : 6
minWeightNumIndex:6
current Minium size edge : 5
find a new point : 5
minWeightNumIndex:7
current Minium size edge : 13
find a new point : 8
minWeightNumIndex:7
current Minium size edge : 8
find a new point : 7
The edges in the MST are :
1  ------->  2
1  ------->  3
3  ------->  4
3  ------->  6
5  ------->  6
6  ------->  8
7  ------->  8
The min weight is : 55






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值