-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimMSTUsingAdj.java
More file actions
145 lines (128 loc) · 4.2 KB
/
PrimMSTUsingAdj.java
File metadata and controls
145 lines (128 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package Graph;
import java.util.*;
/**
* Generic PriorityQueue is not going to work because you will change the elements while traversing the queue.
* http://stackoverflow.com/questions/1871253/updating-java-priorityqueue-when-its-elements-change-priority
* @author Huiqiong Gu
*
*/
public class PrimMSTUsingAdj {
private boolean[] visited;
private int[] pathTo;
private double[] distTo;
private PrimMinHeap heap;
public PrimMSTUsingAdj(WeightedEdgeGraph g) {
visited = new boolean[g.V()];
pathTo = new int[g.V()];
distTo = new double[g.V()];
PrimNode[] nodes = new PrimNode[g.V()];
for (int v = 1; v < g.V(); v++) {
distTo[v] = Double.MAX_VALUE;
nodes[v] = new PrimNode(v, distTo[v]);
}
distTo[0] = 0.0;
nodes[0] = new PrimNode(0, distTo[0]);
heap = new PrimMinHeap(nodes);
mstUsingAdj(g);
}
public void mstUsingAdj(WeightedEdgeGraph g) {
while (!heap.isEmpty()) {
PrimNode front = heap.extractMin();
visited[front.v] = true;
for (WeightedEdge e : g.adj(front.v)) {
int w = e.other(front.v);
if (visited[w]) continue;
if (heap.isInMinHeap(w) && e.weight() < distTo[w]) {
distTo[w] = e.weight();
pathTo[w] = front.v;
heap.decreaseWeight(w, e.weight());
}
}
}
}
public void printMST() {
double totalWeight = 0;
for (int v = 1; v < pathTo.length; v++) {
totalWeight += distTo[v];
System.out.format("%d - %d: %.0f%n", pathTo[v], v, distTo[v]);
}
System.out.format("Total Weight in MST is: %.0f%n", totalWeight);
}
}
class PrimNode {
int v;
double weight;
PrimNode(int v, double weight) {
this.v = v;
this.weight = weight;
}
}
class PrimMinHeap {
int size;
int capacity;
PrimNode[] array;
int[] position; //position[vertexNumber] = position in the array;
public PrimMinHeap(PrimNode[] nodes) {
this.size = this.capacity = nodes.length;
array = nodes;
position = new int[size];
for (int i = 0; i < size; i++) {
position[array[i].v] = i;
}
buildHeap();
}
public void buildHeap() {
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(i);
}
}
public PrimNode extractMin() {
PrimNode smallest = array[0], last = array[size - 1];
swapNodes(0, size - 1);
position[smallest.v] = size - 1;
position[last.v] = 0;
size--;
heapify(0);
return smallest;
}
public void decreaseWeight(int vertexNumber, double newWeight) {
int index = position[vertexNumber];
PrimNode node = array[index];
node.weight = newWeight;
while(index > 0 && array[index].weight < array[(index - 1) / 2].weight) {
PrimNode child = array[index], parent = array[(index - 1) / 2];
swapNodes(index, (index - 1) / 2);
position[child.v] = (index - 1) / 2;
position[parent.v] = index;
index = (index - 1) / 2;
}
}
public boolean isInMinHeap(int vertex) {
return position[vertex] < size;
}
private void heapify(int i) {
int left = 2 * i + 1, right = 2 * i + 2;
int smallestIndex = i;
if (left < size && array[left].weight < array[smallestIndex].weight) {
smallestIndex = left;
}
if (right < size && array[right].weight < array[smallestIndex].weight) {
smallestIndex = right;
}
if (smallestIndex != i) {
PrimNode child = array[smallestIndex], parent = array[i];
swapNodes(i, smallestIndex);
position[child.v] = i;
position[parent.v] = smallestIndex;
heapify(smallestIndex);
}
}
public boolean isEmpty() {
return size == 0;
}
private void swapNodes(int i, int j) {
PrimNode temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}