-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrim_MST.java
More file actions
107 lines (90 loc) · 3.02 KB
/
Prim_MST.java
File metadata and controls
107 lines (90 loc) · 3.02 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
package com.dsj.gp;
import com.dsj.graphs.Graph_Creation_Adj_List;
import com.dsj.graphs.WeightedNode;
/**
* IMPORTANT: This algorithm is not optimized. It is an implementation of Prim's
* algorithm in a simple manner just to illustrate its working.
*/
public class Prim_MST {
final static int MAX_VALUE = Integer.MAX_VALUE;
Graph_Creation_Adj_List graph_to_be_traversed;
int[] weight;
int[] parent;
boolean[] arrOfVisitedNodes;
int numOfVertices;
public Prim_MST(Graph_Creation_Adj_List graph_to_be_traversed) {
this.graph_to_be_traversed = graph_to_be_traversed;
numOfVertices = graph_to_be_traversed.numberOfVertices;
weight = new int[numOfVertices];
parent = new int[numOfVertices];
arrOfVisitedNodes = new boolean[numOfVertices];
mst();
}
private void mst() {
System.out.println("BEGINING PRIM'S MST...");
// Initialize weights: Put MAX value at all indices.
for (int i = 0; i < numOfVertices; i++) {
weight[i] = MAX_VALUE;
}
final int[] indexOfMinWeight = { 0 };
while (!areAllVisited()) {
graph_to_be_traversed.adjList[indexOfMinWeight[0]].forEach(connectedNode -> {
int arrIndex = graph_to_be_traversed.getIndexForThis(connectedNode.getVertexId());
// Check if current node is already visited or has weight less
// than that in weight array.
if (isValidOperation(arrIndex, connectedNode)) {
weight[arrIndex] = connectedNode.getEdgeWeight();
parent[arrIndex] = indexOfMinWeight[0];
}
});
// Mark this node as visited
arrOfVisitedNodes[indexOfMinWeight[0]] = true;
// Get next min
indexOfMinWeight[0] = getIndexOfMinItem();
}
printMST();
}
private void printMST() {
for (int i = 1; i < numOfVertices; i++) {
System.out.println(
graph_to_be_traversed.arrIndexToVertexMap.get(parent[i]) + "====" + graph_to_be_traversed.arrIndexToVertexMap.get(i));
}
}
/**
* @param arrIndex
* index of current neighbor
* @param connectedNode
* Weight-object containing representing current vertex
* @return boolean variable based on whether this node has been visited and
* has weight lesser than in the weight array.
*/
private boolean isValidOperation(int arrIndex, WeightedNode connectedNode) {
return !arrOfVisitedNodes[arrIndex] && weight[arrIndex] > connectedNode.getEdgeWeight();
}
/**
* @return the index of the item which has minimum value in the weight array
* with the condition that it has not been visited.
*/
private int getIndexOfMinItem() {
int min = weight[0];
int minIndex = 0;
for (int i = 1; i < numOfVertices; i++) {
if (min > weight[i] && !arrOfVisitedNodes[i]) {
min = weight[i];
minIndex = i;
}
}
return minIndex;
}
/**
* @return boolean variable based on whether all vertices have been visited.
*/
private boolean areAllVisited() {
for (int i = 1; i < numOfVertices; i++) {
if (!arrOfVisitedNodes[i]) {
return false;
}
}
return true;
}
}