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