prim算法

创建图的边

// 表示图的边
public class GraphEdge {
	// 该边连接的左顶点
	private GraphNode leftNode;
	// 该边连接的右顶点
	private GraphNode rightNode;

	// 边的权值
	private int weight;
	// 该边是否被访问
	private boolean isVisited;

	public GraphEdge(GraphNode leftNode, GraphNode rightNode, int weight) {
		this.leftNode = leftNode;
		this.rightNode = rightNode;
		this.weight = weight;
		isVisited = false;
	}

	public GraphNode getLeftNode() {
		return leftNode;
	}

	public GraphNode getRightNode() {
		return rightNode;
	}

	public int getWeight() {
		return weight;
	}

	public boolean isVisited() {
		return isVisited;
	}
	public void setVisited(boolean isVisited) {
		this.isVisited = isVisited;
	}
}

创建图的节点

//定义图的顶点
import java.util.List;
import java.util.ArrayList;

public class GraphNode {
	// 表示该顶点的数据
	private String data;
	// 表示该顶点的边集合
	private List<GraphEdge> nodeList = null;
	// 表示该顶点是否曾被访问
	private boolean isVisited;
	
	public GraphNode(String data) {
		this.data = data;
		isVisited = false;
		if(nodeList == null) {
			nodeList = new ArrayList<>();
		}
	}

	public boolean isVisited() {
		return isVisited;
	}

	public void setVisited(boolean isVisited) {
		this.isVisited = isVisited;
	}

	public String getData() {
		return data;
	}

	public List<GraphEdge> getEdgeList() {
		return nodeList;
	}
}

实现prim算法

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

public class MyGraph {
	static int sum = 0; // 最短路径总和
	public static void main(String[] args) {
		GraphNode A = new GraphNode("A");
		GraphNode B = new GraphNode("B");
		GraphNode C = new GraphNode("C");
		GraphNode D = new GraphNode("D");
		GraphNode E = new GraphNode("E");
		GraphEdge AB = new GraphEdge(A, B, 2);
		GraphEdge AC = new GraphEdge(A, C, 4);
		GraphEdge AD = new GraphEdge(A, D, 2);
		GraphEdge BC = new GraphEdge(B, C, 3);
		GraphEdge CD = new GraphEdge(C, D, 3);
		GraphEdge BE = new GraphEdge(B, E, 3);
		GraphEdge CE = new GraphEdge(C, E, 4);
		GraphEdge DE = new GraphEdge(D, E, 5);
		A.getEdgeList().add(AB);
		A.getEdgeList().add(AC);
		A.getEdgeList().add(AD);
		B.getEdgeList().add(AB);
		B.getEdgeList().add(BC);
		B.getEdgeList().add(BE);
		C.getEdgeList().add(AC);
		C.getEdgeList().add(BC);
		C.getEdgeList().add(CD);
		C.getEdgeList().add(CE);
		D.getEdgeList().add(AD);
		D.getEdgeList().add(CD);
		D.getEdgeList().add(DE);
		E.getEdgeList().add(BE);
		E.getEdgeList().add(CE);
		E.getEdgeList().add(DE);
		
		List<GraphNode> nodes = new ArrayList<>();
		A.setVisited(true);
		nodes.add(A);
		prim(nodes, 5); // prim算法
		System.out.println(sum);
	}
	
	public static void prim(List<GraphNode> nodes, int size) {
		if(nodes.size() == size) return;
		int x = -1;
		int y = -1;
		int tmp = Integer.MAX_VALUE;
		for(int i = 0; i < nodes.size(); i++) {
			List<GraphEdge> egs = nodes.get(i).getEdgeList();
			for(int j = 0; j < egs.size(); j++) {
				GraphEdge eg = egs.get(j);
				if(eg.isVisited() == true) continue;
				if(eg.getWeight() <= tmp && !eg.getRightNode().isVisited()) {
					tmp = eg.getWeight();
					x = i;
					y = j;
				}
			}
		}
		GraphEdge e = nodes.get(x).getEdgeList().get(y);
		sum += e.getWeight();
		e.setVisited(true);
		if(!e.getLeftNode().isVisited()) {
			nodes.add(e.getLeftNode());
			e.getLeftNode().setVisited(true);
		}
		if(!e.getRightNode().isVisited()) {
			nodes.add(e.getRightNode());
			e.getRightNode().setVisited(true);
		}
		prim(nodes, size);
	}
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值