-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimMSTUsingMatrix.java
More file actions
56 lines (49 loc) · 1.61 KB
/
PrimMSTUsingMatrix.java
File metadata and controls
56 lines (49 loc) · 1.61 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
package Graph;
public class PrimMSTUsingMatrix {
private boolean[] visited;
private double[] distTo;
private int[] pathTo;
private int numberOfVerticesInMST;
public PrimMSTUsingMatrix(WeightedEdgeGraph g) {
numberOfVerticesInMST = g.V() - 1;
visited = new boolean[g.V()];
distTo = new double[g.V()];
pathTo = new int[g.V()];
for (int v = 0; v < g.V(); v++) {
distTo[v] = Double.MAX_VALUE;
}
distTo[0] = 0.0;
mstUsingMatrix(g);
}
public void mstUsingMatrix(WeightedEdgeGraph g) {
for (int count = 0; count < numberOfVerticesInMST; count++) {
int v = getMinWeightIndex(g.V());
visited[v] = true;
for (int w = 0; w < g.V(); w++) {
if (!visited[w] && g.matrix()[v][w] < distTo[w]) {
pathTo[w] = v;
distTo[w] = g.matrix()[v][w];
}
}
}
}
public void printMST() {
double totalWeight = 0;
for (int v = 1; v <= numberOfVerticesInMST; 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);
}
private int getMinWeightIndex(int n) {
int index = -1;
double minWeight = Double.MAX_VALUE;
for (int i = 0; i < n; i++) {
if (!visited[i] && distTo[i] < minWeight) {
index = i;
minWeight = distTo[i];
}
}
return index;
}
}