-
Notifications
You must be signed in to change notification settings - Fork 20k
Kruskal Implemented #1352
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
deadshotsb
merged 3 commits into
TheAlgorithms:master
from
oldMarcosVillacanas:marcosvillacanas-kruskal
Jul 7, 2020
Merged
Kruskal Implemented #1352
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Kruskal Algorithm Implemented
- Loading branch information
commit dd06589833e0117f3db2c196b82c0059c8107bba
There are no files selected for viewing
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,94 @@ | ||
package Kruskal; | ||
|
||
import java.util.Comparator; | ||
import java.util.HashSet; | ||
import java.util.PriorityQueue; | ||
|
||
public class Kruskal { | ||
|
||
//Complexity: O(E log V) time, where E is the number of edges in the graph and V is the number of vertices | ||
|
||
private static class Edge{ | ||
private int from; | ||
private int to; | ||
private int weight; | ||
|
||
public Edge(int from, int to, int weight) { | ||
this.from = from; | ||
this.to = to; | ||
this.weight = weight; | ||
} | ||
} | ||
|
||
private static void addEdge (HashSet<Edge>[] graph, int from, int to, int weight) { | ||
graph[from].add(new Edge(from, to, weight)); | ||
} | ||
|
||
public static void main(String[] args) { | ||
HashSet<Edge>[] graph = new HashSet[7]; | ||
for (int i = 0; i < graph.length; i++) { | ||
graph[i] = new HashSet<>(); | ||
} | ||
addEdge(graph,0, 1, 2); | ||
addEdge(graph,0, 2, 3); | ||
addEdge(graph,0, 3, 3); | ||
addEdge(graph,1, 2, 4); | ||
addEdge(graph,2, 3, 5); | ||
addEdge(graph,1, 4, 3); | ||
addEdge(graph,2, 4, 1); | ||
addEdge(graph,3, 5, 7); | ||
addEdge(graph,4, 5, 8); | ||
addEdge(graph,5, 6, 9); | ||
|
||
System.out.println("Initial Graph: "); | ||
for (int i = 0; i < graph.length; i++) { | ||
for (Edge edge: graph[i]) { | ||
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to); | ||
} | ||
} | ||
|
||
Kruskal k = new Kruskal(); | ||
HashSet<Edge>[] solGraph = k.kruskal(graph); | ||
|
||
System.out.println("\nMinimal Graph: "); | ||
for (int i = 0; i < solGraph.length; i++) { | ||
for (Edge edge: solGraph[i]) { | ||
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to); | ||
} | ||
} | ||
} | ||
|
||
public HashSet<Edge>[] kruskal (HashSet<Edge>[] graph) { | ||
int nodes = graph.length; | ||
int [] captain = new int [nodes]; | ||
//captain of i, stores the set with all the connected nodes to i | ||
HashSet<Integer>[] connectedGroups = new HashSet[nodes]; | ||
HashSet<Edge>[] minGraph = new HashSet[nodes]; | ||
PriorityQueue<Edge> edges = new PriorityQueue<>((Comparator.comparingInt(edge -> edge.weight))); | ||
for (int i = 0; i < nodes; i++) { | ||
minGraph[i] = new HashSet<>(); | ||
connectedGroups[i] = new HashSet<>(); | ||
connectedGroups[i].add(i); | ||
captain[i] = i; | ||
edges.addAll(graph[i]); | ||
} | ||
int connectedElements = 0; | ||
//as soon as two sets merge all the elements, the algorithm must stop | ||
while (connectedElements != nodes && !edges.isEmpty()) { | ||
Edge edge = edges.poll(); | ||
//This if avoids cycles | ||
if (!connectedGroups[captain[edge.from]].contains(edge.to) | ||
&& !connectedGroups[captain[edge.to]].contains(edge.from)) { | ||
//merge sets of the captains of each point connected by the edge | ||
connectedGroups[captain[edge.from]].addAll(connectedGroups[captain[edge.to]]); | ||
//update captains of the elements merged | ||
connectedGroups[captain[edge.from]].forEach(i -> captain[i] = captain[edge.from]); | ||
//add Edge to minimal graph | ||
addEdge(minGraph, edge.from, edge.to, edge.weight); | ||
//count how many elements have been merged | ||
connectedElements = connectedGroups[captain[edge.from]].size(); | ||
} | ||
} | ||
return minGraph; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repository is for educational purpose and therefore it will be great if you could add a description of your problem.