创建图的边
// 表示图的边
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);
}
}