forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskals_algo.java
More file actions
66 lines (60 loc) · 1.92 KB
/
Kruskals_algo.java
File metadata and controls
66 lines (60 loc) · 1.92 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
66
import java.util.Scanner;
public class KruskalsClass {
final static int MAX = 20;
static int n;
static int cost[][];
static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
ReadMatrix();
Kruskals();
}
static void ReadMatrix() {
int i, j;
cost = new int[MAX][MAX];
System.out.println("Implementation of Kruskal's algorithm");
System.out.println("Enter the no. of vertices");
n = scan.nextInt();
System.out.println("Enter the cost adjacency matrix");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
cost[i][j] = scan.nextInt();
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
}
static void Kruskals() {
int a = 0, b = 0, u = 0, v = 0, i, j, ne = 1, min, mincost = 0;
System.out.println("The edges of Minimum Cost Spanning Tree are");
while (ne < n) {
for (i = 1, min = 999; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u);
v = find(v);
if (u != v) {
uni(u, v);
System.out.println(ne++ + "edge (" + a + "," + b + ") =" + min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}
System.out.println("Minimum cost :" + mincost);
}
static int find(int i) {
int parent[] = new int[9];
while (parent[i] == 1)
i = parent[i];
return i;
}
static void uni(int i, int j) {
int parent[] = new int[9];
parent[j] = i;
}
}