|
| 1 | +package com.thealgorithms.graph; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +/** |
| 13 | + * Unit tests for {@link TarjanBridges}. |
| 14 | + * |
| 15 | + * <p>Tests cover a wide range of graph configurations including simple graphs, |
| 16 | + * cycles, trees, disconnected components, multigraph-like structures, and |
| 17 | + * various edge cases to ensure correct bridge detection.</p> |
| 18 | + */ |
| 19 | +class TarjanBridgesTest { |
| 20 | + |
| 21 | + /** |
| 22 | + * Helper to build a symmetric adjacency list for an undirected graph. |
| 23 | + */ |
| 24 | + private static List<List<Integer>> buildGraph(int vertexCount, int[][] edges) { |
| 25 | + List<List<Integer>> adj = new ArrayList<>(); |
| 26 | + for (int i = 0; i < vertexCount; i++) { |
| 27 | + adj.add(new ArrayList<>()); |
| 28 | + } |
| 29 | + for (int[] edge : edges) { |
| 30 | + adj.get(edge[0]).add(edge[1]); |
| 31 | + adj.get(edge[1]).add(edge[0]); |
| 32 | + } |
| 33 | + return adj; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Sorts bridges for deterministic comparison. |
| 38 | + */ |
| 39 | + private static void sortBridges(List<int[]> bridges) { |
| 40 | + bridges.sort(Comparator.comparingInt((int[] a) -> a[0]).thenComparingInt(a -> a[1])); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void testSimpleGraphWithOneBridge() { |
| 45 | + // Graph: 0-1-2-3 where 1-2 is the only bridge |
| 46 | + // 0---1---2---3 |
| 47 | + // | | |
| 48 | + // +-------+ (via 0-2 would make cycle, but not here) |
| 49 | + // Actually: 0-1 in a cycle with 0-1, and 2-3 in a cycle with 2-3 |
| 50 | + // Let's use: 0--1--2 (linear chain). All edges are bridges. |
| 51 | + List<List<Integer>> adj = buildGraph(3, new int[][] {{0, 1}, {1, 2}}); |
| 52 | + List<int[]> bridges = TarjanBridges.findBridges(3, adj); |
| 53 | + sortBridges(bridges); |
| 54 | + assertEquals(2, bridges.size()); |
| 55 | + assertEquals(0, bridges.get(0)[0]); |
| 56 | + assertEquals(1, bridges.get(0)[1]); |
| 57 | + assertEquals(1, bridges.get(1)[0]); |
| 58 | + assertEquals(2, bridges.get(1)[1]); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testCycleGraphHasNoBridges() { |
| 63 | + // Graph: 0-1-2-0 (triangle). No bridges. |
| 64 | + List<List<Integer>> adj = buildGraph(3, new int[][] {{0, 1}, {1, 2}, {2, 0}}); |
| 65 | + List<int[]> bridges = TarjanBridges.findBridges(3, adj); |
| 66 | + assertTrue(bridges.isEmpty()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testTreeGraphAllEdgesAreBridges() { |
| 71 | + // Tree: 0 |
| 72 | + // / \ |
| 73 | + // 1 2 |
| 74 | + // / \ |
| 75 | + // 3 4 |
| 76 | + List<List<Integer>> adj = buildGraph(5, new int[][] {{0, 1}, {0, 2}, {1, 3}, {1, 4}}); |
| 77 | + List<int[]> bridges = TarjanBridges.findBridges(5, adj); |
| 78 | + assertEquals(4, bridges.size()); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testGraphWithMixedBridgesAndCycles() { |
| 83 | + // Graph: |
| 84 | + // 0---1 |
| 85 | + // | | |
| 86 | + // 3---2---4---5 |
| 87 | + // | |
| 88 | + // 6 |
| 89 | + // Cycle: 0-1-2-3-0 (no bridges within) |
| 90 | + // Bridges: 2-4, 4-5, 5-6 |
| 91 | + List<List<Integer>> adj = buildGraph(7, new int[][] { |
| 92 | + {0, 1}, {1, 2}, {2, 3}, {3, 0}, {2, 4}, {4, 5}, {5, 6} |
| 93 | + }); |
| 94 | + List<int[]> bridges = TarjanBridges.findBridges(7, adj); |
| 95 | + sortBridges(bridges); |
| 96 | + assertEquals(3, bridges.size()); |
| 97 | + assertEquals(2, bridges.get(0)[0]); |
| 98 | + assertEquals(4, bridges.get(0)[1]); |
| 99 | + assertEquals(4, bridges.get(1)[0]); |
| 100 | + assertEquals(5, bridges.get(1)[1]); |
| 101 | + assertEquals(5, bridges.get(2)[0]); |
| 102 | + assertEquals(6, bridges.get(2)[1]); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void testDisconnectedGraphWithBridges() { |
| 107 | + // Component 1: 0-1 (bridge) |
| 108 | + // Component 2: 2-3-4-2 (cycle, no bridges) |
| 109 | + List<List<Integer>> adj = buildGraph(5, new int[][] { |
| 110 | + {0, 1}, {2, 3}, {3, 4}, {4, 2} |
| 111 | + }); |
| 112 | + List<int[]> bridges = TarjanBridges.findBridges(5, adj); |
| 113 | + assertEquals(1, bridges.size()); |
| 114 | + assertEquals(0, bridges.get(0)[0]); |
| 115 | + assertEquals(1, bridges.get(0)[1]); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testSingleVertex() { |
| 120 | + List<List<Integer>> adj = buildGraph(1, new int[][] {}); |
| 121 | + List<int[]> bridges = TarjanBridges.findBridges(1, adj); |
| 122 | + assertTrue(bridges.isEmpty()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void testTwoVerticesWithOneEdge() { |
| 127 | + List<List<Integer>> adj = buildGraph(2, new int[][] {{0, 1}}); |
| 128 | + List<int[]> bridges = TarjanBridges.findBridges(2, adj); |
| 129 | + assertEquals(1, bridges.size()); |
| 130 | + assertEquals(0, bridges.get(0)[0]); |
| 131 | + assertEquals(1, bridges.get(0)[1]); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void testEmptyGraph() { |
| 136 | + List<List<Integer>> adj = buildGraph(0, new int[][] {}); |
| 137 | + List<int[]> bridges = TarjanBridges.findBridges(0, adj); |
| 138 | + assertTrue(bridges.isEmpty()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void testIsolatedVertices() { |
| 143 | + // 5 vertices, no edges — all isolated |
| 144 | + List<List<Integer>> adj = buildGraph(5, new int[][] {}); |
| 145 | + List<int[]> bridges = TarjanBridges.findBridges(5, adj); |
| 146 | + assertTrue(bridges.isEmpty()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + void testLargeCycleNoBridges() { |
| 151 | + // Cycle: 0-1-2-3-4-5-6-7-0 |
| 152 | + int n = 8; |
| 153 | + int[][] edges = new int[n][2]; |
| 154 | + for (int i = 0; i < n; i++) { |
| 155 | + edges[i] = new int[] {i, (i + 1) % n}; |
| 156 | + } |
| 157 | + List<List<Integer>> adj = buildGraph(n, edges); |
| 158 | + List<int[]> bridges = TarjanBridges.findBridges(n, adj); |
| 159 | + assertTrue(bridges.isEmpty()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void testComplexGraphWithMultipleCyclesAndBridges() { |
| 164 | + // Two cycles connected by a single bridge edge: |
| 165 | + // Cycle A: 0-1-2-0 |
| 166 | + // Cycle B: 3-4-5-3 |
| 167 | + // Bridge: 2-3 |
| 168 | + List<List<Integer>> adj = buildGraph(6, new int[][] { |
| 169 | + {0, 1}, {1, 2}, {2, 0}, {3, 4}, {4, 5}, {5, 3}, {2, 3} |
| 170 | + }); |
| 171 | + List<int[]> bridges = TarjanBridges.findBridges(6, adj); |
| 172 | + assertEquals(1, bridges.size()); |
| 173 | + assertEquals(2, bridges.get(0)[0]); |
| 174 | + assertEquals(3, bridges.get(0)[1]); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + void testNegativeVertexCountThrowsException() { |
| 179 | + assertThrows(IllegalArgumentException.class, () -> TarjanBridges.findBridges(-1, new ArrayList<>())); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void testNullAdjacencyListThrowsException() { |
| 184 | + assertThrows(IllegalArgumentException.class, () -> TarjanBridges.findBridges(3, null)); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void testMismatchedAdjacencyListSizeThrowsException() { |
| 189 | + List<List<Integer>> adj = buildGraph(2, new int[][] {{0, 1}}); |
| 190 | + assertThrows(IllegalArgumentException.class, () -> TarjanBridges.findBridges(5, adj)); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void testStarGraphAllEdgesAreBridges() { |
| 195 | + // Star graph: center vertex 0 connected to 1, 2, 3, 4 |
| 196 | + List<List<Integer>> adj = buildGraph(5, new int[][] { |
| 197 | + {0, 1}, {0, 2}, {0, 3}, {0, 4} |
| 198 | + }); |
| 199 | + List<int[]> bridges = TarjanBridges.findBridges(5, adj); |
| 200 | + assertEquals(4, bridges.size()); |
| 201 | + } |
| 202 | + |
| 203 | + @Test |
| 204 | + void testBridgeBetweenTwoCycles() { |
| 205 | + // Two squares connected by one bridge: |
| 206 | + // Square 1: 0-1-2-3-0 |
| 207 | + // Square 2: 4-5-6-7-4 |
| 208 | + // Bridge: 3-4 |
| 209 | + List<List<Integer>> adj = buildGraph(8, new int[][] { |
| 210 | + {0, 1}, {1, 2}, {2, 3}, {3, 0}, |
| 211 | + {4, 5}, {5, 6}, {6, 7}, {7, 4}, |
| 212 | + {3, 4} |
| 213 | + }); |
| 214 | + List<int[]> bridges = TarjanBridges.findBridges(8, adj); |
| 215 | + assertEquals(1, bridges.size()); |
| 216 | + assertEquals(3, bridges.get(0)[0]); |
| 217 | + assertEquals(4, bridges.get(0)[1]); |
| 218 | + } |
| 219 | +} |
0 commit comments