|
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * 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> |
14 | 11 | * 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. |
16 | 13 |
|
17 | 14 | * 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. |
20 | 17 |
|
21 | 18 | * 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. |
54 | 51 |
|
55 | 52 | */
|
56 | 53 | public class TarjansAlgorithm {
|
57 | 54 |
|
58 | 55 | // Timer for tracking lowtime and insertion time
|
59 | 56 | private int time;
|
60 | 57 |
|
61 |
| - private List<List<Integer>> sccList = new ArrayList<List<Integer>>(); |
| 58 | + private final List<List<Integer>> sccList = new ArrayList<List<Integer>>(); |
62 | 59 |
|
63 | 60 | public List<List<Integer>> stronglyConnectedComponents(int v, List<List<Integer>> graph) {
|
64 | 61 |
|
|
0 commit comments