-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal's Algorithm
80 lines (71 loc) · 2.15 KB
/
Kruskal's Algorithm
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
77
78
import java.util.Scanner;
public class Kruskals {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
System.out.println("Enter the number of vertices");
n = sc.nextInt();
int cA[][] = new int[n][n];
System.out.println("Enter the cost adjacency matrix.Enter 9999 for infinity");
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cA[i][j] = sc.nextInt();
int edges[] = new int[100];
int source[] = new int[100];
int destination[] = new int[100];
int k = 0;
for (int i = 0; i < n; i++)// Creating an edges array which contain the
// edges and corresponding source and
// destination arrays for each edge from the
// cost adjacency matrix
for (int j = 0; j < n; j++) {
if ((cA[i][j] != 0) && (cA[i][j] != 9999)) {
edges[k] = cA[i][j];
source[k] = i;
destination[k] = j;
cA[j][i] = 0;
k++;
}
}
for (int i = 0; i < k - 1; i++)// Sorting the edges array in increasing
// order and swapping the corresponding
// source and destination(Bubble Sort)
for (int j = 0; j < k - i - 1; j++) {
if (edges[j] > edges[j + 1]) {
swap(edges, j);
swap(source, j);
swap(destination, j);
}
}
int parent[] = new int[n];
int minimumCost = 0;
System.out.println("The edges in the MCST are:");
for (int i = 0; i < n; i++)
parent[i] = -1;
for (int j = 0; j < k; j++) {
int a = find(parent, source[j]);// Applying union find algorithm to
// detect cycle in the graph
int b = find(parent, destination[j]);
if (a != b) {
minimumCost += edges[j];
union(parent, a, b);
System.out.println((source[j]+1) + " -> " + (destination[j]+1) + " = " + edges[j]);
}
}
System.out.println("Minimum Cost of the Spanning Tree = " + minimumCost);
sc.close();
}
public static void swap(int array[], int index) {
int temp = array[index];
array[index] = array[index + 1];
array[index + 1] = temp;
}
public static int find(int parent[], int i) {
if (parent[i] == -1)
return i;
return find(parent, parent[i]);
}
public static void union(int parent[], int a, int b) {
parent[a] = b;
}
}