-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskals.java
More file actions
60 lines (55 loc) · 2.1 KB
/
Copy pathkruskals.java
File metadata and controls
60 lines (55 loc) · 2.1 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
import java.util.*;
class kruskals {
static final int max = 10;
static int cost[][] = new int[max][max]; //cost adjacency matrix
static int n;
static int parent[] = new int[max]; //this array will check the parent of the vertices to avoid the formation of loop
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter total no. of vertices : ");
n = input.nextInt();
System.out.println("Enter the cost Adjacency Matrix : ");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cost[i][j] = input.nextInt();
if (cost[i][j] == 0)
cost[i][j] = 999; //for same vertices
}
}
kruskalalgo();
input.close();
}
static void kruskalalgo() {
int mincost = 0, u = 0, v = 0, a = 0, b = 0, ne = 1, min = 999;
while (ne < n) {
min = 999;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (min > cost[i][j]) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u); //this will find the parent of the vertex u
v = find(v); //this will find the parent of the vertex v
if (u != v) { //dont consider if the parent of vertices are same, so that it will not form loop.
uni(u, v);
mincost += cost[u][v];
System.out.println(ne++ + "edge(" + a + "," + b + ")" + "=" + cost[u][v]);
}
cost[b][a] = 999;
cost[a][b] = 999;
}
System.out.println("Minimum cost : " + mincost); //minimum cost of the minimum spanning tree.
}
static int find(int i) { // this function will find the parent of a vertex
while (parent[i] > 0)
i = parent[i];
return i;
}
static void uni(int i, int j) {
parent[j] = i; // this function will update the parent array
}
}