Skip to content

Commit 2f9f75a

Browse files
Add StronglyConnectedComponentOptimized (#5825)
1 parent 6006025 commit 2f9f75a

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Stack;
6+
7+
/**
8+
* Finds the strongly connected components in a directed graph.
9+
*
10+
* @param adjList The adjacency list representation of the graph.
11+
* @param n The number of nodes in the graph.
12+
* @return The number of strongly connected components.
13+
*/
14+
public class StronglyConnectedComponentOptimized {
15+
16+
public void btrack(HashMap<Integer, List<Integer>> adjList, int[] visited, Stack<Integer> dfsCallsNodes, int currentNode) {
17+
visited[currentNode] = 1;
18+
List<Integer> neighbors = adjList.get(currentNode);
19+
// Check for null before iterating
20+
if (neighbors != null) {
21+
for (int neighbor : neighbors) {
22+
if (visited[neighbor] == -1) {
23+
btrack(adjList, visited, dfsCallsNodes, neighbor);
24+
}
25+
}
26+
}
27+
dfsCallsNodes.add(currentNode);
28+
}
29+
30+
public void btrack2(HashMap<Integer, List<Integer>> adjRevList, int[] visited, int currentNode, List<Integer> newScc) {
31+
visited[currentNode] = 1;
32+
newScc.add(currentNode);
33+
List<Integer> neighbors = adjRevList.get(currentNode);
34+
// Check for null before iterating
35+
if (neighbors != null) {
36+
for (int neighbor : neighbors) {
37+
if (visited[neighbor] == -1) {
38+
btrack2(adjRevList, visited, neighbor, newScc);
39+
}
40+
}
41+
}
42+
}
43+
44+
public int getOutput(HashMap<Integer, List<Integer>> adjList, int n) {
45+
int[] visited = new int[n];
46+
Arrays.fill(visited, -1);
47+
Stack<Integer> dfsCallsNodes = new Stack<>();
48+
49+
for (int i = 0; i < n; i++) {
50+
if (visited[i] == -1) {
51+
btrack(adjList, visited, dfsCallsNodes, i);
52+
}
53+
}
54+
55+
HashMap<Integer, List<Integer>> adjRevList = new HashMap<>();
56+
for (int i = 0; i < n; i++) {
57+
adjRevList.put(i, new ArrayList<>());
58+
}
59+
60+
for (int i = 0; i < n; i++) {
61+
List<Integer> neighbors = adjList.get(i);
62+
// Check for null before iterating
63+
if (neighbors != null) {
64+
for (int neighbor : neighbors) {
65+
adjRevList.get(neighbor).add(i);
66+
}
67+
}
68+
}
69+
70+
Arrays.fill(visited, -1);
71+
int stronglyConnectedComponents = 0;
72+
73+
while (!dfsCallsNodes.isEmpty()) {
74+
int node = dfsCallsNodes.pop();
75+
if (visited[node] == -1) {
76+
List<Integer> newScc = new ArrayList<>();
77+
btrack2(adjRevList, visited, node, newScc);
78+
stronglyConnectedComponents++;
79+
}
80+
}
81+
82+
return stronglyConnectedComponents;
83+
}
84+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
9+
public class StronglyConnectedComponentOptimizedTest {
10+
11+
private StronglyConnectedComponentOptimized sccOptimized;
12+
13+
@BeforeEach
14+
public void setUp() {
15+
sccOptimized = new StronglyConnectedComponentOptimized();
16+
}
17+
18+
@Test
19+
public void testSingleComponent() {
20+
// Create a simple graph with 3 nodes, all forming one SCC
21+
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
22+
adjList.put(0, new ArrayList<>(List.of(1)));
23+
adjList.put(1, new ArrayList<>(List.of(2)));
24+
adjList.put(2, new ArrayList<>(List.of(0)));
25+
26+
int result = sccOptimized.getOutput(adjList, 3);
27+
28+
// The entire graph is one strongly connected component
29+
assertEquals(1, result, "There should be 1 strongly connected component.");
30+
}
31+
32+
@Test
33+
public void testTwoComponents() {
34+
// Create a graph with 4 nodes and two SCCs: {0, 1, 2} and {3}
35+
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
36+
adjList.put(0, new ArrayList<>(List.of(1)));
37+
adjList.put(1, new ArrayList<>(List.of(2)));
38+
adjList.put(2, new ArrayList<>(List.of(0)));
39+
adjList.put(3, new ArrayList<>());
40+
41+
int result = sccOptimized.getOutput(adjList, 4);
42+
43+
// There are 2 SCCs: {0, 1, 2} and {3}
44+
assertEquals(2, result, "There should be 2 strongly connected components.");
45+
}
46+
47+
@Test
48+
public void testDisconnectedGraph() {
49+
// Create a graph with 4 nodes that are all disconnected
50+
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
51+
adjList.put(0, new ArrayList<>());
52+
adjList.put(1, new ArrayList<>());
53+
adjList.put(2, new ArrayList<>());
54+
adjList.put(3, new ArrayList<>());
55+
56+
int result = sccOptimized.getOutput(adjList, 4);
57+
58+
// Each node is its own strongly connected component
59+
assertEquals(4, result, "There should be 4 strongly connected components.");
60+
}
61+
62+
@Test
63+
public void testComplexGraph() {
64+
// Create a more complex graph with multiple SCCs
65+
HashMap<Integer, List<Integer>> adjList = new HashMap<>();
66+
adjList.put(0, new ArrayList<>(List.of(1)));
67+
adjList.put(1, new ArrayList<>(List.of(2)));
68+
adjList.put(2, new ArrayList<>(List.of(0, 3)));
69+
adjList.put(3, new ArrayList<>(List.of(4)));
70+
adjList.put(4, new ArrayList<>(List.of(5)));
71+
adjList.put(5, new ArrayList<>(List.of(3)));
72+
73+
int result = sccOptimized.getOutput(adjList, 6);
74+
75+
// There are 2 SCCs: {0, 1, 2} and {3, 4, 5}
76+
assertEquals(2, result, "There should be 2 strongly connected components.");
77+
}
78+
}

0 commit comments

Comments
 (0)