Threads
java ( 2123563 ) - java ( 2123565 ) stack: com.thealgorithms.datastructures.graphs.Kruskal.main(Kruskal.java:37)
package com.thealgorithms.datastructures.graphs;
// Problem -> Connect all the edges with the minimum cost.
// 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.
// 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.
// 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.
// This implementations below has some changes compared to conventional ones, but they are explained
// all along the code.
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;
Edge(int from, int to, int weight) {
this.from = from;
this.to = to;
this.weight = weight;
}
}
private static void addEdge(HashSet[] graph, int from, int to, int weight) {
graph[from].add(new Edge(from, to, weight));
}
public static void main(String[] args) {
HashSet[] 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[] 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[] kruskal(HashSet[] graph) {
int nodes = graph.length;
int[] captain = new int[nodes];
// captain of i, stores the set with all the connected nodes to i
HashSet[] connectedGroups = new HashSet[nodes];
HashSet[] minGraph = new HashSet[nodes];
PriorityQueue 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;
}
}
Variables All
No. | From | Name | Value |
---|---|---|---|
1 | 37 | args | [Ljava.lang.String;@7852e922 |
END | 0 | 0 | 0 |
Process Filter | Thread Filter |
---|---|
2123563 java | 2123565 java |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | java | 2123563 | 2123565 | java | Initial Graph: |
2 | java | 2123563 | 2123565 | java | 0 <-- weight 3 --> 3 |
3 | java | 2123563 | 2123565 | java | 0 <-- weight 3 --> 2 |
4 | java | 2123563 | 2123565 | java | 0 <-- weight 2 --> 1 |
5 | java | 2123563 | 2123565 | java | 1 <-- weight 4 --> 2 |
6 | java | 2123563 | 2123565 | java | 1 <-- weight 3 --> 4 |
7 | java | 2123563 | 2123565 | java | 2 <-- weight 1 --> 4 |
8 | java | 2123563 | 2123565 | java | 2 <-- weight 5 --> 3 |
9 | java | 2123563 | 2123565 | java | 3 <-- weight 7 --> 5 |
10 | java | 2123563 | 2123565 | java | 4 <-- weight 8 --> 5 |
11 | java | 2123563 | 2123565 | java | 5 <-- weight 9 --> 6 |
12 | java | 2123563 | 2123565 | java | |
13 | java | 2123563 | 2123565 | java | Minimal Graph: |
14 | java | 2123563 | 2123565 | java | 0 <-- weight 3 --> 3 |
15 | java | 2123563 | 2123565 | java | 0 <-- weight 3 --> 2 |
16 | java | 2123563 | 2123565 | java | 0 <-- weight 2 --> 1 |
17 | java | 2123563 | 2123565 | java | 2 <-- weight 1 --> 4 |
18 | java | 2123563 | 2123565 | java | 3 <-- weight 7 --> 5 |
19 | java | 2123563 | 2123565 | java | 5 <-- weight 9 --> 6 |
END | 0 | 0 | 0 | 0 | 0 |
×
Functions and Shortcuts
No. | Function | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | Alg-Java |
Update: | 20240824 |
Commit: | a7cd97d7 |
Source Code: | datastructures.graphs.Kruskal |
BuildTool: | Java17 |
Compiler: | Java17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |