- Notifications
You must be signed in to change notification settings - Fork20.1k
Add Krushkal's Minimum Spanning Tree algorithm#6409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
PayalB24 wants to merge1 commit intoTheAlgorithms:masterChoose a base branch fromPayalB24:kruskal-mst
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+77 −0
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletionssrc/main/java/com/thealgorithms/graph/KruskalMinimumSpanningTree.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.thealgorithms.graph; | ||
import java.util.*; | ||
/** | ||
* Implements Kruskal's Algorithm to find the Minimum Spanning Tree (MST) | ||
* of a connected, undirected, weighted graph using Union-Find. | ||
*/ | ||
public class KruskalMinimumSpanningTree { | ||
static 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 Integer.compare(this.weight, other.weight); | ||
} | ||
} | ||
static class UnionFind { | ||
int[] parent; | ||
UnionFind(int size) { | ||
parent = new int[size]; | ||
Arrays.fill(parent, -1); | ||
} | ||
int find(int node) { | ||
if (parent[node] < 0) return node; | ||
return parent[node] = find(parent[node]); | ||
} | ||
boolean union(int u, int v) { | ||
int rootU = find(u); | ||
int rootV = find(v); | ||
if (rootU == rootV) return false; | ||
parent[rootV] = rootU; | ||
return true; | ||
} | ||
} | ||
public static List<Edge> kruskalMST(List<Edge> edges, int vertices) { | ||
Collections.sort(edges); | ||
UnionFind uf = new UnionFind(vertices); | ||
List<Edge> mst = new ArrayList<>(); | ||
for (Edge edge : edges) { | ||
if (uf.union(edge.src, edge.dest)) { | ||
mst.add(edge); | ||
} | ||
if (mst.size() == vertices - 1) break; | ||
} | ||
return mst; | ||
} | ||
public static void main(String[] args) { | ||
int V = 4; | ||
List<Edge> edges = new ArrayList<>(); | ||
edges.add(new Edge(0, 1, 10)); | ||
edges.add(new Edge(0, 2, 6)); | ||
edges.add(new Edge(0, 3, 5)); | ||
edges.add(new Edge(1, 3, 15)); | ||
edges.add(new Edge(2, 3, 4)); | ||
List<Edge> mst = kruskalMST(edges, V); | ||
System.out.println("Edges in the Minimum Spanning Tree:"); | ||
for (Edge edge : mst) { | ||
System.out.println(edge.src + " - " + edge.dest + " : " + edge.weight); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.