-
Notifications
You must be signed in to change notification settings - Fork 88
/
PrimsAlgorithm.java
46 lines (36 loc) · 1.1 KB
/
PrimsAlgorithm.java
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
import java.util.ArrayList;
import java.util.PriorityQueue;
class Pair {
int node;
int distance;
Pair(int distance, int node) {
this.node = node;
this.distance = distance;
}
}
class PrimsAlgorithm {
static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj) {
PriorityQueue<Pair> pq = new PriorityQueue<>((x, y) -> x.distance - y.distance);
int[] vis = new int[V];
pq.add(new Pair(0, 0));
int sum = 0;
while (pq.size() > 0) {
int wt = pq.peek().distance;
int node = pq.peek().node;
pq.remove();
if (vis[node] == 1)
continue;
vis[node] = 1;
sum = sum + wt;
for (int i = 0; i < adj.get(node).size(); i++) {
int edw = adj.get(node).get(i).get(1);
int adjNode = adj.get(node).get(i).get(0);
// if(edw == 0) continue;
if (vis[adjNode] == 0) {
pq.add(new Pair(edw, adjNode));
}
}
}
return sum;
}
}