mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
style: enable InvalidJavadocPosition in checkstyle (#5237)
enable style InvalidJavadocPosition Co-authored-by: Samuel Facchinello <samuel.facchinello@piksel.com>
This commit is contained in:
committed by
GitHub
parent
39e065437c
commit
74e51990c1
@@ -2,9 +2,9 @@ package com.thealgorithms.datastructures.graphs;
|
||||
|
||||
/**
|
||||
* Java program for Hamiltonian Cycle
|
||||
* (https://en.wikipedia.org/wiki/Hamiltonian_path)
|
||||
* <a href="https://en.wikipedia.org/wiki/Hamiltonian_path">wikipedia</a>
|
||||
*
|
||||
* @author Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
* @author <a href="https://github.com/itsAkshayDubey">Akshay Dubey</a>
|
||||
*/
|
||||
public class HamiltonianCycle {
|
||||
|
||||
@@ -58,31 +58,31 @@ public class HamiltonianCycle {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** all vertices selected but last vertex not linked to 0 **/
|
||||
/* all vertices selected but last vertex not linked to 0 **/
|
||||
if (this.pathCount == this.vertex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int v = 0; v < this.vertex; v++) {
|
||||
/** if connected **/
|
||||
/* if connected **/
|
||||
if (this.graph[vertex][v] == 1) {
|
||||
/** add to path **/
|
||||
/* add to path **/
|
||||
this.cycle[this.pathCount++] = v;
|
||||
|
||||
/** remove connection **/
|
||||
/* remove connection **/
|
||||
this.graph[vertex][v] = 0;
|
||||
this.graph[v][vertex] = 0;
|
||||
|
||||
/** if vertex not already selected solve recursively **/
|
||||
/* if vertex not already selected solve recursively **/
|
||||
if (!isPresent(v)) {
|
||||
return isPathFound(v);
|
||||
}
|
||||
|
||||
/** restore connection **/
|
||||
/* restore connection **/
|
||||
this.graph[vertex][v] = 1;
|
||||
this.graph[v][vertex] = 1;
|
||||
|
||||
/** remove path **/
|
||||
/* remove path **/
|
||||
this.cycle[--this.pathCount] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,6 @@ import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* An algorithm that sorts a graph in toplogical order.
|
||||
*/
|
||||
/**
|
||||
* A class that represents the adjaceny list of a graph
|
||||
*/
|
||||
|
||||
@@ -6,53 +6,50 @@ import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Java program that implements Kosaraju Algorithm.
|
||||
* @author Shivanagouda S A (https://github.com/shivu2002a)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/shivu2002a">Shivanagouda S A</a>
|
||||
* <p>
|
||||
* Kosaraju algorithm is a linear time algorithm to find the strongly connected components of a
|
||||
directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
|
||||
transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
|
||||
graph.
|
||||
directed graph, which, from here onwards will be referred by SCC. It leverages the fact that the
|
||||
transpose graph (same graph with all the edges reversed) has exactly the same SCCs as the original
|
||||
graph.
|
||||
|
||||
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
|
||||
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
|
||||
connected. Single node is always a SCC.
|
||||
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
|
||||
connected. Single node is always a SCC.
|
||||
|
||||
* Example:
|
||||
|
||||
0 <--- 2 -------> 3 -------- > 4 ---- > 7
|
||||
| ^ | ^ ^
|
||||
| / | \ /
|
||||
| / | \ /
|
||||
v / v \ /
|
||||
1 5 --> 6
|
||||
0 <--- 2 -------> 3 -------- > 4 ---- > 7
|
||||
| ^ | ^ ^
|
||||
| / | \ /
|
||||
| / | \ /
|
||||
v / v \ /
|
||||
1 5 --> 6
|
||||
|
||||
For the above graph, the SCC list goes as follows:
|
||||
0, 1, 2
|
||||
3
|
||||
4, 5, 6
|
||||
7
|
||||
For the above graph, the SCC list goes as follows:
|
||||
0, 1, 2
|
||||
3
|
||||
4, 5, 6
|
||||
7
|
||||
|
||||
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
|
||||
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
|
||||
|
||||
{@summary}
|
||||
{@summary}
|
||||
* Kosaraju Algorithm:
|
||||
1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
|
||||
sorted by lowest finish time.
|
||||
2. Find the transpose graph by reversing the edges.
|
||||
3. Pop nodes one by one from the stack and again to DFS on the modified graph.
|
||||
1. Perform DFS traversal of the graph. Push node to stack before returning. This gives edges
|
||||
sorted by lowest finish time.
|
||||
2. Find the transpose graph by reversing the edges.
|
||||
3. Pop nodes one by one from the stack and again to DFS on the modified graph.
|
||||
|
||||
The transpose graph of the above graph:
|
||||
0 ---> 2 <------- 3 <------- 4 <------ 7
|
||||
^ / ^ \ /
|
||||
| / | \ /
|
||||
| / | \ /
|
||||
| v | v v
|
||||
1 5 <--- 6
|
||||
The transpose graph of the above graph:
|
||||
0 ---> 2 <------- 3 <------- 4 <------ 7
|
||||
^ / ^ \ /
|
||||
| / | \ /
|
||||
| / | \ /
|
||||
| v | v v
|
||||
1 5 <--- 6
|
||||
|
||||
We can observe that this graph has the same SCC as that of original graph.
|
||||
We can observe that this graph has the same SCC as that of original graph.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
@@ -6,51 +6,48 @@ import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Java program that implements Tarjan's Algorithm.
|
||||
* @author Shivanagouda S A (https://github.com/shivu2002a)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/shivu2002a">Shivanagouda S A</a>
|
||||
* <p>
|
||||
* Tarjan's algorithm is a linear time algorithm to find the strongly connected components of a
|
||||
directed graph, which, from here onwards will be referred as SCC.
|
||||
directed graph, which, from here onwards will be referred as SCC.
|
||||
|
||||
* A graph is said to be strongly connected if every vertex is reachable from every other vertex.
|
||||
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
|
||||
connected. Single node is always a SCC.
|
||||
The SCCs of a directed graph form a partition into subgraphs that are themselves strongly
|
||||
connected. Single node is always a SCC.
|
||||
|
||||
* Example:
|
||||
0 --------> 1 -------> 3 --------> 4
|
||||
^ /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
|V
|
||||
2
|
||||
0 --------> 1 -------> 3 --------> 4
|
||||
^ /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
| /
|
||||
|V
|
||||
2
|
||||
|
||||
For the above graph, the SCC list goes as follows:
|
||||
1, 2, 0
|
||||
3
|
||||
4
|
||||
For the above graph, the SCC list goes as follows:
|
||||
1, 2, 0
|
||||
3
|
||||
4
|
||||
|
||||
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
|
||||
We can also see that order of the nodes in an SCC doesn't matter since they are in cycle.
|
||||
|
||||
{@summary}
|
||||
Tarjan's Algorithm:
|
||||
* DFS search produces a DFS tree
|
||||
* Strongly Connected Components form subtrees of the DFS tree.
|
||||
* If we can find the head of these subtrees, we can get all the nodes in that subtree (including
|
||||
the head) and that will be one SCC.
|
||||
* There is no back edge from one SCC to another (here can be cross edges, but they will not be
|
||||
used).
|
||||
{@summary}
|
||||
Tarjan's Algorithm:
|
||||
* DFS search produces a DFS tree
|
||||
* Strongly Connected Components form subtrees of the DFS tree.
|
||||
* If we can find the head of these subtrees, we can get all the nodes in that subtree (including
|
||||
the head) and that will be one SCC.
|
||||
* There is no back edge from one SCC to another (here can be cross edges, but they will not be
|
||||
used).
|
||||
|
||||
* Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
|
||||
algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
|
||||
* Kosaraju Algorithm aims at doing the same but uses two DFS traversalse whereas Tarjan’s
|
||||
algorithm does the same in a single DFS, which leads to much lower constant factors in the latter.
|
||||
|
||||
*/
|
||||
public class TarjansAlgorithm {
|
||||
@@ -58,7 +55,7 @@ public class TarjansAlgorithm {
|
||||
// Timer for tracking lowtime and insertion time
|
||||
private int time;
|
||||
|
||||
private List<List<Integer>> sccList = new ArrayList<List<Integer>>();
|
||||
private final List<List<Integer>> sccList = new ArrayList<List<Integer>>();
|
||||
|
||||
public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>> graph) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user