-
Notifications
You must be signed in to change notification settings - Fork 2
/
MST.java
executable file
·48 lines (44 loc) · 1.36 KB
/
MST.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
47
48
import java.util.*;
class MST {
/**Searches a graph for its minimum spanning tree. Prim's algorithm.
* Allows for negative weights and disconnected graph
* Requires: Pair
* O((m+n)*log(n))
* @param graph Graph represented by adjacency list
* @return An [n] array with each vertex' parent*/
static int[] mst(List<List<Pair>> graph) {
int n = graph.size();
// Initialize visited, parent and cost arrays
boolean[] visited = new boolean[n];
int[] parent = new int[n];
int[] cost = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(cost, Integer.MAX_VALUE);
Queue<Pair> queue = new PriorityQueue(10, new Pair.CompareSecond());
int count = 0;
int s = 0;
while(s != -1) {
queue.add(new Pair(s, 0));
while(!queue.isEmpty() && count < n) {
Pair a = queue.poll();
if(!visited[a.fst]) {
++count;
visited[a.fst] = true;
for(Pair b : graph.get(a.fst))
if(!visited[b.fst] && cost[b.fst] > b.snd) {
cost[b.fst] = b.snd;
parent[b.fst] = a.fst;
queue.add(new Pair(b.fst, b.snd));
}
}
}
s = -1;
if(count < n)
// Find next vertex to start search from
for(int i = 0; i < n && s == -1; ++i)
if(!visited[i])
s = i;
}
return parent;
}
}