-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimsAlgo.java
More file actions
76 lines (65 loc) · 2.34 KB
/
Copy pathPrimsAlgo.java
File metadata and controls
76 lines (65 loc) · 2.34 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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
public class PrimsAlgo {
class Node {
int v, weight;
Node(int v, int weight) {
this.v = v;
this.weight = weight;
}
}
void primsMST(int N, ArrayList<ArrayList<Node>> adjList) {
// defining data structures
int[] key = new int[N];
int[] par = new int[N];
boolean[] mst = new boolean[N];
for (int i = 0; i < N; i++) par[i] = -1;
for (int i = 0; i < N; i++) key[i] = 10005;
key[0] = 0;
// creating min-heap to get min weighted node
PriorityQueue<Node> pQueue = new PriorityQueue<Node>(new Comparator<Node>() {
@Override
public int compare(Node node0, Node node1) {
if (node0.weight < node1.weight) return -1;
if (node0.weight > node1.weight) return 1;
return 0;
}
});
int mstCost = 0;
pQueue.add(new Node(0, 0));
for (int e = 0; e < N-1; e++) {
Node sNode = pQueue.poll();
int u = sNode.v;
mstCost += sNode.weight;
mst[u] = true;
// iterating over all incident vertices
for (Node node : adjList.get(u)) {
if (node.weight < key[node.v]) {
key[node.v] = node.weight;
par[node.v] = u;
pQueue.add(new Node(node.v, node.weight));
}
}
}
mstCost += pQueue.poll().weight;
System.out.println("MST Cost -> " + mstCost);
for (int p = 1; p < N; p++) {
System.out.println("u: "+par[p]+", v: "+p+", weight: "+key[p]);
}
}
public static void main(String[] args) {
PrimsAlgo pAlgo = new PrimsAlgo();
int V = 5;
ArrayList<ArrayList<Node>> adjList = new ArrayList<>();
for (int v = 0; v < V; v++) adjList.add(new ArrayList<Node>());
// building graph
adjList.get(0).add(pAlgo.new Node(1, 2));
adjList.get(0).add(pAlgo.new Node(3, 6));
adjList.get(1).add(pAlgo.new Node(2, 3));
adjList.get(1).add(pAlgo.new Node(3, 8));
adjList.get(1).add(pAlgo.new Node(4, 5));
adjList.get(2).add(pAlgo.new Node(4, 7));
pAlgo.primsMST(V, adjList);
}
}