-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimsAlgorithm.java
More file actions
65 lines (52 loc) · 1.69 KB
/
PrimsAlgorithm.java
File metadata and controls
65 lines (52 loc) · 1.69 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
import java.util.*;
public class PrimsAlgorithm {
private static final int INF = Integer.MAX_VALUE;
public static void primMST(int[][] graph) {
int vertices = graph.length;
int[] parent = new int[vertices];
int[] key = new int[vertices];
boolean[] visited = new boolean[vertices];
Arrays.fill(key, INF);
Arrays.fill(visited, false);
key[0] = 0;
parent[0] = -1;
for (int i = 0; i < vertices - 1; i++) {
int minKey = findMinKey(key, visited);
visited[minKey] = true;
for (int j = 0; j < vertices; j++) {
if (graph[minKey][j] != 0 && !visited[j] && graph[minKey][j] < key[j]) {
parent[j] = minKey;
key[j] = graph[minKey][j];
}
}
}
printMST(parent, graph);
}
private static int findMinKey(int[] key, boolean[] visited) {
int min = INF;
int minIndex = -1;
for (int i = 0; i < key.length; i++) {
if (!visited[i] && key[i] < min) {
min = key[i];
minIndex = i;
}
}
return minIndex;
}
private static void printMST(int[] parent, int[][] graph) {
System.out.println("Edge \tWeight");
for (int i = 1; i < graph.length; i++) {
System.out.println(parent[i] + " - " + i + "\t" + graph[i][parent[i]]);
}
}
public static void main(String[] args) {
int[][] graph = {
{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}
};
primMST(graph);
}
}