Skip to content

Commit a89b6cf

Browse files
added
1 parent eee970c commit a89b6cf

File tree

2 files changed

+29
-16
lines changed

2 files changed

+29
-16
lines changed

src/main/java/com/thealgorithms/graph/StronglyConnectedComponentOptimized.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@ public class StronglyConnectedComponentOptimized {
1515

1616
public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack<Integer> dfsCallsNodes, int currentNode) {
1717
visited[currentNode] = 1;
18-
for (int neighbor : adjList.get(currentNode)) {
19-
if (visited[neighbor] == -1) {
20-
btrack(adjList, visited, dfsCallsNodes, neighbor);
18+
List<Integer> neighbors = adjList.get(currentNode);
19+
20+
// Check for null before iterating
21+
if (neighbors != null) {
22+
for (int neighbor : neighbors) {
23+
if (visited[neighbor] == -1) {
24+
btrack(adjList, visited, dfsCallsNodes, neighbor);
25+
}
2126
}
2227
}
2328
dfsCallsNodes.add(currentNode);
@@ -26,9 +31,14 @@ public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack
2631
public void btrack2(HashMap<Integer, List<Integer>> adjRevList, int[] visited, int currentNode, List<Integer> newScc) {
2732
visited[currentNode] = 1;
2833
newScc.add(currentNode);
29-
for (int neighbor : adjRevList.get(currentNode)) {
30-
if (visited[neighbor] == -1) {
31-
btrack2(adjRevList, visited, neighbor, newScc);
34+
List<Integer> neighbors = adjRevList.get(currentNode);
35+
36+
// Check for null before iterating
37+
if (neighbors != null) {
38+
for (int neighbor : neighbors) {
39+
if (visited[neighbor] == -1) {
40+
btrack2(adjRevList, visited, neighbor, newScc);
41+
}
3242
}
3343
}
3444
}
@@ -50,8 +60,12 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int n) {
5060
}
5161

5262
for (int i = 0; i < n; i++) {
53-
for (int neighbor : adjList.get(i)) {
54-
adjRevList.get(neighbor).add(i);
63+
List<Integer> neighbors = adjList.get(i);
64+
// Check for null before iterating
65+
if (neighbors != null) {
66+
for (int neighbor : neighbors) {
67+
adjRevList.get(neighbor).add(i);
68+
}
5569
}
5670
}
5771

@@ -69,5 +83,4 @@ public int getOutput(HashMap<Integer, List<Integer>> adjList, int n) {
6983

7084
return stronglyConnectedComponents;
7185
}
72-
7386
}

src/test/java/com/thealgorithms/graph/StronglyConnectedComponentOptimizedTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import org.junit.jupiter.api.BeforeEach;
2-
import org.junit.jupiter.api.Test;
3-
41
import java.util.ArrayList;
52
import java.util.HashMap;
63
import java.util.List;
74

5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
88
import static org.junit.jupiter.api.Assertions.assertEquals;
99

1010
public class StronglyConnectedComponentOptimizedTest {
@@ -17,7 +17,7 @@ public void setUp() {
1717
}
1818

1919
@Test
20-
public void testSingleComponent() {
20+
public void testSingleComponent() {
2121
// Create a simple graph with 3 nodes, all forming one SCC
2222
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
2323
adjList.put(0, new ArrayList<>(List.of(1)));
@@ -31,7 +31,7 @@ public void testSingleComponent() {
3131
}
3232

3333
@Test
34-
public void testTwoComponents() {
34+
public void testTwoComponents() {
3535
// Create a graph with 4 nodes and two SCCs: {0, 1, 2} and {3}
3636
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
3737
adjList.put(0, new ArrayList<>(List.of(1)));
@@ -46,7 +46,7 @@ public void testTwoComponents() {
4646
}
4747

4848
@Test
49-
public void testDisconnectedGraph() {
49+
public void testDisconnectedGraph() {
5050
// Create a graph with 4 nodes that are all disconnected
5151
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
5252
adjList.put(0, new ArrayList<>());
@@ -61,7 +61,7 @@ public void testDisconnectedGraph() {
6161
}
6262

6363
@Test
64-
public void testComplexGraph() {
64+
public void testComplexGraph() {
6565
// Create a more complex graph with multiple SCCs
6666
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
6767
adjList.put(0, new ArrayList<>(List.of(1)));

0 commit comments

Comments
 (0)