-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkruskal.java
More file actions
99 lines (72 loc) · 1.45 KB
/
Copy pathkruskal.java
File metadata and controls
99 lines (72 loc) · 1.45 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import java.util.Scanner;
public class kruskal {
//parent node
int parent[] = new int[10];
//to check if visited or not
int find(int a){
int p = a;
if(parent[p] != 0) {
p = parent[p];
}
return p;
}
//to form the edge
void union(int i, int j) {
if(i<j) {
parent[i] =j;
}
else {
parent[j] =i;
}
}
void krsk(int a[][], int n)
{
int k=0;
int min, u=0, v=0;
int i,j;
int sum = 0;
while(k<n-1)
{
min = 99;
//with source
for ( i = 1; i<=n; i++)
//comparing vertex
for ( j = 1; j<=n; j++)
//check if the weight of the vertex is less than minimum and the vertex is not same
if(a[i][j]<min && i!=j)
{
min = a[i][j];
u= i;
v= j;
}
//check u and v
i = find(u);
j = find(v);
if(i!=j)
{
union(i,j);
System.out.println("("+u+","+v+") = "+a[u][v]);
sum+=a[u][v];
k++;
}
//maximize the weight of the visited edge
a[u][v]=a[v][v]=99;
}
System.out.print("Sum = "+sum);
}
public static void main(String[] args) {
int n;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of vertex");
n = sc.nextInt();
int w[][] = new int[n+1][n+1];
System.out.println("Enter the weighted matrix");
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++) {
w[i][j] = sc.nextInt();
}
kruskal kr = new kruskal();
kr.krsk(w,n);
sc.close();
}
}