Skip to content
Merged
Changes from 4 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
40620ae
To find sum of triplets according to given value
joshiujjawal22 May 18, 2020
c0eb73a
Rotation of an array without using extra space
joshiujjawal22 May 18, 2020
63b9e13
Create BucketSort.java
vineetrathor May 21, 2020
227dd31
Merge pull request #1 from THE-VR7/THE-VR7-patch-1
vineetrathor May 21, 2020
627115b
Created Graph Algos
Ritik2604 May 22, 2020
278529e
Create SinglyLinkedList
Ritik2604 May 23, 2020
4842a4c
Update and rename SinglyLinkedList to ListAddnFun
Ritik2604 May 23, 2020
ef2be07
Create AVLSimple
Ritik2604 May 23, 2020
07aee8b
Create BoardPath
Ritik2604 May 23, 2020
e717f96
Create CountNumBinaryStrings
Ritik2604 May 23, 2020
9a8ccea
Delete LinkedList.java
Ritik2604 May 23, 2020
3a234f0
Delete Node.java
Ritik2604 May 23, 2020
01d5814
Create Intersection
Ritik2604 May 23, 2020
ed497ce
Update BucketSort.java
vineetrathor May 23, 2020
1ef46db
Update GraphAlgos
Ritik2604 May 24, 2020
a5f42e2
Fixed Error:(6, 8) java: class algorithm is public, should be declare…
May 24, 2020
f710f3a
Update ListAddnFun
Ritik2604 May 24, 2020
82e2132
Update AVLSimple
Ritik2604 May 24, 2020
920852a
Update BoardPath
Ritik2604 May 24, 2020
94cfab0
Update BoardPath
Ritik2604 May 24, 2020
ec4f6a1
Update CountNumBinaryStrings
Ritik2604 May 24, 2020
a23a17b
Update Intersection
Ritik2604 May 24, 2020
c76c4dd
Update CONTRIBUTING.md file
MohamedBechir May 24, 2020
d342c2d
Merge pull request #1322 from Ritik2604/master
deadshotsb May 25, 2020
91b9ac7
updating DIRECTORY.md
May 25, 2020
8b6415f
Update CONTRIBUTION.md
deadshotsb May 25, 2020
c37257c
Merge pull request #1326 from MohamedBechir/patch-1
deadshotsb May 25, 2020
88c6ad9
Merge pull request #1325 from mariaRoxana94/fix-error
StepfenShawn May 28, 2020
3b52109
Added test cases
joshiujjawal22 May 29, 2020
65dcfd9
Merge pull request #1316 from joshiujjawal22/master
deadshotsb May 29, 2020
a6398d1
updating DIRECTORY.md
May 29, 2020
de85372
Merge pull request #1321 from THE-VR7/master
StepfenShawn Jun 1, 2020
e3a78a6
updating DIRECTORY.md
Jun 1, 2020
b1caf8c
Minimum sum partition
swatiprajapati08 Jun 1, 2020
5d77174
Update Minimum sum partition
swatiprajapati08 Jun 3, 2020
4ac4a62
Merge pull request #1338 from swatiprajapati08/patch-1
deadshotsb Jun 3, 2020
7179718
Update ParseInteger.java
a1denvalu3 Jun 9, 2020
88d65ff
Merge pull request #1347 from lollerfirst/patch-1, close #1345
yanglbme Jun 12, 2020
1553639
marcosvillacanas-A-Star
MarcosVillacanas Jun 12, 2020
ce945bc
Complexity Added
MarcosVillacanas Jun 12, 2020
dd06589
Kruskal Algorithm Implemented
MarcosVillacanas Jun 17, 2020
c21ae9a
Graph Initialization outside private classes
MarcosVillacanas Jun 19, 2020
5150a6b
Closed Scanner in 3 sum, fixed syntax errors in BucketSort.java
hardik-kapadia Jun 29, 2020
d07e668
Fixed <T> parameter error in SelectionSort
hardik-kapadia Jun 29, 2020
95389bf
Fixed wrong order in BubbleSort, corrected spelling mistakes
Jun 30, 2020
4b42784
Merge pull request #1357 from astralcake/master
StepfenShawn Jul 4, 2020
bf0fdd7
Context added
MarcosVillacanas Jul 6, 2020
174b43c
link-Removed
MarcosVillacanas Jul 6, 2020
0c43428
Merge pull request #1354 from thecoderenroute/master
StepfenShawn Jul 7, 2020
d7a6796
Merge pull request #1352 from MarcosVillacanas/marcosvillacanas-kruskal
deadshotsb Jul 7, 2020
6f0ea8d
updating DIRECTORY.md
Jul 7, 2020
4e058de
Update README.md
StepfenShawn Jul 8, 2020
7a4d6b2
z function added
vakhokoto Jul 10, 2020
a63cb70
Merge pull request #1350 from MarcosVillacanas/marcosvillacanas-A-Star
AnupKumarPanwar Jul 16, 2020
b8fa504
updating DIRECTORY.md
Jul 16, 2020
a983a15
description added and also link to the algorithm review
vakhokoto Jul 22, 2020
8bb54ca
Merge pull request #1362 from vakhokoto/z-function
deadshotsb Jul 22, 2020
5e8c8c7
updating DIRECTORY.md
Jul 22, 2020
6e7a326
Revert "z function added"
deadshotsb Jul 22, 2020
c4a8f71
updating DIRECTORY.md
Jul 22, 2020
d2870c8
Merge pull request #1365 from TheAlgorithms/revert-1362-z-function
deadshotsb Jul 22, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions DataStructures/Graphs/Kruskal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
//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;

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;
}
}