From ff69b24d74015992230f958fcee1ef5cffd5e628 Mon Sep 17 00:00:00 2001 From: coffee-loves-code-2003 <128959041+coffee-loves-code-2003@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:06:45 +0530 Subject: [PATCH 1/4] Added graoh algorithm --- .../SCC_Naive.java | 81 ++++++++++++++ .../SCC_Optimized.java | 100 ++++++++++++++++++ 2 files changed, 181 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java create mode 100644 src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java new file mode 100644 index 000000000000..c0ef11b84c45 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java @@ -0,0 +1,81 @@ +import java.util.*; +public class SCC_Naive +{ + //Implementing the conventional DFS + public boolean isPath(HashMap> adj_list,int N,int visited[],int current_node,int destination) + { + if(current_node == destination) + { + return true; + } + List lists = adj_list.get(current_node); + int lists_size = lists.size(); + visited[current_node] = 1; + for(int i = 0;i < lists_size;i++) + { + if(visited[lists.get(i)] == 0) + { + if(isPath(adj_list,N,visited,lists.get(i),destination)) + { + return true; + } + + } + } + return false; + } + public Set> getOutput(HashMap> adj_list,int N) + { + Set> SCC_lists = new HashSet<>(); + for(int i = 1;i <= N;i++) + { + List neighbour_vertices = new ArrayList<>(); + for(int j = 1;j <= N;j++) + { + if(i != j) + { + int visited[] = new int[N+1]; + if(isPath(adj_list, N,visited,i,j)) + { + visited = new int[N+1]; + if(isPath(adj_list, N, visited, j, i)) + { + neighbour_vertices.add(j); + } + } + } + else + { + neighbour_vertices.add(i); + + } + } + SCC_lists.add(new ArrayList<>(neighbour_vertices)); + } + return SCC_lists; + } + public static void main(String[] args) { + Scanner rs = new Scanner(System.in); + HashMap> adj_list = new HashMap<>(); + int N = rs.nextInt(); + for(int i = 1;i <= N;i++) + { + adj_list.put(i,new ArrayList<>()); + } + for(int i = 1;i <= N;i++) + { + System.out.println("Number of neighbour vertices"); + int n_vertices = rs.nextInt(); + for(int j = 0;j < n_vertices;j++) + { + int vertices = rs.nextInt(); + List lists = adj_list.get(i); + lists.add(vertices); + } + } + System.out.println(adj_list); + SCC_Naive obj = new SCC_Naive(); + System.out.print(obj.getOutput(adj_list,N)); + rs.close(); + } +} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java new file mode 100644 index 000000000000..01c7151eabf2 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java @@ -0,0 +1,100 @@ +import java.util.*; +public class SCC_Optimized +{ + + public void btrack(HashMap> adj_list,int visited[],Stack dfs_calls_nodes,int current_node) + { + visited[current_node] = 1; + for(int i = 0;i < adj_list.get(current_node).size();i++) + { + if(visited[adj_list.get(current_node).get(i)] == -1) + { + btrack(adj_list, visited, dfs_calls_nodes, current_node); + } + } + dfs_calls_nodes.add(current_node); + } + public void btrack2(HashMap> adj_rev_list,int visited[],int current_node,List new_scc) + { + visited[current_node] = 1; + for(int i = 0;i < adj_rev_list.get(current_node).size();i++) + { + if(visited[adj_rev_list.get(current_node).get(i)] == -1) + { + btrack2(adj_rev_list, visited, current_node,new_scc); + } + } + } + public int getOutput(HashMap> adj_list,int N) + { + int visited[] = new int[N+1]; + Arrays.fill(visited,-1); + Stack dfs_calls_nodes = new Stack(); + for(int i = 0;i < N;i++) + { + if(visited[i]==-1) + { + btrack(adj_list, visited,dfs_calls_nodes,i); + } + } + System.out.println(dfs_calls_nodes); + HashMap> adj_rev_list = new HashMap<>(); + for(int i = 0;i < N;i++) + { + visited[i] = -1; + adj_rev_list.put(i,new ArrayList()); + } + //Reversing the adjacency lists + for(int i = 0;i < N;i++) + { + List lists = adj_list.get(i); + for(Integer num: lists) + { + List list1 = adj_rev_list.get(num); + list1.add(i); + adj_rev_list.put(num,list1); + } + } + //Popping out the stack in the descending order and counting the number of SCC + int strongly_connected_components = 0; + List> sccs = new ArrayList<>(); + while(!dfs_calls_nodes.isEmpty()) + { + int node = dfs_calls_nodes.pop(); + if(visited[node] == -1) + { + ++strongly_connected_components; + List new_scc = new ArrayList<>(); + new_scc.add(node); + btrack2(adj_rev_list, visited, node,new_scc); + sccs.add(new_scc); + } + } + System.out.println(sccs.get(sccs.size()-1).size()); + return strongly_connected_components; + } + public static void main(String[] args) { + Scanner rs = new Scanner(System.in); + HashMap> adj_list = new HashMap<>(); + int N = rs.nextInt(); + for(int i = 0;i < N;i++) + { + adj_list.put(i,new ArrayList<>()); + } + for(int i = 0;i < N;i++) + { + System.out.println("Number of neighbour vertices"); + int n_vertices = rs.nextInt(); + for(int j = 0;j < n_vertices;j++) + { + int vertices = rs.nextInt(); + List lists = adj_list.get(i); + lists.add(vertices); + } + } + System.out.println(adj_list); + SCC_Optimized obj = new SCC_Optimized(); + System.out.print(obj.getOutput(adj_list,N)); + rs.close(); + } +} \ No newline at end of file From a91050a0d8bc5888f10ba7c51f0002559267b4f4 Mon Sep 17 00:00:00 2001 From: coffee-loves-code-2003 <128959041+coffee-loves-code-2003@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:27:40 +0530 Subject: [PATCH 2/4] newly added graph algorithms --- .../{SCC_Naive.java => sccNaive.java} | 2 +- .../SCC_Optimized.java | 100 ------------------ .../sccOptimized.java | 99 +++++++++++++++++ 3 files changed, 100 insertions(+), 101 deletions(-) rename src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/{SCC_Naive.java => sccNaive.java} (99%) delete mode 100644 src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java create mode 100644 src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java similarity index 99% rename from src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java rename to src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java index c0ef11b84c45..912528627968 100644 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/SCC_Naive.java +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java @@ -1,5 +1,5 @@ import java.util.*; -public class SCC_Naive +public class sccNaive { //Implementing the conventional DFS public boolean isPath(HashMap> adj_list,int N,int visited[],int current_node,int destination) diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java deleted file mode 100644 index 01c7151eabf2..000000000000 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/SCC_Optimized.java +++ /dev/null @@ -1,100 +0,0 @@ -import java.util.*; -public class SCC_Optimized -{ - - public void btrack(HashMap> adj_list,int visited[],Stack dfs_calls_nodes,int current_node) - { - visited[current_node] = 1; - for(int i = 0;i < adj_list.get(current_node).size();i++) - { - if(visited[adj_list.get(current_node).get(i)] == -1) - { - btrack(adj_list, visited, dfs_calls_nodes, current_node); - } - } - dfs_calls_nodes.add(current_node); - } - public void btrack2(HashMap> adj_rev_list,int visited[],int current_node,List new_scc) - { - visited[current_node] = 1; - for(int i = 0;i < adj_rev_list.get(current_node).size();i++) - { - if(visited[adj_rev_list.get(current_node).get(i)] == -1) - { - btrack2(adj_rev_list, visited, current_node,new_scc); - } - } - } - public int getOutput(HashMap> adj_list,int N) - { - int visited[] = new int[N+1]; - Arrays.fill(visited,-1); - Stack dfs_calls_nodes = new Stack(); - for(int i = 0;i < N;i++) - { - if(visited[i]==-1) - { - btrack(adj_list, visited,dfs_calls_nodes,i); - } - } - System.out.println(dfs_calls_nodes); - HashMap> adj_rev_list = new HashMap<>(); - for(int i = 0;i < N;i++) - { - visited[i] = -1; - adj_rev_list.put(i,new ArrayList()); - } - //Reversing the adjacency lists - for(int i = 0;i < N;i++) - { - List lists = adj_list.get(i); - for(Integer num: lists) - { - List list1 = adj_rev_list.get(num); - list1.add(i); - adj_rev_list.put(num,list1); - } - } - //Popping out the stack in the descending order and counting the number of SCC - int strongly_connected_components = 0; - List> sccs = new ArrayList<>(); - while(!dfs_calls_nodes.isEmpty()) - { - int node = dfs_calls_nodes.pop(); - if(visited[node] == -1) - { - ++strongly_connected_components; - List new_scc = new ArrayList<>(); - new_scc.add(node); - btrack2(adj_rev_list, visited, node,new_scc); - sccs.add(new_scc); - } - } - System.out.println(sccs.get(sccs.size()-1).size()); - return strongly_connected_components; - } - public static void main(String[] args) { - Scanner rs = new Scanner(System.in); - HashMap> adj_list = new HashMap<>(); - int N = rs.nextInt(); - for(int i = 0;i < N;i++) - { - adj_list.put(i,new ArrayList<>()); - } - for(int i = 0;i < N;i++) - { - System.out.println("Number of neighbour vertices"); - int n_vertices = rs.nextInt(); - for(int j = 0;j < n_vertices;j++) - { - int vertices = rs.nextInt(); - List lists = adj_list.get(i); - lists.add(vertices); - } - } - System.out.println(adj_list); - SCC_Optimized obj = new SCC_Optimized(); - System.out.print(obj.getOutput(adj_list,N)); - rs.close(); - } -} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java new file mode 100644 index 000000000000..af17f5409fdc --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java @@ -0,0 +1,99 @@ +import java.util.*; + +public class sccOptimized { + + public void btrack(HashMap> adjList, int[] visited, Stack dfsCallsNodes, int currentNode) { + visited[currentNode] = 1; + for (int i = 0; i < adjList.get(currentNode).size(); i++) { + int neighbor = adjList.get(currentNode).get(i); + if (visited[neighbor] == -1) { + btrack(adjList, visited, dfsCallsNodes, neighbor); + } + } + dfsCallsNodes.add(currentNode); // Add node after finishing DFS on all neighbors + } + + public void btrack2(HashMap> adjRevList, int[] visited, int currentNode, List newScc) { + visited[currentNode] = 1; + newScc.add(currentNode); // Add node to the current SCC + for (int i = 0; i < adjRevList.get(currentNode).size(); i++) { + int neighbor = adjRevList.get(currentNode).get(i); + if (visited[neighbor] == -1) { + btrack2(adjRevList, visited, neighbor, newScc); + } + } + } + + public int getOutput(HashMap> adjList, int N) { + int[] visited = new int[N]; + Arrays.fill(visited, -1); + Stack dfsCallsNodes = new Stack<>(); + + // First DFS pass to fill the stack with the finishing times of nodes + for (int i = 0; i < N; i++) { + if (visited[i] == -1) { + btrack(adjList, visited, dfsCallsNodes, i); + } + } + + System.out.println("Stack of nodes by finish time: " + dfsCallsNodes); + + // Reverse the graph + HashMap> adjRevList = new HashMap<>(); + for (int i = 0; i < N; i++) { + adjRevList.put(i, new ArrayList<>()); + } + + for (int i = 0; i < N; i++) { + for (int neighbor : adjList.get(i)) { + adjRevList.get(neighbor).add(i); // Reverse edge + } + } + + // Second DFS on the reversed graph + Arrays.fill(visited, -1); + int stronglyConnectedComponents = 0; + List> sccs = new ArrayList<>(); + + while (!dfsCallsNodes.isEmpty()) { + int node = dfsCallsNodes.pop(); + if (visited[node] == -1) { + List newScc = new ArrayList<>(); + btrack2(adjRevList, visited, node, newScc); + sccs.add(newScc); + stronglyConnectedComponents++; + } + } + + // Print the found SCCs + System.out.println("Strongly Connected Components: " + sccs); + return stronglyConnectedComponents; + } + + public static void main(String[] args) { + Scanner rs = new Scanner(System.in); + HashMap> adjList = new HashMap<>(); + int N = rs.nextInt(); // Number of nodes + + // Initialize adjacency list + for (int i = 0; i < N; i++) { + adjList.put(i, new ArrayList<>()); + } + + // Input graph data + for (int i = 0; i < N; i++) { + System.out.println("Number of neighbor vertices for node " + i + ": "); + int nVertices = rs.nextInt(); + for (int j = 0; j < nVertices; j++) { + int neighbor = rs.nextInt(); + adjList.get(i).add(neighbor); + } + } + + System.out.println("Adjacency list: " + adjList); + + SCC_Optimized obj = new SCC_Optimized(); + System.out.println("Number of SCCs: " + obj.getOutput(adjList, N)); + rs.close(); + } +} From 7c4a8ec64eee7df9d3722c049eb7062ca790d24a Mon Sep 17 00:00:00 2001 From: coffee-loves-code-2003 <128959041+coffee-loves-code-2003@users.noreply.github.com> Date: Wed, 2 Oct 2024 17:33:31 +0530 Subject: [PATCH 3/4] newly added algos --- .../sccNaive.java | 2 +- .../sccOptimized.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java index 912528627968..6b3e446d337c 100644 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java @@ -74,7 +74,7 @@ public static void main(String[] args) { } } System.out.println(adj_list); - SCC_Naive obj = new SCC_Naive(); + sccNaive obj = new sccNaive(); System.out.print(obj.getOutput(adj_list,N)); rs.close(); } diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java index af17f5409fdc..907495b94cb8 100644 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java +++ b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java @@ -92,7 +92,7 @@ public static void main(String[] args) { System.out.println("Adjacency list: " + adjList); - SCC_Optimized obj = new SCC_Optimized(); + sccOptimized obj = new sccOptimized(); System.out.println("Number of SCCs: " + obj.getOutput(adjList, N)); rs.close(); } From d3be8d0e8444287b0c106eeedd95051d8592751d Mon Sep 17 00:00:00 2001 From: coffee-loves-code-2003 <128959041+coffee-loves-code-2003@users.noreply.github.com> Date: Wed, 9 Oct 2024 21:56:55 +0530 Subject: [PATCH 4/4] Newly added graph algorithm --- .../sccNaive.java | 81 ------------------- ...stronglyConnectedComponent_Optimized.java} | 51 ++++-------- 2 files changed, 14 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java rename src/main/java/com/thealgorithms/graph/{Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java => stronglyConnectedComponent_Optimized.java} (57%) diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java b/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java deleted file mode 100644 index 6b3e446d337c..000000000000 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Naive Approach/sccNaive.java +++ /dev/null @@ -1,81 +0,0 @@ -import java.util.*; -public class sccNaive -{ - //Implementing the conventional DFS - public boolean isPath(HashMap> adj_list,int N,int visited[],int current_node,int destination) - { - if(current_node == destination) - { - return true; - } - List lists = adj_list.get(current_node); - int lists_size = lists.size(); - visited[current_node] = 1; - for(int i = 0;i < lists_size;i++) - { - if(visited[lists.get(i)] == 0) - { - if(isPath(adj_list,N,visited,lists.get(i),destination)) - { - return true; - } - - } - } - return false; - } - public Set> getOutput(HashMap> adj_list,int N) - { - Set> SCC_lists = new HashSet<>(); - for(int i = 1;i <= N;i++) - { - List neighbour_vertices = new ArrayList<>(); - for(int j = 1;j <= N;j++) - { - if(i != j) - { - int visited[] = new int[N+1]; - if(isPath(adj_list, N,visited,i,j)) - { - visited = new int[N+1]; - if(isPath(adj_list, N, visited, j, i)) - { - neighbour_vertices.add(j); - } - } - } - else - { - neighbour_vertices.add(i); - - } - } - SCC_lists.add(new ArrayList<>(neighbour_vertices)); - } - return SCC_lists; - } - public static void main(String[] args) { - Scanner rs = new Scanner(System.in); - HashMap> adj_list = new HashMap<>(); - int N = rs.nextInt(); - for(int i = 1;i <= N;i++) - { - adj_list.put(i,new ArrayList<>()); - } - for(int i = 1;i <= N;i++) - { - System.out.println("Number of neighbour vertices"); - int n_vertices = rs.nextInt(); - for(int j = 0;j < n_vertices;j++) - { - int vertices = rs.nextInt(); - List lists = adj_list.get(i); - lists.add(vertices); - } - } - System.out.println(adj_list); - sccNaive obj = new sccNaive(); - System.out.print(obj.getOutput(adj_list,N)); - rs.close(); - } -} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java b/src/main/java/com/thealgorithms/graph/stronglyConnectedComponent_Optimized.java similarity index 57% rename from src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java rename to src/main/java/com/thealgorithms/graph/stronglyConnectedComponent_Optimized.java index 907495b94cb8..2c98be12693e 100644 --- a/src/main/java/com/thealgorithms/graph/Connected Components/Strongly Connected Components - Kosaraju Algorithm/sccOptimized.java +++ b/src/main/java/com/thealgorithms/graph/stronglyConnectedComponent_Optimized.java @@ -1,6 +1,10 @@ -import java.util.*; +import java.util.List; +import java.util.Stack; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; -public class sccOptimized { +public class stronglyConnectedComponent_Optimized { public void btrack(HashMap> adjList, int[] visited, Stack dfsCallsNodes, int currentNode) { visited[currentNode] = 1; @@ -10,12 +14,12 @@ public void btrack(HashMap> adjList, int[] visited, Stack btrack(adjList, visited, dfsCallsNodes, neighbor); } } - dfsCallsNodes.add(currentNode); // Add node after finishing DFS on all neighbors + dfsCallsNodes.add(currentNode); } public void btrack2(HashMap> adjRevList, int[] visited, int currentNode, List newScc) { visited[currentNode] = 1; - newScc.add(currentNode); // Add node to the current SCC + newScc.add(currentNode); for (int i = 0; i < adjRevList.get(currentNode).size(); i++) { int neighbor = adjRevList.get(currentNode).get(i); if (visited[neighbor] == -1) { @@ -29,16 +33,16 @@ public int getOutput(HashMap> adjList, int N) { Arrays.fill(visited, -1); Stack dfsCallsNodes = new Stack<>(); - // First DFS pass to fill the stack with the finishing times of nodes + for (int i = 0; i < N; i++) { if (visited[i] == -1) { btrack(adjList, visited, dfsCallsNodes, i); } } - System.out.println("Stack of nodes by finish time: " + dfsCallsNodes); - // Reverse the graph + + HashMap> adjRevList = new HashMap<>(); for (int i = 0; i < N; i++) { adjRevList.put(i, new ArrayList<>()); @@ -46,11 +50,11 @@ public int getOutput(HashMap> adjList, int N) { for (int i = 0; i < N; i++) { for (int neighbor : adjList.get(i)) { - adjRevList.get(neighbor).add(i); // Reverse edge + adjRevList.get(neighbor).add(i); } } - // Second DFS on the reversed graph + Arrays.fill(visited, -1); int stronglyConnectedComponents = 0; List> sccs = new ArrayList<>(); @@ -65,35 +69,8 @@ public int getOutput(HashMap> adjList, int N) { } } - // Print the found SCCs - System.out.println("Strongly Connected Components: " + sccs); + return stronglyConnectedComponents; } - public static void main(String[] args) { - Scanner rs = new Scanner(System.in); - HashMap> adjList = new HashMap<>(); - int N = rs.nextInt(); // Number of nodes - - // Initialize adjacency list - for (int i = 0; i < N; i++) { - adjList.put(i, new ArrayList<>()); - } - - // Input graph data - for (int i = 0; i < N; i++) { - System.out.println("Number of neighbor vertices for node " + i + ": "); - int nVertices = rs.nextInt(); - for (int j = 0; j < nVertices; j++) { - int neighbor = rs.nextInt(); - adjList.get(i).add(neighbor); - } - } - - System.out.println("Adjacency list: " + adjList); - - sccOptimized obj = new sccOptimized(); - System.out.println("Number of SCCs: " + obj.getOutput(adjList, N)); - rs.close(); - } }