|
| 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