From 13dd42eb8d1df210fcfba725946f5c18d72f85d3 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Tue, 17 Jan 2023 08:13:43 +0530 Subject: [PATCH 1/8] Adding Kosaraju Algorithm --- .../datastructures/graphs/Kosaraju.java | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java new file mode 100644 index 000000000000..4d1100b46517 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java @@ -0,0 +1,145 @@ +package com.thealgorithms.datastructures.graphs; + +import java.util.ArrayList; +import java.util.List; +import java.util.Stack; + +/** + * Java program that implements Kosaraju Algorithm. + * @author Shivanagouda S A (https://github.com/shivu2002a) + * + */ + +/** + * 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. + + * 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. + + * Example: + + 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 + + {@summary} + * Kosaraju Algorithm: + 1. Perform DFS traversal of the graph. Push node to stack before returning. + 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 + + We can observe that this graph has the same SCC as that of original graph. + + */ + +public class Kosaraju { + + //Store each component + private List scc = new ArrayList<>(); + + //All the strongly connected components + private List> sccsList = new ArrayList<>(); + + public void kosaraju(int v, List> list){ + + //Sort the edges according to lowest finish time + var st = new Stack(); + int vis[] = new int[v]; + for (int i = 0; i < v; i++) { + if(vis[i] == 0){ + dfs(i, vis, list, st); + } + } + + //Create a transpose graph of the given graph + var transposeGraph = new ArrayList>(8); + for (int i = 0; i < v; i++) { + transposeGraph.add(new ArrayList<>()); + } + for (int i = 0; i < vis.length; i++) { + vis[i] = 0; + for (Integer neigh : list.get(i)) { + transposeGraph.get(neigh).add(i); + } + } + + //Run DFS on the transpose graph and get the Strongly Connected Components + while (!st.isEmpty()) { + var node = st.pop(); + if(vis[node] == 0){ + dfs2(node, vis, transposeGraph); + System.out.println(); + sccsList.add(scc); + scc = new ArrayList<>(); + } + } + } + + //Dfs to store the nodes in order of lowest finish time + private void dfs(int node, int vis[], List> list, Stack st){ + vis[node] = 1; + for(Integer neighbour : list.get(node)){ + if(vis[neighbour] == 0) + dfs(neighbour, vis, list, st); + } + st.push(node); + } + + //Dfs to find all the nodes of each strongly connected component + private void dfs2(int node, int vis[], List> list){ + vis[node] = 1; + for(Integer neighbour : list.get(node)){ + if(vis[neighbour] == 0) + dfs2(neighbour, vis, list); + } + scc.add(node); + } + + + public static void main(String[] args) { + var n = 8; + var adjList = new ArrayList>(n); + + for (int i = 0; i < n; i++) { + adjList.add(new ArrayList<>()); + } + + adjList.get(0).add(1); + adjList.get(1).add(2); + adjList.get(2).add(0); + adjList.get(2).add(3); + adjList.get(3).add(4); + adjList.get(4).add(5); + adjList.get(4).add(7); + adjList.get(5).add(6); + adjList.get(6).add(4); + adjList.get(6).add(7); + System.out.println(adjList); + + var scc = new Kosaraju(); + scc.kosaraju(n, adjList); + + System.out.println(scc.sccsList); + } + +} From 4c5773e45d7930af7659cae5faffcf11cae3c6eb Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Sat, 4 Feb 2023 08:04:31 +0530 Subject: [PATCH 2/8] Adding changes to Kosaraju --- .../com/thealgorithms/datastructures/graphs/Kosaraju.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java index 4d1100b46517..8deb05063066 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java @@ -60,7 +60,7 @@ public class Kosaraju { //All the strongly connected components private List> sccsList = new ArrayList<>(); - public void kosaraju(int v, List> list){ + public List> kosaraju(int v, List> list){ //Sort the edges according to lowest finish time var st = new Stack(); @@ -93,6 +93,7 @@ public void kosaraju(int v, List> list){ scc = new ArrayList<>(); } } + return sccsList; } //Dfs to store the nodes in order of lowest finish time @@ -137,9 +138,9 @@ public static void main(String[] args) { System.out.println(adjList); var scc = new Kosaraju(); - scc.kosaraju(n, adjList); + List> sccsList = scc.kosaraju(n, adjList); - System.out.println(scc.sccsList); + System.out.println(sccsList); } } From 08e7fa0615bd48ef380bbeb238b396ae50371067 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Sat, 4 Feb 2023 08:06:14 +0530 Subject: [PATCH 3/8] Adding testcases for Kosaraju Algorithm --- .../datastructures/graphs/KosarajuTest.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java new file mode 100644 index 000000000000..e34d236f47a3 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java @@ -0,0 +1,83 @@ +package com.thealgorithms.datastructures.graphs; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class KosarajuTest { + + private Kosaraju kosaraju = new Kosaraju(); + + @Test + public void findStronglyConnectedComps() { + //Create a adjacency list of graph + var n = 8; + var adjList = new ArrayList>(n); + + for (int i = 0; i < n; i++) { + adjList.add(new ArrayList<>()); + } + + adjList.get(0).add(1); + adjList.get(1).add(2); + adjList.get(2).add(0); + adjList.get(2).add(3); + adjList.get(3).add(4); + adjList.get(4).add(5); + adjList.get(4).add(7); + adjList.get(5).add(6); + adjList.get(6).add(4); + adjList.get(6).add(7); + + List> actualResult = kosaraju.kosaraju(n, adjList); + List> expectedResult = new ArrayList<>(); + /* + Expected result: + 0, 1, 2, 0 + 3 + 5, 4, 6 + 7 + */ + expectedResult.add(Arrays.asList(1, 2, 0)); + expectedResult.add(Arrays.asList(3)); + expectedResult.add(Arrays.asList(5, 6, 4)); + expectedResult.add(Arrays.asList(7)); + assertTrue(expectedResult.equals(actualResult)); + } + + @Test + public void findStronglyConnectedCompsShouldGetSingleNodes() { + //Create a adjacency list of graph + var n = 8; + var adjList = new ArrayList>(n); + + for (int i = 0; i < n; i++) { + adjList.add(new ArrayList<>()); + } + + adjList.get(0).add(1); + adjList.get(1).add(2); + adjList.get(2).add(3); + adjList.get(3).add(4); + adjList.get(4).add(5); + adjList.get(5).add(6); + adjList.get(6).add(7); + adjList.get(7).add(0); + + List> actualResult = kosaraju.kosaraju(n, adjList); + System.out.println(actualResult); + List> expectedResult = new ArrayList<>(); + /* + Expected result: + 0, 1, 2, 3, 4, 5, 6, 7 + */ + expectedResult.add(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 0)); + assertTrue(expectedResult.equals(actualResult)); + } + +} From 90ee4b93ac4cbe055340c304f372e94fcf858b78 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Mon, 6 Feb 2023 17:05:43 +0530 Subject: [PATCH 4/8] Extracted methods --- .../datastructures/graphs/Kosaraju.java | 78 ++++++++++++++----- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java index 8deb05063066..666911c2e9a1 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java @@ -29,14 +29,16 @@ 1 5 --> 6 For the above graph, the SCC list goes as follows: - 0, 1, 2 + 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. + {@summary} * Kosaraju Algorithm: - 1. Perform DFS traversal of the graph. Push node to stack before returning. + 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. @@ -54,56 +56,83 @@ public class Kosaraju { + // Sort edges according to lowest finish time + Stack stack = new Stack(); + //Store each component private List scc = new ArrayList<>(); //All the strongly connected components private List> sccsList = new ArrayList<>(); + /** + * + * @param v Node count + * @param list Adjacency list of graph + * @return List of SCCs + */ public List> kosaraju(int v, List> list){ //Sort the edges according to lowest finish time - var st = new Stack(); + sortEdgesByLowestFinishTime(v, list); + + //Create transpose graph of the given graph + List> transposeGraph = createTransposeMatrix(v, list); + + //Run DFS on the transpose graph and get the Strongly Connected Components + findSCCs(v, transposeGraph); + + //Return the SCCs + return sccsList; + } + + private void sortEdgesByLowestFinishTime(int v, List> list){ int vis[] = new int[v]; for (int i = 0; i < v; i++) { if(vis[i] == 0){ - dfs(i, vis, list, st); + dfs(i, vis, list); } } + } - //Create a transpose graph of the given graph - var transposeGraph = new ArrayList>(8); + private List> createTransposeMatrix(int v, List> list) { + var transposeGraph = new ArrayList>(v); for (int i = 0; i < v; i++) { transposeGraph.add(new ArrayList<>()); } - for (int i = 0; i < vis.length; i++) { - vis[i] = 0; + for (int i = 0; i < v; i++) { for (Integer neigh : list.get(i)) { transposeGraph.get(neigh).add(i); } } + return transposeGraph; + } - //Run DFS on the transpose graph and get the Strongly Connected Components - while (!st.isEmpty()) { - var node = st.pop(); + /** + * + * @param v Node count + * @param transposeGraph Transpose of the given adjacency list + */ + public void findSCCs(int v, List> transposeGraph){ + int vis[] = new int[v]; + while (!stack.isEmpty()) { + var node = stack.pop(); if(vis[node] == 0){ dfs2(node, vis, transposeGraph); - System.out.println(); sccsList.add(scc); scc = new ArrayList<>(); } } - return sccsList; } //Dfs to store the nodes in order of lowest finish time - private void dfs(int node, int vis[], List> list, Stack st){ + private void dfs(int node, int vis[], List> list){ vis[node] = 1; for(Integer neighbour : list.get(node)){ if(vis[neighbour] == 0) - dfs(neighbour, vis, list, st); + dfs(neighbour, vis, list); } - st.push(node); + stack.push(node); } //Dfs to find all the nodes of each strongly connected component @@ -116,7 +145,6 @@ private void dfs2(int node, int vis[], List> list){ scc.add(node); } - public static void main(String[] args) { var n = 8; var adjList = new ArrayList>(n); @@ -135,12 +163,24 @@ public static void main(String[] args) { adjList.get(5).add(6); adjList.get(6).add(4); adjList.get(6).add(7); - System.out.println(adjList); + + System.out.println("Adjacency list: "); + for (int i = 0; i < adjList.size(); i++) { + System.out.print(i + ": "); + if(adjList.get(i).isEmpty()){ + System.out.println("No outgoing edges"); + continue; + } + for (int index = 0; index < adjList.get(i).size(); index++) { + System.out.print(adjList.get(i).get(index) + " "); + } + System.out.println(); + } var scc = new Kosaraju(); List> sccsList = scc.kosaraju(n, adjList); - System.out.println(sccsList); + System.out.println("\nStrongly connected components: " + sccsList); } } From c7555756238977878afb7bbb8a241f6a94a52279 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Mon, 6 Feb 2023 17:07:54 +0530 Subject: [PATCH 5/8] Adding changes --- .../com/thealgorithms/datastructures/graphs/KosarajuTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java index e34d236f47a3..838f8cedbe4d 100644 --- a/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java +++ b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java @@ -1,6 +1,5 @@ package com.thealgorithms.datastructures.graphs; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.ArrayList; @@ -38,7 +37,7 @@ public void findStronglyConnectedComps() { List> expectedResult = new ArrayList<>(); /* Expected result: - 0, 1, 2, 0 + 0, 1, 2 3 5, 4, 6 7 From 1dd0a4b249e4b1b1b4f64a643abb71e18fb17014 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Tue, 7 Feb 2023 12:35:02 +0530 Subject: [PATCH 6/8] Adding suggested changes --- .../datastructures/graphs/Kosaraju.java | 46 +------------------ .../datastructures/graphs/KosarajuTest.java | 1 - 2 files changed, 2 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java index 666911c2e9a1..b3632b47970d 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java @@ -73,16 +73,12 @@ public class Kosaraju { */ public List> kosaraju(int v, List> list){ - //Sort the edges according to lowest finish time sortEdgesByLowestFinishTime(v, list); - //Create transpose graph of the given graph List> transposeGraph = createTransposeMatrix(v, list); - //Run DFS on the transpose graph and get the Strongly Connected Components - findSCCs(v, transposeGraph); + findStronglyConnectedComponents(v, transposeGraph); - //Return the SCCs return sccsList; } @@ -113,7 +109,7 @@ private List> createTransposeMatrix(int v, List> lis * @param v Node count * @param transposeGraph Transpose of the given adjacency list */ - public void findSCCs(int v, List> transposeGraph){ + public void findStronglyConnectedComponents(int v, List> transposeGraph){ int vis[] = new int[v]; while (!stack.isEmpty()) { var node = stack.pop(); @@ -144,43 +140,5 @@ private void dfs2(int node, int vis[], List> list){ } scc.add(node); } - - public static void main(String[] args) { - var n = 8; - var adjList = new ArrayList>(n); - - for (int i = 0; i < n; i++) { - adjList.add(new ArrayList<>()); - } - - adjList.get(0).add(1); - adjList.get(1).add(2); - adjList.get(2).add(0); - adjList.get(2).add(3); - adjList.get(3).add(4); - adjList.get(4).add(5); - adjList.get(4).add(7); - adjList.get(5).add(6); - adjList.get(6).add(4); - adjList.get(6).add(7); - - System.out.println("Adjacency list: "); - for (int i = 0; i < adjList.size(); i++) { - System.out.print(i + ": "); - if(adjList.get(i).isEmpty()){ - System.out.println("No outgoing edges"); - continue; - } - for (int index = 0; index < adjList.get(i).size(); index++) { - System.out.print(adjList.get(i).get(index) + " "); - } - System.out.println(); - } - - var scc = new Kosaraju(); - List> sccsList = scc.kosaraju(n, adjList); - - System.out.println("\nStrongly connected components: " + sccsList); - } } diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java index 838f8cedbe4d..5395b04ee732 100644 --- a/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java +++ b/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java @@ -69,7 +69,6 @@ public void findStronglyConnectedCompsShouldGetSingleNodes() { adjList.get(7).add(0); List> actualResult = kosaraju.kosaraju(n, adjList); - System.out.println(actualResult); List> expectedResult = new ArrayList<>(); /* Expected result: From 1bf045e161ab3878297f3cd5cf02287c3c0667e9 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Sat, 11 Feb 2023 14:16:16 +0530 Subject: [PATCH 7/8] Adding Tarjan's Algorithm and Tests --- .../graphs/TarjansAlgorithm.java | 138 ++++++++++++++++++ .../graphs/TarjansAlgorithmTest.java | 72 +++++++++ 2 files changed, 210 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java create mode 100644 src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java new file mode 100644 index 000000000000..e452b379acc8 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java @@ -0,0 +1,138 @@ +package com.thealgorithms.datastructures.graphs; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Stack; + +/** + * Java program that implements Kosaraju Algorithm. + * @author Shivanagouda S A (https://github.com/shivu2002a) + * + */ + +/** + * 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. + + * 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. + + * Example: + 0 --------> 1 -------> 3 --------> 4 + ^ / + | / + | / + | / + | / + | / + | / + | / + | / + | / + |V + 2 + + 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. + + {@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. + + */ +public class TarjansAlgorithm { + + //Timer for tracking lowtime and insertion time + private int Time; + + private List> SCClist = new ArrayList>(); + + public List> stronglyConnectedComponents(int V, List> graph) { + + // Initially all vertices as unvisited, insertion and low time are undefined + + // insertionTime:Time when a node is visited 1st time while DFS traversal + + // lowTime: indicates the earliest visited vertex (the vertex with minimum insertion time) that can + // be reached from a subtree rooted with a particular node. + int lowTime[] = new int[V]; + int insertionTime[] = new int[V]; + for (int i = 0; i < V; i++) { + insertionTime[i] = -1; + lowTime[i] = -1; + } + + // To check if element is present in stack + boolean isInStack[] = new boolean[V]; + + // Store nodes during DFS + Stack st = new Stack(); + + for (int i = 0; i < V; i++) { + if (insertionTime[i] == -1) + stronglyConnCompsUtil(i, lowTime, insertionTime, isInStack, st, graph); + } + + return SCClist; + } + + private void stronglyConnCompsUtil(int u, int lowTime[], int insertionTime[], + boolean isInStack[], Stack st, List> graph) { + + // Initialize insertion time and lowTime value of current node + insertionTime[u] = Time; + lowTime[u] = Time; + Time += 1; + + //Push current node into stack + isInStack[u] = true; + st.push(u); + + int n; + + // Go through all vertices adjacent to this + Iterator i = graph.get(u).iterator(); + + while (i.hasNext()) { + n = i.next(); + + //If the adjacent node is unvisited, do DFS + if (insertionTime[n] == -1) { + stronglyConnCompsUtil(n, lowTime, insertionTime, isInStack, st, graph); + //update lowTime for the current node comparing lowtime of adj node + lowTime[u] = Math.min(lowTime[u], lowTime[n]); + } else if (isInStack[n] == true) { + //If adj node is in stack, update low + lowTime[u] = Math.min(lowTime[u], insertionTime[n]); + } + } + //If lowtime and insertion time are same, current node is the head of an SCC + // head node found, get all the nodes in this SCC + if (lowTime[u] == insertionTime[u]) { + int w = -1; + var scc = new ArrayList(); + + //Stack has all the nodes of the current SCC + while (w != u) { + w = st.pop(); + scc.add(w); + isInStack[w] = false; + } + SCClist.add(scc); + } + } + +} diff --git a/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java b/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java new file mode 100644 index 000000000000..2166b2ef6c68 --- /dev/null +++ b/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java @@ -0,0 +1,72 @@ +package com.thealgorithms.datastructures.graphs; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class TarjansAlgorithmTest { + + TarjansAlgorithm tarjansAlgo = new TarjansAlgorithm(); + + @Test + public void findStronglyConnectedComps(){ + var v = 5; + var graph = new ArrayList>(); + for (int i = 0; i < v; i++) { + graph.add(new ArrayList<>()); + } + graph.get(0).add(1); + graph.get(1).add(2); + graph.get(2).add(0); + graph.get(1).add(3); + graph.get(3).add(4); + + var actualResult = tarjansAlgo.stronglyConnectedComponents(v, graph); + /* + Expected result: + 0, 1, 2 + 3 + 4 + */ + List> expectedResult = new ArrayList<>(); + + expectedResult.add(Arrays.asList(4)); + expectedResult.add(Arrays.asList(3)); + expectedResult.add(Arrays.asList(2, 1, 0)); + assertTrue(expectedResult.equals(actualResult)); + } + + @Test + public void findStronglyConnectedCompsShouldGetSingleNodes() { + //Create a adjacency list of graph + var n = 8; + var adjList = new ArrayList>(n); + + for (int i = 0; i < n; i++) { + adjList.add(new ArrayList<>()); + } + + adjList.get(0).add(1); + adjList.get(1).add(2); + adjList.get(2).add(3); + adjList.get(3).add(4); + adjList.get(4).add(5); + adjList.get(5).add(6); + adjList.get(6).add(7); + adjList.get(7).add(0); + + List> actualResult = tarjansAlgo.stronglyConnectedComponents(n, adjList); + List> expectedResult = new ArrayList<>(); + /* + Expected result: + 7, 6, 5, 4, 3, 2, 1, 0 + */ + expectedResult.add(Arrays.asList(7, 6, 5, 4, 3, 2, 1, 0)); + assertTrue(expectedResult.equals(actualResult)); + } + +} From 76b250faa10be7e33738c512a607ea99ccb18884 Mon Sep 17 00:00:00 2001 From: Shivanagouda S A Date: Sat, 11 Feb 2023 14:25:19 +0530 Subject: [PATCH 8/8] Fixing mistake in comments --- .../thealgorithms/datastructures/graphs/TarjansAlgorithm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java index e452b379acc8..18e43c49daf2 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithm.java @@ -6,7 +6,7 @@ import java.util.Stack; /** - * Java program that implements Kosaraju Algorithm. + * Java program that implements Tarjan's Algorithm. * @author Shivanagouda S A (https://github.com/shivu2002a) * */