Skip to content

Commit dd06589

Browse files
Kruskal Algorithm Implemented
1 parent 88d65ff commit dd06589

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

DataStructures/Graphs/Kruskal.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package Kruskal;
2+
3+
import java.util.Comparator;
4+
import java.util.HashSet;
5+
import java.util.PriorityQueue;
6+
7+
public class Kruskal {
8+
9+
//Complexity: O(E log V) time, where E is the number of edges in the graph and V is the number of vertices
10+
11+
private static class Edge{
12+
private int from;
13+
private int to;
14+
private int weight;
15+
16+
public Edge(int from, int to, int weight) {
17+
this.from = from;
18+
this.to = to;
19+
this.weight = weight;
20+
}
21+
}
22+
23+
private static void addEdge (HashSet<Edge>[] graph, int from, int to, int weight) {
24+
graph[from].add(new Edge(from, to, weight));
25+
}
26+
27+
public static void main(String[] args) {
28+
HashSet<Edge>[] graph = new HashSet[7];
29+
for (int i = 0; i < graph.length; i++) {
30+
graph[i] = new HashSet<>();
31+
}
32+
addEdge(graph,0, 1, 2);
33+
addEdge(graph,0, 2, 3);
34+
addEdge(graph,0, 3, 3);
35+
addEdge(graph,1, 2, 4);
36+
addEdge(graph,2, 3, 5);
37+
addEdge(graph,1, 4, 3);
38+
addEdge(graph,2, 4, 1);
39+
addEdge(graph,3, 5, 7);
40+
addEdge(graph,4, 5, 8);
41+
addEdge(graph,5, 6, 9);
42+
43+
System.out.println("Initial Graph: ");
44+
for (int i = 0; i < graph.length; i++) {
45+
for (Edge edge: graph[i]) {
46+
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to);
47+
}
48+
}
49+
50+
Kruskal k = new Kruskal();
51+
HashSet<Edge>[] solGraph = k.kruskal(graph);
52+
53+
System.out.println("\nMinimal Graph: ");
54+
for (int i = 0; i < solGraph.length; i++) {
55+
for (Edge edge: solGraph[i]) {
56+
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to);
57+
}
58+
}
59+
}
60+
61+
public HashSet<Edge>[] kruskal (HashSet<Edge>[] graph) {
62+
int nodes = graph.length;
63+
int [] captain = new int [nodes];
64+
//captain of i, stores the set with all the connected nodes to i
65+
HashSet<Integer>[] connectedGroups = new HashSet[nodes];
66+
HashSet<Edge>[] minGraph = new HashSet[nodes];
67+
PriorityQueue<Edge> edges = new PriorityQueue<>((Comparator.comparingInt(edge -> edge.weight)));
68+
for (int i = 0; i < nodes; i++) {
69+
minGraph[i] = new HashSet<>();
70+
connectedGroups[i] = new HashSet<>();
71+
connectedGroups[i].add(i);
72+
captain[i] = i;
73+
edges.addAll(graph[i]);
74+
}
75+
int connectedElements = 0;
76+
//as soon as two sets merge all the elements, the algorithm must stop
77+
while (connectedElements != nodes && !edges.isEmpty()) {
78+
Edge edge = edges.poll();
79+
//This if avoids cycles
80+
if (!connectedGroups[captain[edge.from]].contains(edge.to)
81+
&& !connectedGroups[captain[edge.to]].contains(edge.from)) {
82+
//merge sets of the captains of each point connected by the edge
83+
connectedGroups[captain[edge.from]].addAll(connectedGroups[captain[edge.to]]);
84+
//update captains of the elements merged
85+
connectedGroups[captain[edge.from]].forEach(i -> captain[i] = captain[edge.from]);
86+
//add Edge to minimal graph
87+
addEdge(minGraph, edge.from, edge.to, edge.weight);
88+
//count how many elements have been merged
89+
connectedElements = connectedGroups[captain[edge.from]].size();
90+
}
91+
}
92+
return minGraph;
93+
}
94+
}

0 commit comments

Comments
 (0)