Skip to content

Commit 619c40a

Browse files
authored
Merge branch 'TheAlgorithms:master' into master
2 parents 418ae23 + 91101ec commit 619c40a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+554
-615
lines changed

.gitpod.dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM gitpod/workspace-java-21:2024-06-10-10-39-01
1+
FROM gitpod/workspace-java-21:2024-06-17-10-03-09
22

33
ENV LLVM_SCRIPT="tmp_llvm.sh"
44

checkstyle.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999

100100
<!-- Checks for Javadoc comments. -->
101101
<!-- See https://checkstyle.org/checks/javadoc/index.html -->
102-
<!-- TODO <module name="InvalidJavadocPosition"/> -->
102+
<module name="InvalidJavadocPosition"/>
103103
<!-- TODO <module name="JavadocMethod"/> -->
104104
<!-- TODO <module name="JavadocType"/> -->
105105
<!-- TODO <module name="JavadocVariable"/> -->
@@ -152,7 +152,7 @@
152152

153153
<!-- Checks for blocks. You know, those {}'s -->
154154
<!-- See https://checkstyle.org/checks/blocks/index.html -->
155-
<!-- TODO <module name="AvoidNestedBlocks"/> -->
155+
<module name="AvoidNestedBlocks"/>
156156
<!-- TODO <module name="EmptyBlock"/> -->
157157
<!-- TODO <module name="LeftCurly"/> -->
158158
<module name="NeedBraces"/>

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@
5555
<dependency>
5656
<groupId>org.apache.commons</groupId>
5757
<artifactId>commons-collections4</artifactId>
58-
<version>4.5.0-M1</version>
58+
<version>4.5.0-M2</version>
5959
</dependency>
6060
</dependencies>
6161

6262
<build>
6363
<plugins>
6464
<plugin>
6565
<artifactId>maven-surefire-plugin</artifactId>
66-
<version>3.2.5</version>
66+
<version>3.3.0</version>
6767
<configuration>
6868
<forkNode implementation="org.apache.maven.plugin.surefire.extensions.SurefireForkNodeFactory"/>
6969
</configuration>

src/main/java/com/thealgorithms/backtracking/AllPathsFromSourceToTarget.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
/**
2-
* Author : Siddhant Swarup Mallick
3-
* Github : https://github.com/siddhant2002
4-
*/
5-
6-
/** Program description - To find all possible paths from source to destination*/
7-
8-
/**Wikipedia link -> https://en.wikipedia.org/wiki/Shortest_path_problem */
91
package com.thealgorithms.backtracking;
102

113
import java.util.ArrayList;
124
import java.util.List;
135

6+
/**
7+
* Program description - To find all possible paths from source to destination
8+
* <a href="https://en.wikipedia.org/wiki/Shortest_path_problem">Wikipedia</a>
9+
*
10+
* @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
11+
*/
1412
public class AllPathsFromSourceToTarget {
1513

1614
// No. of vertices in graph

src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
/**
44
* Java program for Hamiltonian Cycle
5-
* (https://en.wikipedia.org/wiki/Hamiltonian_path)
5+
* <a href="https://en.wikipedia.org/wiki/Hamiltonian_path">wikipedia</a>
66
*
7-
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
7+
* @author <a href="https://github.com/itsAkshayDubey">Akshay Dubey</a>
88
*/
99
public class HamiltonianCycle {
1010

@@ -58,31 +58,31 @@ public boolean isPathFound(int vertex) {
5858
return true;
5959
}
6060

61-
/** all vertices selected but last vertex not linked to 0 **/
61+
/* all vertices selected but last vertex not linked to 0 **/
6262
if (this.pathCount == this.vertex) {
6363
return false;
6464
}
6565

6666
for (int v = 0; v < this.vertex; v++) {
67-
/** if connected **/
67+
/* if connected **/
6868
if (this.graph[vertex][v] == 1) {
69-
/** add to path **/
69+
/* add to path **/
7070
this.cycle[this.pathCount++] = v;
7171

72-
/** remove connection **/
72+
/* remove connection **/
7373
this.graph[vertex][v] = 0;
7474
this.graph[v][vertex] = 0;
7575

76-
/** if vertex not already selected solve recursively **/
76+
/* if vertex not already selected solve recursively **/
7777
if (!isPresent(v)) {
7878
return isPathFound(v);
7979
}
8080

81-
/** restore connection **/
81+
/* restore connection **/
8282
this.graph[vertex][v] = 1;
8383
this.graph[v][vertex] = 1;
8484

85-
/** remove path **/
85+
/* remove path **/
8686
this.cycle[--this.pathCount] = -1;
8787
}
8888
}

src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java

-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import java.util.Queue;
99
import java.util.Set;
1010

11-
/**
12-
* An algorithm that sorts a graph in toplogical order.
13-
*/
1411
/**
1512
* A class that represents the adjaceny list of a graph
1613
*/

src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java

+34-37
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,50 @@
66

77
/**
88
* Java program that implements Kosaraju Algorithm.
9-
* @author Shivanagouda S A (https://github.com/shivu2002a)
10-
*
11-
*/
12-
13-
/**
9+
* @author <a href="https://github.com/shivu2002a">Shivanagouda S A</a>
10+
* <p>
1411
* Kosaraju algorithm is a linear time algorithm to find the strongly connected components of a
15-
directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
16-
transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
17-
graph.
12+
directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
13+
transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
14+
graph.
1815
1916
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
20-
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
21-
connected. Single node is always a SCC.
17+
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
18+
connected. Single node is always a SCC.
2219
2320
* Example:
2421
25-
0 <--- 2 -------> 3 -------- > 4 ---- > 7
26-
| ^ | ^ ^
27-
| / | \ /
28-
| / | \ /
29-
v / v \ /
30-
1 5 --> 6
22+
0 <--- 2 -------> 3 -------- > 4 ---- > 7
23+
| ^ | ^ ^
24+
| / | \ /
25+
| / | \ /
26+
v / v \ /
27+
1 5 --> 6
3128
32-
For the above graph, the SCC list goes as follows:
33-
0, 1, 2
34-
3
35-
4, 5, 6
36-
7
29+
For the above graph, the SCC list goes as follows:
30+
0, 1, 2
31+
3
32+
4, 5, 6
33+
7
3734
38-
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
35+
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
3936
40-
{@summary}
37+
{@summary}
4138
* Kosaraju Algorithm:
42-
1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
43-
sorted by lowest finish time.
44-
2. Find the transpose graph by reversing the edges.
45-
3. Pop nodes one by one from the stack and again to DFS on the modified graph.
46-
47-
The transpose graph of the above graph:
48-
0 ---> 2 <------- 3 <------- 4 <------ 7
49-
^ / ^ \ /
50-
| / | \ /
51-
| / | \ /
52-
| v | v v
53-
1 5 <--- 6
54-
55-
We can observe that this graph has the same SCC as that of original graph.
39+
1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
40+
sorted by lowest finish time.
41+
2. Find the transpose graph by reversing the edges.
42+
3. Pop nodes one by one from the stack and again to DFS on the modified graph.
43+
44+
The transpose graph of the above graph:
45+
0 ---> 2 <------- 3 <------- 4 <------ 7
46+
^ / ^ \ /
47+
| / | \ /
48+
| / | \ /
49+
| v | v v
50+
1 5 <--- 6
51+
52+
We can observe that this graph has the same SCC as that of original graph.
5653
5754
*/
5855

src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java

+38-41
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,56 @@
66

77
/**
88
* Java program that implements Tarjan's Algorithm.
9-
* @author Shivanagouda S A (https://github.com/shivu2002a)
10-
*
11-
*/
12-
13-
/**
9+
* @author <a href="https://github.com/shivu2002a">Shivanagouda S A</a>
10+
* <p>
1411
* Tarjan's algorithm is a linear time algorithm to find the strongly connected components of a
15-
directed graph, which, from here onwards will be referred as SCC.
12+
directed graph, which, from here onwards will be referred as SCC.
1613
1714
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
18-
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
19-
connected. Single node is always a SCC.
15+
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
16+
connected. Single node is always a SCC.
2017
2118
* Example:
22-
0 --------> 1 -------> 3 --------> 4
23-
^ /
24-
| /
25-
| /
26-
| /
27-
| /
28-
| /
29-
| /
30-
| /
31-
| /
32-
| /
33-
|V
34-
2
35-
36-
For the above graph, the SCC list goes as follows:
37-
1, 2, 0
38-
3
39-
4
40-
41-
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
42-
43-
{@summary}
44-
Tarjan's Algorithm:
45-
* DFS search produces a DFS tree
46-
* Strongly Connected Components form subtrees of the DFS tree.
47-
* If we can find the head of these subtrees, we can get all the nodes in that subtree (including
48-
the head) and that will be one SCC.
49-
* There is no back edge from one SCC to another (here can be cross edges, but they will not be
50-
used).
51-
52-
* Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
53-
algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
19+
0 --------> 1 -------> 3 --------> 4
20+
^ /
21+
| /
22+
| /
23+
| /
24+
| /
25+
| /
26+
| /
27+
| /
28+
| /
29+
| /
30+
|V
31+
2
32+
33+
For the above graph, the SCC list goes as follows:
34+
1, 2, 0
35+
3
36+
4
37+
38+
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
39+
40+
{@summary}
41+
Tarjan's Algorithm:
42+
* DFS search produces a DFS tree
43+
* Strongly Connected Components form subtrees of the DFS tree.
44+
* If we can find the head of these subtrees, we can get all the nodes in that subtree (including
45+
the head) and that will be one SCC.
46+
* There is no back edge from one SCC to another (here can be cross edges, but they will not be
47+
used).
48+
49+
* Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
50+
algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
5451
5552
*/
5653
public class TarjansAlgorithm {
5754

5855
// Timer for tracking lowtime and insertion time
5956
private int time;
6057

61-
private List<List<Integer>> sccList = new ArrayList<List<Integer>>();
58+
private final List<List<Integer>> sccList = new ArrayList<List<Integer>>();
6259

6360
public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>> graph) {
6461

src/main/java/com/thealgorithms/datastructures/hashmap/hashing/Main.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,30 @@ public static void main(String[] args) {
2323
choice = scan.nextInt();
2424

2525
switch (choice) {
26-
case 1: {
26+
case 1:
2727
System.out.println("Enter the Key: ");
2828
key = scan.nextInt();
2929
h.insertHash(key);
3030
break;
31-
}
32-
case 2: {
31+
32+
case 2:
3333
System.out.println("Enter the Key delete: ");
3434
key = scan.nextInt();
3535
h.deleteHash(key);
3636
break;
37-
}
38-
case 3: {
37+
38+
case 3:
3939
System.out.println("Print table");
4040
h.displayHashtable();
4141
break;
42-
}
43-
case 4: {
42+
43+
case 4:
4444
scan.close();
4545
return;
46-
}
47-
default: {
46+
47+
default:
4848
throw new IllegalArgumentException("Unexpected value: " + choice);
4949
}
50-
}
5150
}
5251
}
5352
}

0 commit comments

Comments
 (0)