forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskalAlgorithm.java
More file actions
102 lines (81 loc) · 2.46 KB
/
KruskalAlgorithm.java
File metadata and controls
102 lines (81 loc) · 2.46 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
100
101
102
package com.thealgorithms.greedyalgorithms;
import java.util.*;
class Edge implements Comparable<Edge> {
int src, dest, weight;
Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
@Override
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
class DisjointSet {
int[] parent, rank;
DisjointSet(int n) {
parent = new int[n];
rank = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
void union(int x, int y) {
int rootX = find(x);
int rootY = find(y);
if (rootX != rootY) {
if (rank[rootX] < rank[rootY]) {
parent[rootX] = rootY;
} else if (rank[rootX] > rank[rootY]) {
parent[rootY] = rootX;
} else {
parent[rootY] = rootX;
rank[rootX]++;
}
}
}
}
public class KruskalAlgorithm {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of vertices: ");
int vertices = sc.nextInt();
System.out.print("Enter number of edges: ");
int edgesCount = sc.nextInt();
List<Edge> edges = new ArrayList<>();
System.out.println("Enter edges (src dest weight):");
for (int i = 0; i < edgesCount; i++) {
int src = sc.nextInt();
int dest = sc.nextInt();
int weight = sc.nextInt();
edges.add(new Edge(src, dest, weight));
}
Collections.sort(edges);
DisjointSet ds = new DisjointSet(vertices);
List<Edge> mst = new ArrayList<>();
for (Edge edge : edges) {
int root1 = ds.find(edge.src);
int root2 = ds.find(edge.dest);
if (root1 != root2) {
mst.add(edge);
ds.union(root1, root2);
}
}
System.out.println("\nMinimum Spanning Tree:");
int totalWeight = 0;
for (Edge e : mst) {
System.out.println(e.src + " -- " + e.dest + " == " + e.weight);
totalWeight += e.weight;
}
System.out.println("Total Weight = " + totalWeight);
sc.close();
}
}