-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrims.java
More file actions
40 lines (37 loc) · 1.14 KB
/
Copy pathPrims.java
File metadata and controls
40 lines (37 loc) · 1.14 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
import java.util.Arrays;
public class Prims {
static int prims(int graph[][], int v) {
int dist[] = new int[v];
Arrays.fill(dist, Integer.MAX_VALUE);
dist[0] = 0;
boolean set[] = new boolean[v];
int result = 0;
for(int i = 0; i < v; i++) {
int source = -1;
for(int j = 0; j < v; j++) {
if(!set[j] && (source == -1 || dist[j] < source)) {
source = j;
}
}
set[source] = true;
result += dist[source];
for(int vertex = 0; vertex < v; vertex++) {
if(graph[source][vertex] != 0 && set[vertex] == false) {
dist[vertex] = Math.min(dist[vertex], graph[source][vertex]);
}
}
}
return result;
}
public static void main(String[] args) {
int V = 4;
int graph[][] = {
{0,5,8,0},
{5,0,10,15},
{8,10,0,20},
{0,15,20,0}
};
int result = prims(graph, V);
System.out.println(result);
}
}