Skip to content

Commit d7a6796

Browse files
authored
Merge pull request #1352 from MarcosVillacanas/marcosvillacanas-kruskal
Kruskal Implemented
2 parents 0c43428 + 174b43c commit d7a6796

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

DataStructures/Graphs/Kruskal.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//Problem -> Connect all the edges with the minimum cost.
2+
//Possible Solution -> Kruskal Algorithm (KA), KA finds the minimum-spanning-tree, which means, the group of edges with the minimum sum of their weights that connect the whole graph.
3+
//The graph needs to be connected, because if there are nodes impossible to reach, there are no edges that could connect every node in the graph.
4+
//KA is a Greedy Algorithm, because edges are analysed based on their weights, that is why a Priority Queue is used, to take first those less weighted.
5+
//This implementations below has some changes compared to conventional ones, but they are explained all along the code.
6+
7+
import java.util.Comparator;
8+
import java.util.HashSet;
9+
import java.util.PriorityQueue;
10+
11+
public class Kruskal {
12+
13+
//Complexity: O(E log V) time, where E is the number of edges in the graph and V is the number of vertices
14+
15+
private static class Edge{
16+
private int from;
17+
private int to;
18+
private int weight;
19+
20+
public Edge(int from, int to, int weight) {
21+
this.from = from;
22+
this.to = to;
23+
this.weight = weight;
24+
}
25+
}
26+
27+
private static void addEdge (HashSet<Edge>[] graph, int from, int to, int weight) {
28+
graph[from].add(new Edge(from, to, weight));
29+
}
30+
31+
public static void main(String[] args) {
32+
HashSet<Edge>[] graph = new HashSet[7];
33+
for (int i = 0; i < graph.length; i++) {
34+
graph[i] = new HashSet<>();
35+
}
36+
addEdge(graph,0, 1, 2);
37+
addEdge(graph,0, 2, 3);
38+
addEdge(graph,0, 3, 3);
39+
addEdge(graph,1, 2, 4);
40+
addEdge(graph,2, 3, 5);
41+
addEdge(graph,1, 4, 3);
42+
addEdge(graph,2, 4, 1);
43+
addEdge(graph,3, 5, 7);
44+
addEdge(graph,4, 5, 8);
45+
addEdge(graph,5, 6, 9);
46+
47+
System.out.println("Initial Graph: ");
48+
for (int i = 0; i < graph.length; i++) {
49+
for (Edge edge: graph[i]) {
50+
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to);
51+
}
52+
}
53+
54+
Kruskal k = new Kruskal();
55+
HashSet<Edge>[] solGraph = k.kruskal(graph);
56+
57+
System.out.println("\nMinimal Graph: ");
58+
for (int i = 0; i < solGraph.length; i++) {
59+
for (Edge edge: solGraph[i]) {
60+
System.out.println(i + " <-- weight " + edge.weight + " --> " + edge.to);
61+
}
62+
}
63+
}
64+
65+
public HashSet<Edge>[] kruskal (HashSet<Edge>[] graph) {
66+
int nodes = graph.length;
67+
int [] captain = new int [nodes];
68+
//captain of i, stores the set with all the connected nodes to i
69+
HashSet<Integer>[] connectedGroups = new HashSet[nodes];
70+
HashSet<Edge>[] minGraph = new HashSet[nodes];
71+
PriorityQueue<Edge> edges = new PriorityQueue<>((Comparator.comparingInt(edge -> edge.weight)));
72+
for (int i = 0; i < nodes; i++) {
73+
minGraph[i] = new HashSet<>();
74+
connectedGroups[i] = new HashSet<>();
75+
connectedGroups[i].add(i);
76+
captain[i] = i;
77+
edges.addAll(graph[i]);
78+
}
79+
int connectedElements = 0;
80+
//as soon as two sets merge all the elements, the algorithm must stop
81+
while (connectedElements != nodes && !edges.isEmpty()) {
82+
Edge edge = edges.poll();
83+
//This if avoids cycles
84+
if (!connectedGroups[captain[edge.from]].contains(edge.to)
85+
&& !connectedGroups[captain[edge.to]].contains(edge.from)) {
86+
//merge sets of the captains of each point connected by the edge
87+
connectedGroups[captain[edge.from]].addAll(connectedGroups[captain[edge.to]]);
88+
//update captains of the elements merged
89+
connectedGroups[captain[edge.from]].forEach(i -> captain[i] = captain[edge.from]);
90+
//add Edge to minimal graph
91+
addEdge(minGraph, edge.from, edge.to, edge.weight);
92+
//count how many elements have been merged
93+
connectedElements = connectedGroups[captain[edge.from]].size();
94+
}
95+
}
96+
return minGraph;
97+
}
98+
}

0 commit comments

Comments
 (0)