Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,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();
}
}
Loading