一、节点
package weightGraph;
public class Vertex {
public char label;
public boolean isInTree;
public Vertex(char label) {
this.label = label;
this.isInTree = false;
}
}
二、边
package weightGraph;
public class Edge {
public int startVert;
public int endVert;
public int price;
public Edge(int startVert, int endVert, int price) {
this.startVert = startVert;
this.endVert = endVert;
this.price = price;
}
}
三、优先级队列
package weightGraph;
public class PriorityQueue {
private Edge[] edges;
private final int MAX_SIZE = 20;
private int size;
public PriorityQueue() {
edges = new Edge[MAX_SIZE];
size = 0;
}
public void insert(Edge edge) {
int i;
for (i = 0; i < size; i++) {
if (edge.price >= edges[i].price) {
break;
}
}
for (int j = size - 1; j >= i; j--) {
edges[j + 1] = edges[j];
}
edges[i] = edge;
size++;
}
public Edge removeMin() {
return edges[--size];
}
public void removeN(int n) {
for (int i = n; i < size; i++) {
edges[i] = edges[i + 1];
}
size--;
}
public Edge peekMin() {
return edges[size - 1];
}
public boolean isEmpty() {
return size == 0;
}
public int getSize() {
return size;
}
public Edge peekN(int n) {
return edges[n];
}
}
四、带权图
package weightGraph;
public class WGraph {
private Vertex[] vertexList;
private int[][] adjMat;
private int nVerts;
private final int MAX_VERTS = 20;
private PriorityQueue priorityQueue;
private int nTree;
private int currentVert;
public final int INF = 1000000;
public WGraph() {
vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
for (int i = 0; i < MAX_VERTS; i++) {
for (int j = 0; j < MAX_VERTS; j++) {
adjMat[i][j] = INF;
}
}
nVerts = 0;
priorityQueue = new PriorityQueue();
}
public void insert(char label) {
vertexList[nVerts++] = new Vertex(label);
}
public void addEdge(int start, int end, int price) {
adjMat[start][end] = price;
adjMat[end][start] = price;
}
public void displayVertex(int v) {
System.out.print(vertexList[v].label);
}
public void mstw() {
currentVert = 0;
while (nTree < nVerts - 1) {
vertexList[currentVert].isInTree = true;
nTree++;
for (int i = 0; i < nVerts; i++) {
if(i == currentVert) continue;
if(vertexList[i].isInTree) continue;
int price = adjMat[currentVert][i];
if(price == INF) continue;
priorityQueue.insert(new Edge(currentVert,i,price));
}
if (priorityQueue.getSize() == 0) {
System.out.println("当前顶点一个临界点也没有");
return;
}
for (int i = 0; i < priorityQueue.getSize(); i++) {
if (vertexList[priorityQueue.peekN(i).startVert].isInTree && vertexList[priorityQueue.peekN(i).endVert].isInTree) {
priorityQueue.removeN(i);
}
}
Edge minEdge = priorityQueue.removeMin();
int startV = minEdge.startVert;
currentVert = minEdge.endVert;
System.out.print(vertexList[startV].label);
System.out.print(vertexList[currentVert].label + " ");
}
for (int i = 0; i < nVerts; i++) {
vertexList[i].isInTree = false;
}
}
}
五、测试
package weightGraph;
public class WGraphTest {
public static void main(String[] args) {
WGraph wGraph = new WGraph();
wGraph.insert('A');
wGraph.insert('B');
wGraph.insert('C');
wGraph.insert('D');
wGraph.insert('E');
wGraph.insert('F');
wGraph.addEdge(0, 1, 6);
wGraph.addEdge(0, 3, 4);
wGraph.addEdge(1, 2, 10);
wGraph.addEdge(1, 3, 7);
wGraph.addEdge(1, 4, 7);
wGraph.addEdge(2, 3, 8);
wGraph.addEdge(2, 4, 5);
wGraph.addEdge(2, 5, 6);
wGraph.addEdge(3, 4, 12);
wGraph.addEdge(4, 5, 7);
wGraph.mstw();
}
}