From 711b1fe5b0f2d01d8fc9cf17ec0382b5573cabba Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:26:30 +0530 Subject: [PATCH 01/12] Create Bridges.java --- .../java/com/thealgorithms/graph/Bridges.java | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/Bridges.java diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java new file mode 100644 index 000000000000..42c5f1db9114 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -0,0 +1,137 @@ +```java +package com.thealgorithms.graph; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +/** + * Finds bridges in an undirected graph using depth-first search. + * + *

A bridge is an edge whose removal increases the number of connected + * components in the graph. + * + *

Vertices are numbered from 0 to {@code vertexCount - 1}. + * + *

Time complexity: O(V + E), where V is the number of vertices + * and E is the number of edges. + */ +public final class Bridges { + + private Bridges() { + // Utility class; do not instantiate. + } + + /** + * Returns all bridges in an undirected graph. + * + * @param vertexCount number of vertices + * @param edges array of edges, with each edge represented as {u, v} + * @return list of bridges, with each bridge represented as {u, v} + * @throws IllegalArgumentException if the vertex count is negative, + * or an edge is null, malformed, + * or references an invalid vertex + */ + public static List findBridges(int vertexCount, int[][] edges) { + if (vertexCount < 0) { + throw new IllegalArgumentException("Vertex count cannot be negative."); + } + + if (edges == null) { + throw new IllegalArgumentException("Edges cannot be null."); + } + + List> adjacencyList = new ArrayList<>(vertexCount); + + for (int i = 0; i < vertexCount; i++) { + adjacencyList.add(new ArrayList<>()); + } + + for (int[] edge : edges) { + if (edge == null || edge.length != 2) { + throw new IllegalArgumentException( + "Each edge must contain exactly two vertices." + ); + } + + int u = edge[0]; + int v = edge[1]; + + if (u < 0 || u >= vertexCount || v < 0 || v >= vertexCount) { + throw new IllegalArgumentException( + "Edge contains an invalid vertex." + ); + } + + adjacencyList.get(u).add(v); + adjacencyList.get(v).add(u); + } + + int[] discoveryTime = new int[vertexCount]; + int[] low = new int[vertexCount]; + Arrays.fill(discoveryTime, -1); + + List bridges = new ArrayList<>(); + int[] timer = {0}; + + for (int vertex = 0; vertex < vertexCount; vertex++) { + if (discoveryTime[vertex] == -1) { + dfs( + vertex, + -1, + adjacencyList, + discoveryTime, + low, + timer, + bridges + ); + } + } + + return bridges; + } + + private static void dfs( + int vertex, + int parent, + List> adjacencyList, + int[] discoveryTime, + int[] low, + int[] timer, + List bridges + ) { + discoveryTime[vertex] = timer[0]; + low[vertex] = timer[0]; + timer[0]++; + + for (int neighbor : adjacencyList.get(vertex)) { + if (neighbor == parent) { + continue; + } + + if (discoveryTime[neighbor] != -1) { + low[vertex] = Math.min(low[vertex], discoveryTime[neighbor]); + } else { + dfs( + neighbor, + vertex, + adjacencyList, + discoveryTime, + low, + timer, + bridges + ); + + low[vertex] = Math.min(low[vertex], low[neighbor]); + + if (low[neighbor] > discoveryTime[vertex]) { + bridges.add(new int[] { + Math.min(vertex, neighbor), + Math.max(vertex, neighbor) + }); + } + } + } + } +} +``` From d3886e180c96bff0f8eeb72c423aa3d23b0e68ca Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:36:50 +0530 Subject: [PATCH 02/12] Update Bridges.java --- src/main/java/com/thealgorithms/graph/Bridges.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java index 42c5f1db9114..82da91e1f8e7 100644 --- a/src/main/java/com/thealgorithms/graph/Bridges.java +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -1,4 +1,4 @@ -```java + package com.thealgorithms.graph; import java.util.ArrayList; @@ -134,4 +134,4 @@ private static void dfs( } } } -``` + From b5149b07413b70c0d9fa4adb4df6b36abaddfe5c Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:47:43 +0530 Subject: [PATCH 03/12] Update Bridges.java --- .../java/com/thealgorithms/graph/Bridges.java | 68 ++++++------------- 1 file changed, 19 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java index 82da91e1f8e7..5ee78f8258b8 100644 --- a/src/main/java/com/thealgorithms/graph/Bridges.java +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -1,4 +1,3 @@ - package com.thealgorithms.graph; import java.util.ArrayList; @@ -15,6 +14,8 @@ * *

Time complexity: O(V + E), where V is the number of vertices * and E is the number of edges. + * + *

Reference: https://en.wikipedia.org/wiki/Bridge_(graph_theory) */ public final class Bridges { @@ -41,30 +42,25 @@ public static List findBridges(int vertexCount, int[][] edges) { throw new IllegalArgumentException("Edges cannot be null."); } - List> adjacencyList = new ArrayList<>(vertexCount); - + List> adjacencyList = new ArrayList<>(vertexCount); for (int i = 0; i < vertexCount; i++) { adjacencyList.add(new ArrayList<>()); } - for (int[] edge : edges) { + for (int edgeId = 0; edgeId < edges.length; edgeId++) { + int[] edge = edges[edgeId]; if (edge == null || edge.length != 2) { - throw new IllegalArgumentException( - "Each edge must contain exactly two vertices." - ); + throw new IllegalArgumentException("Each edge must contain exactly two vertices."); } int u = edge[0]; int v = edge[1]; - if (u < 0 || u >= vertexCount || v < 0 || v >= vertexCount) { - throw new IllegalArgumentException( - "Edge contains an invalid vertex." - ); + throw new IllegalArgumentException("Edge contains an invalid vertex."); } - adjacencyList.get(u).add(v); - adjacencyList.get(v).add(u); + adjacencyList.get(u).add(new int[] {u, edgeId}); + adjacencyList.get(v).add(new int[] {v, edgeId}); } int[] discoveryTime = new int[vertexCount]; @@ -76,62 +72,36 @@ public static List findBridges(int vertexCount, int[][] edges) { for (int vertex = 0; vertex < vertexCount; vertex++) { if (discoveryTime[vertex] == -1) { - dfs( - vertex, - -1, - adjacencyList, - discoveryTime, - low, - timer, - bridges - ); + dfs(vertex, -1, adjacencyList, discoveryTime, low, timer, bridges); } } return bridges; } - private static void dfs( - int vertex, - int parent, - List> adjacencyList, - int[] discoveryTime, - int[] low, - int[] timer, - List bridges - ) { + private static void dfs(int vertex, int parentEdgeId, List> adjacencyList, int[] discoveryTime, int[] low, int[] timer, List bridges) { discoveryTime[vertex] = timer[0]; - low[vertex] = timer[0]; - timer[0]++; + low[vertex] = timer[0]++; + + for (int[] adjacent : adjacencyList.get(vertex)) { + int neighbor = adjacent[0]; + int edgeId = adjacent[1]; - for (int neighbor : adjacencyList.get(vertex)) { - if (neighbor == parent) { + if (edgeId == parentEdgeId) { continue; } if (discoveryTime[neighbor] != -1) { low[vertex] = Math.min(low[vertex], discoveryTime[neighbor]); } else { - dfs( - neighbor, - vertex, - adjacencyList, - discoveryTime, - low, - timer, - bridges - ); + dfs(neighbor, edgeId, adjacencyList, discoveryTime, low, timer, bridges); low[vertex] = Math.min(low[vertex], low[neighbor]); if (low[neighbor] > discoveryTime[vertex]) { - bridges.add(new int[] { - Math.min(vertex, neighbor), - Math.max(vertex, neighbor) - }); + bridges.add(new int[] {Math.min(vertex, neighbor), Math.max(vertex, neighbor)}); } } } } } - From 9f54bcc89dff75c3685fe78a3ac1a1c52503209a Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 17:56:02 +0530 Subject: [PATCH 04/12] Create BridgesTest.java --- .../com/thealgorithms/graph/BridgesTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/main/java/com/thealgorithms/graph/BridgesTest.java diff --git a/src/main/java/com/thealgorithms/graph/BridgesTest.java b/src/main/java/com/thealgorithms/graph/BridgesTest.java new file mode 100644 index 000000000000..212a4f62ebe3 --- /dev/null +++ b/src/main/java/com/thealgorithms/graph/BridgesTest.java @@ -0,0 +1,102 @@ +package com.thealgorithms.graph; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class BridgesTest { + + @Test + void testTriangleHasNoBridges() { + int[][] edges = {{0, 1}, {1, 2}, {2, 0}}; + + List result = Bridges.findBridges(3, edges); + + assertEquals(0, result.size()); + } + + @Test + void testChainHasTwoBridges() { + int[][] edges = {{0, 1}, {1, 2}}; + + List result = Bridges.findBridges(3, edges); + + assertEquals(2, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + assertArrayEquals(new int[] {1, 2}, result.get(1)); + } + + @Test + void testDisconnectedGraph() { + int[][] edges = {{0, 1}, {2, 3}, {3, 4}}; + + List result = Bridges.findBridges(5, edges); + + assertEquals(3, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + assertArrayEquals(new int[] {2, 3}, result.get(1)); + assertArrayEquals(new int[] {3, 4}, result.get(2)); + } + + @Test + void testEmptyGraph() { + List result = Bridges.findBridges(4, new int[0][]); + + assertEquals(0, result.size()); + } + + @Test + void testParallelEdgesAreNotBridges() { + int[][] edges = {{0, 1}, {0, 1}}; + + List result = Bridges.findBridges(2, edges); + + assertEquals(0, result.size()); + } + + @Test + void testSelfLoopIsNotBridge() { + int[][] edges = {{0, 0}}; + + List result = Bridges.findBridges(1, edges); + + assertEquals(0, result.size()); + } + + @Test + void testNegativeVertexCountThrowsException() { + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(-1, new int[0][])); + } + + @Test + void testNullEdgesThrowsException() { + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, null)); + } + + @Test + void testMalformedEdgeThrowsException() { + int[][] edges = {{0}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges)); + } + + @Test + void testInvalidVertexThrowsException() { + int[][] edges = {{0, 2}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges)); + } +} From 7918aad01d17dfe47a62f5362f76b45b866410ee Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:02:36 +0530 Subject: [PATCH 05/12] Create BridgesTest.java Move BridgesTest to test source directory --- .../com/thealgorithms/graph/BridgesTest.java | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 src/test/java/com/thealgorithms/graph/BridgesTest.java diff --git a/src/test/java/com/thealgorithms/graph/BridgesTest.java b/src/test/java/com/thealgorithms/graph/BridgesTest.java new file mode 100644 index 000000000000..212a4f62ebe3 --- /dev/null +++ b/src/test/java/com/thealgorithms/graph/BridgesTest.java @@ -0,0 +1,102 @@ +package com.thealgorithms.graph; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Test; + +class BridgesTest { + + @Test + void testTriangleHasNoBridges() { + int[][] edges = {{0, 1}, {1, 2}, {2, 0}}; + + List result = Bridges.findBridges(3, edges); + + assertEquals(0, result.size()); + } + + @Test + void testChainHasTwoBridges() { + int[][] edges = {{0, 1}, {1, 2}}; + + List result = Bridges.findBridges(3, edges); + + assertEquals(2, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + assertArrayEquals(new int[] {1, 2}, result.get(1)); + } + + @Test + void testDisconnectedGraph() { + int[][] edges = {{0, 1}, {2, 3}, {3, 4}}; + + List result = Bridges.findBridges(5, edges); + + assertEquals(3, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + assertArrayEquals(new int[] {2, 3}, result.get(1)); + assertArrayEquals(new int[] {3, 4}, result.get(2)); + } + + @Test + void testEmptyGraph() { + List result = Bridges.findBridges(4, new int[0][]); + + assertEquals(0, result.size()); + } + + @Test + void testParallelEdgesAreNotBridges() { + int[][] edges = {{0, 1}, {0, 1}}; + + List result = Bridges.findBridges(2, edges); + + assertEquals(0, result.size()); + } + + @Test + void testSelfLoopIsNotBridge() { + int[][] edges = {{0, 0}}; + + List result = Bridges.findBridges(1, edges); + + assertEquals(0, result.size()); + } + + @Test + void testNegativeVertexCountThrowsException() { + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(-1, new int[0][])); + } + + @Test + void testNullEdgesThrowsException() { + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, null)); + } + + @Test + void testMalformedEdgeThrowsException() { + int[][] edges = {{0}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges)); + } + + @Test + void testInvalidVertexThrowsException() { + int[][] edges = {{0, 2}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges)); + } +} From 91b3d76e84b1b2672b16718993cbdfb2739029b1 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:17:48 +0530 Subject: [PATCH 06/12] Delete src/main/java/com/thealgorithms/graph/BridgesTest.java --- .../com/thealgorithms/graph/BridgesTest.java | 102 ------------------ 1 file changed, 102 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/graph/BridgesTest.java diff --git a/src/main/java/com/thealgorithms/graph/BridgesTest.java b/src/main/java/com/thealgorithms/graph/BridgesTest.java deleted file mode 100644 index 212a4f62ebe3..000000000000 --- a/src/main/java/com/thealgorithms/graph/BridgesTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.thealgorithms.graph; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import org.junit.jupiter.api.Test; - -class BridgesTest { - - @Test - void testTriangleHasNoBridges() { - int[][] edges = {{0, 1}, {1, 2}, {2, 0}}; - - List result = Bridges.findBridges(3, edges); - - assertEquals(0, result.size()); - } - - @Test - void testChainHasTwoBridges() { - int[][] edges = {{0, 1}, {1, 2}}; - - List result = Bridges.findBridges(3, edges); - - assertEquals(2, result.size()); - assertArrayEquals(new int[] {0, 1}, result.get(0)); - assertArrayEquals(new int[] {1, 2}, result.get(1)); - } - - @Test - void testDisconnectedGraph() { - int[][] edges = {{0, 1}, {2, 3}, {3, 4}}; - - List result = Bridges.findBridges(5, edges); - - assertEquals(3, result.size()); - assertArrayEquals(new int[] {0, 1}, result.get(0)); - assertArrayEquals(new int[] {2, 3}, result.get(1)); - assertArrayEquals(new int[] {3, 4}, result.get(2)); - } - - @Test - void testEmptyGraph() { - List result = Bridges.findBridges(4, new int[0][]); - - assertEquals(0, result.size()); - } - - @Test - void testParallelEdgesAreNotBridges() { - int[][] edges = {{0, 1}, {0, 1}}; - - List result = Bridges.findBridges(2, edges); - - assertEquals(0, result.size()); - } - - @Test - void testSelfLoopIsNotBridge() { - int[][] edges = {{0, 0}}; - - List result = Bridges.findBridges(1, edges); - - assertEquals(0, result.size()); - } - - @Test - void testNegativeVertexCountThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(-1, new int[0][])); - } - - @Test - void testNullEdgesThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, null)); - } - - @Test - void testMalformedEdgeThrowsException() { - int[][] edges = {{0}}; - - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges)); - } - - @Test - void testInvalidVertexThrowsException() { - int[][] edges = {{0, 2}}; - - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges)); - } -} From 6a21425ab616228f8166a57e1b7ab7de0b92ece8 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:22:39 +0530 Subject: [PATCH 07/12] Update BridgesTest.java --- .../com/thealgorithms/graph/BridgesTest.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/thealgorithms/graph/BridgesTest.java b/src/test/java/com/thealgorithms/graph/BridgesTest.java index 212a4f62ebe3..f9ebdff14c90 100644 --- a/src/test/java/com/thealgorithms/graph/BridgesTest.java +++ b/src/test/java/com/thealgorithms/graph/BridgesTest.java @@ -4,8 +4,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.Test; @@ -70,33 +68,25 @@ void testSelfLoopIsNotBridge() { @Test void testNegativeVertexCountThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(-1, new int[0][])); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(-1, new int[0][])); } @Test void testNullEdgesThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, null)); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, null)); } @Test void testMalformedEdgeThrowsException() { int[][] edges = {{0}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges)); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testInvalidVertexThrowsException() { int[][] edges = {{0, 2}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges)); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } } From c5b315f6c0a11c62fa2a0e7c8e0fcb53f6bdd3e9 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:28:50 +0530 Subject: [PATCH 08/12] Update Bridges.java --- .../java/com/thealgorithms/graph/Bridges.java | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java index 5ee78f8258b8..6185cca17c6f 100644 --- a/src/main/java/com/thealgorithms/graph/Bridges.java +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -59,8 +59,8 @@ public static List findBridges(int vertexCount, int[][] edges) { throw new IllegalArgumentException("Edge contains an invalid vertex."); } - adjacencyList.get(u).add(new int[] {u, edgeId}); - adjacencyList.get(v).add(new int[] {v, edgeId}); + adjacencyList.get(u).add(new int[] {v, edgeId}); + adjacencyList.get(v).add(new int[] {u, edgeId}); } int[] discoveryTime = new int[vertexCount]; @@ -79,7 +79,14 @@ public static List findBridges(int vertexCount, int[][] edges) { return bridges; } - private static void dfs(int vertex, int parentEdgeId, List> adjacencyList, int[] discoveryTime, int[] low, int[] timer, List bridges) { + private static void dfs( + int vertex, + int parentEdgeId, + List> adjacencyList, + int[] discoveryTime, + int[] low, + int[] timer, + List bridges) { discoveryTime[vertex] = timer[0]; low[vertex] = timer[0]++; @@ -94,12 +101,22 @@ private static void dfs(int vertex, int parentEdgeId, List> adjacenc if (discoveryTime[neighbor] != -1) { low[vertex] = Math.min(low[vertex], discoveryTime[neighbor]); } else { - dfs(neighbor, edgeId, adjacencyList, discoveryTime, low, timer, bridges); + dfs( + neighbor, + edgeId, + adjacencyList, + discoveryTime, + low, + timer, + bridges); low[vertex] = Math.min(low[vertex], low[neighbor]); if (low[neighbor] > discoveryTime[vertex]) { - bridges.add(new int[] {Math.min(vertex, neighbor), Math.max(vertex, neighbor)}); + bridges.add( + new int[] { + Math.min(vertex, neighbor), Math.max(vertex, neighbor) + }); } } } From 28877aee5b8b385585d435c7c4a621cd72cf9129 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:32:28 +0530 Subject: [PATCH 09/12] Update Bridges.java --- .../java/com/thealgorithms/graph/Bridges.java | 23 +++---------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java index 6185cca17c6f..ff7aaa26234e 100644 --- a/src/main/java/com/thealgorithms/graph/Bridges.java +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -79,14 +79,7 @@ public static List findBridges(int vertexCount, int[][] edges) { return bridges; } - private static void dfs( - int vertex, - int parentEdgeId, - List> adjacencyList, - int[] discoveryTime, - int[] low, - int[] timer, - List bridges) { + private static void dfs(int vertex, int parentEdgeId, List> adjacencyList, int[] discoveryTime, int[] low, int[] timer, List bridges) { discoveryTime[vertex] = timer[0]; low[vertex] = timer[0]++; @@ -101,22 +94,12 @@ private static void dfs( if (discoveryTime[neighbor] != -1) { low[vertex] = Math.min(low[vertex], discoveryTime[neighbor]); } else { - dfs( - neighbor, - edgeId, - adjacencyList, - discoveryTime, - low, - timer, - bridges); + dfs(neighbor, edgeId, adjacencyList, discoveryTime, low, timer, bridges); low[vertex] = Math.min(low[vertex], low[neighbor]); if (low[neighbor] > discoveryTime[vertex]) { - bridges.add( - new int[] { - Math.min(vertex, neighbor), Math.max(vertex, neighbor) - }); + bridges.add(new int[] {Math.min(vertex, neighbor), Math.max(vertex, neighbor)}); } } } From 06e717e5f4576a877174f1c6e292da589d9dcc28 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:39:26 +0530 Subject: [PATCH 10/12] Update Bridges.java --- src/main/java/com/thealgorithms/graph/Bridges.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/thealgorithms/graph/Bridges.java b/src/main/java/com/thealgorithms/graph/Bridges.java index ff7aaa26234e..6f64403691db 100644 --- a/src/main/java/com/thealgorithms/graph/Bridges.java +++ b/src/main/java/com/thealgorithms/graph/Bridges.java @@ -76,6 +76,11 @@ public static List findBridges(int vertexCount, int[][] edges) { } } + bridges.sort((first, second) -> { + int comparison = Integer.compare(first[0], second[0]); + return comparison != 0 ? comparison : Integer.compare(first[1], second[1]); + }); + return bridges; } From 423cc3a6a825533f05a0c797018fea2e3af433da Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:17:47 +0530 Subject: [PATCH 11/12] Update BridgesTest.java --- .../com/thealgorithms/graph/BridgesTest.java | 111 +++++++++++++++++- 1 file changed, 106 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/graph/BridgesTest.java b/src/test/java/com/thealgorithms/graph/BridgesTest.java index f9ebdff14c90..130d6c53bb5b 100644 --- a/src/test/java/com/thealgorithms/graph/BridgesTest.java +++ b/src/test/java/com/thealgorithms/graph/BridgesTest.java @@ -48,6 +48,13 @@ void testEmptyGraph() { assertEquals(0, result.size()); } + @Test + void testZeroVertices() { + List result = Bridges.findBridges(0, new int[0][]); + + assertEquals(0, result.size()); + } + @Test void testParallelEdgesAreNotBridges() { int[][] edges = {{0, 1}, {0, 1}}; @@ -66,27 +73,121 @@ void testSelfLoopIsNotBridge() { assertEquals(0, result.size()); } + @Test + void testSingleEdgeIsBridge() { + int[][] edges = {{0, 1}}; + + List result = Bridges.findBridges(2, edges); + + assertEquals(1, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + } + + @Test + void testCycleWithTail() { + int[][] edges = {{0, 1}, {1, 2}, {2, 0}, {2, 3}}; + + List result = Bridges.findBridges(4, edges); + + assertEquals(1, result.size()); + assertArrayEquals(new int[] {2, 3}, result.get(0)); + } + @Test void testNegativeVertexCountThrowsException() { - assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(-1, new int[0][])); + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(-1, new int[0][]) + ); } @Test void testNullEdgesThrowsException() { - assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, null)); + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, null) + ); } @Test void testMalformedEdgeThrowsException() { int[][] edges = {{0}}; - assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); + } + + @Test + void testEdgeWithTooManyVerticesThrowsException() { + int[][] edges = {{0, 1, 2}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(3, edges) + ); + } + + @Test + void testNullEdgeThrowsException() { + int[][] edges = {null}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); + } + + @Test + void testNegativeFirstVertexThrowsException() { + int[][] edges = {{-1, 1}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); + } + + @Test + void testNegativeSecondVertexThrowsException() { + int[][] edges = {{0, -1}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); } @Test - void testInvalidVertexThrowsException() { + void testFirstVertexOutOfRangeThrowsException() { + int[][] edges = {{2, 1}}; + + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); + } + + @Test + void testSecondVertexOutOfRangeThrowsException() { int[][] edges = {{0, 2}}; - assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); + assertThrows( + IllegalArgumentException.class, + () -> Bridges.findBridges(2, edges) + ); + } + + @Test + void testBridgesAreSortedByBothEndpoints() { + int[][] edges = {{2, 3}, {0, 2}, {0, 1}}; + + List result = Bridges.findBridges(4, edges); + + assertEquals(3, result.size()); + assertArrayEquals(new int[] {0, 1}, result.get(0)); + assertArrayEquals(new int[] {0, 2}, result.get(1)); + assertArrayEquals(new int[] {2, 3}, result.get(2)); } } From a6f7c26501a9a2ba12f0dcea327202a2bbf434c0 Mon Sep 17 00:00:00 2001 From: Rajat Tiwari <129544468+rajattiwariind@users.noreply.github.com> Date: Mon, 26 Oct 2026 12:21:23 +0530 Subject: [PATCH 12/12] Update BridgesTest.java --- .../com/thealgorithms/graph/BridgesTest.java | 45 ++++--------------- 1 file changed, 9 insertions(+), 36 deletions(-) diff --git a/src/test/java/com/thealgorithms/graph/BridgesTest.java b/src/test/java/com/thealgorithms/graph/BridgesTest.java index 130d6c53bb5b..7a3774c9aee2 100644 --- a/src/test/java/com/thealgorithms/graph/BridgesTest.java +++ b/src/test/java/com/thealgorithms/graph/BridgesTest.java @@ -95,88 +95,61 @@ void testCycleWithTail() { @Test void testNegativeVertexCountThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(-1, new int[0][]) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(-1, new int[0][])); } @Test void testNullEdgesThrowsException() { - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, null) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, null)); } @Test void testMalformedEdgeThrowsException() { int[][] edges = {{0}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testEdgeWithTooManyVerticesThrowsException() { int[][] edges = {{0, 1, 2}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(3, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(3, edges)); } @Test void testNullEdgeThrowsException() { int[][] edges = {null}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testNegativeFirstVertexThrowsException() { int[][] edges = {{-1, 1}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testNegativeSecondVertexThrowsException() { int[][] edges = {{0, -1}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testFirstVertexOutOfRangeThrowsException() { int[][] edges = {{2, 1}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test void testSecondVertexOutOfRangeThrowsException() { int[][] edges = {{0, 2}}; - assertThrows( - IllegalArgumentException.class, - () -> Bridges.findBridges(2, edges) - ); + assertThrows(IllegalArgumentException.class, () -> Bridges.findBridges(2, edges)); } @Test