Explores a graph level by level outward from a starting node using a + * FIFO queue. Because BFS visits nodes in order of increasing distance, + * it naturally finds the shortest path (fewest edges) between two nodes + * in an unweighted graph. + * + *
Algorithm: + *
Time: O(V + E) + *
Space: O(V)
+ *
+ * @author William Fiset, william.alexandre.fiset@gmail.com
+ */
+package com.williamfiset.algorithms.graphtheory;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.LinkedList;
+import java.util.List;
+
+public class BreadthFirstSearchAdjacencyList {
+
+ public static class Edge {
+ int from, to;
+
+ public Edge(int from, int to) {
+ this.from = from;
+ this.to = to;
+ }
+ }
+
+ private final int n;
+ private final List Time Complexity: O(V + E)
- *
- * @author William Fiset, william.alexandre.fiset@gmail.com
- */
-package com.williamfiset.algorithms.graphtheory;
-
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Deque;
-import java.util.List;
-
-public class BreadthFirstSearchAdjacencyListIterative {
-
- public static class Edge {
- int from, to, cost;
-
- public Edge(int from, int to, int cost) {
- this.from = from;
- this.to = to;
- this.cost = cost;
- }
- }
-
- private int n;
- private Integer[] prev;
- private List Time Complexity: O(V + E)
- *
- * @author William Fiset, william.alexandre.fiset@gmail.com
- */
-package com.williamfiset.algorithms.graphtheory;
-
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Queue;
-
-public class BreadthFirstSearchRecursive {
-
- // Each breadth first search layer gets separated by a DEPTH_TOKEN.
- // DEPTH_TOKENs help count the distance from one node to another because
- // we can increment the depth counter each time a DEPTH_TOKEN is encountered
- public static int DEPTH_TOKEN = -1;
-
- // Computes the eccentricity (distance to furthest node) from the starting node.
- public static int bfs(List Test against HackerEarth online judge at:
+ * A bridge is an edge whose removal disconnects the graph (or increases
+ * the number of connected components). This implementation uses Tarjan's
+ * DFS-based algorithm with low-link values.
+ *
+ * An edge (u, v) is a bridge if no vertex in the subtree rooted at v
+ * (in the DFS tree) has a back edge to u or any ancestor of u:
+ *
+ * Limitation: This implementation assumes a simple graph (no
+ * parallel/multi-edges between the same pair of nodes). If parallel edges exist,
+ * the algorithm may incorrectly report an edge as a bridge even though a second
+ * edge still connects the two nodes. This is because the parent-skip logic
+ * ({@code to == parent}) skips all occurrences of the parent in the
+ * adjacency list, rather than only the single tree edge used to arrive at the
+ * current node.
+ *
+ * Works on disconnected graphs by running DFS from every unvisited node.
+ *
+ * See also: {@link ArticulationPointsAdjacencyList} for finding cut vertices.
+ *
+ * Tested against HackerEarth online judge at:
* https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/tutorial
*
+ * Time: O(V + E)
+ * Space: O(V)
+ *
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.graphtheory;
@@ -15,64 +39,99 @@
public class BridgesAdjacencyList {
- private int n, id;
- private int[] low, ids;
+ private final int n;
+ private final List Test against HackerEarth online judge at:
- * https://www.hackerearth.com/practice/algorithms/graphs/articulation-points-and-bridges/tutorial
- *
- * @author William Fiset, william.alexandre.fiset@gmail.com
- */
-package com.williamfiset.algorithms.graphtheory;
-
-import static java.lang.Math.min;
-
-import java.util.*;
-
-public class BridgesAdjacencyListIterative {
-
- private int n, id;
- private int[] low, ids;
- private boolean solved;
- private boolean[] visited;
- private List Time Complexity: O(V^2)
- *
- * @author William Fiset, william.alexandre.fiset@gmail.com
- */
-package com.williamfiset.algorithms.graphtheory;
-
-import java.util.*;
-
-public class LazyPrimsAdjacencyMatrix {
-
- static class Edge implements Comparable Time Complexity: O(V^2)
- *
- * @author Micah Stairs
- */
-package com.williamfiset.algorithms.graphtheory;
-
-public class TopologicalSortAdjacencyMatrix {
-
- // Performs the topological sort and returns the
- // indexes of the nodes in topological order
- public static int[] topologicalSort(Double[][] adj) {
-
- // Setup
- int n = adj.length;
- boolean[] visited = new boolean[n];
- int[] order = new int[n];
- int index = n - 1;
-
- // Visit each node
- for (int u = 0; u < n; u++) if (!visited[u]) index = visit(adj, visited, order, index, u);
-
- // Return topological sort
- return order;
- }
-
- private static int visit(Double[][] adj, boolean[] visited, int[] order, int index, int u) {
-
- if (visited[u]) return index;
- visited[u] = true;
-
- // Visit all neighbors
- for (int v = 0; v < adj.length; v++)
- if (adj[u][v] != null && !visited[v]) index = visit(adj, visited, order, index, v);
-
- // Place this node at the head of the list
- order[index--] = u;
-
- return index;
- }
-
- // A useful application of the topological sort is to find the shortest path
- // between two nodes in a Directed Acyclic Graph (DAG). Given an adjacency matrix
- // with double valued edge weights this method finds the shortest path from
- // a start node to all other nodes in the graph.
- public static double[] dagShortestPath(Double[][] adj, int start) {
-
- // Set up array used to maintain minimum distances from start
- int n = adj.length;
- double[] dist = new double[n];
- java.util.Arrays.fill(dist, Double.POSITIVE_INFINITY);
- dist[start] = 0.0;
-
- // Process nodes in a topologically sorted order
- for (int u : topologicalSort(adj))
- for (int v = 0; v < n; v++) {
- if (adj[u][v] != null) {
- double newDist = dist[u] + adj[u][v];
- if (newDist < dist[v]) dist[v] = newDist;
- }
- }
-
- // Return minimum distance
- return dist;
- }
-
- // Example usage of topological sort
- public static void main(String[] args) {
-
- final int N = 7;
- Double[][] adjMatrix = new Double[N][N];
-
- adjMatrix[0][1] = 3.0;
- adjMatrix[0][2] = 2.0;
- adjMatrix[0][5] = 3.0;
-
- adjMatrix[1][3] = 1.0;
- adjMatrix[1][2] = 6.0;
-
- adjMatrix[2][3] = 1.0;
- adjMatrix[2][4] = 10.0;
-
- adjMatrix[3][4] = 5.0;
-
- adjMatrix[5][4] = 7.0;
-
- int[] ordering = topologicalSort(adjMatrix);
-
- // Prints: [6, 0, 5, 1, 2, 3, 4]
- System.out.println(java.util.Arrays.toString(ordering));
-
- // Find the shortest path from 0 to all other nodes
- double[] dists = dagShortestPath(adjMatrix, 0);
-
- // Find the distance from 0 to 4 which is 8.0
- System.out.println(dists[4]);
-
- // Finds the shortest path from 0 to 6
- // prints Infinity (6 is not reachable!)
- System.out.println(dists[6]);
- }
-}
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/BUILD b/src/test/java/com/williamfiset/algorithms/graphtheory/BUILD
index da8231b2c..6a610086b 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/BUILD
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/BUILD
@@ -46,24 +46,13 @@ java_test(
deps = TEST_DEPS,
)
-# bazel test //src/test/java/com/williamfiset/algorithms/graphtheory:BreadthFirstSearchAdjacencyListIterativeTest
+# bazel test //src/test/java/com/williamfiset/algorithms/graphtheory:BreadthFirstSearchAdjacencyListTest
java_test(
- name = "BreadthFirstSearchAdjacencyListIterativeTest",
- srcs = ["BreadthFirstSearchAdjacencyListIterativeTest.java"],
+ name = "BreadthFirstSearchAdjacencyListTest",
+ srcs = ["BreadthFirstSearchAdjacencyListTest.java"],
main_class = "org.junit.platform.console.ConsoleLauncher",
use_testrunner = False,
- args = ["--select-class=com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListIterativeTest"],
- runtime_deps = JUNIT5_RUNTIME_DEPS,
- deps = TEST_DEPS,
-)
-
-# bazel test //src/test/java/com/williamfiset/algorithms/graphtheory:BridgesAdjacencyListIterativeTest
-java_test(
- name = "BridgesAdjacencyListIterativeTest",
- srcs = ["BridgesAdjacencyListIterativeTest.java"],
- main_class = "org.junit.platform.console.ConsoleLauncher",
- use_testrunner = False,
- args = ["--select-class=com.williamfiset.algorithms.graphtheory.BridgesAdjacencyListIterativeTest"],
+ args = ["--select-class=com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListTest"],
runtime_deps = JUNIT5_RUNTIME_DEPS,
deps = TEST_DEPS,
)
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterativeTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListTest.java
similarity index 62%
rename from src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterativeTest.java
rename to src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListTest.java
index 917ef9b92..c3fa96b45 100644
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterativeTest.java
+++ b/src/test/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListTest.java
@@ -1,9 +1,9 @@
package com.williamfiset.algorithms.graphtheory;
import static com.google.common.truth.Truth.assertThat;
-import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListIterative.Edge;
-import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListIterative.addUnweightedUndirectedEdge;
-import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyListIterative.createEmptyGraph;
+import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyList.Edge;
+import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyList.addUndirectedEdge;
+import static com.williamfiset.algorithms.graphtheory.BreadthFirstSearchAdjacencyList.createEmptyGraph;
import static java.lang.Math.max;
import static java.lang.Math.random;
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -13,9 +13,11 @@
import java.util.List;
import org.junit.jupiter.api.*;
-public class BreadthFirstSearchAdjacencyListIterativeTest {
+import com.williamfiset.algorithms.graphtheory.BellmanFordEdgeList;
- BreadthFirstSearchAdjacencyListIterative solver;
+public class BreadthFirstSearchAdjacencyListTest {
+
+ BreadthFirstSearchAdjacencyList solver;
@BeforeEach
public void setup() {
@@ -25,7 +27,7 @@ public void setup() {
@Test
public void testNullGraphInput() {
assertThrows(
- IllegalArgumentException.class, () -> new BreadthFirstSearchAdjacencyListIterative(null));
+ IllegalArgumentException.class, () -> new BreadthFirstSearchAdjacencyList(null));
}
@Test
@@ -33,7 +35,7 @@ public void testSingletonGraph() {
int n = 1;
List> graph;
+ private Integer[] prev;
+
+ public BreadthFirstSearchAdjacencyList(List
> graph) {
+ if (graph == null) {
+ throw new IllegalArgumentException("Graph can not be null");
+ }
+ this.n = graph.size();
+ this.graph = graph;
+ }
+
+ /**
+ * Returns the shortest path (fewest edges) from {@code start} to {@code end}.
+ *
+ * @return node indices along the path, or an empty list if unreachable.
+ */
+ public List
> createEmptyGraph(int n) {
+ List
> graph = new ArrayList<>(n);
+ for (int i = 0; i < n; i++) {
+ graph.add(new ArrayList<>());
+ }
+ return graph;
+ }
+
+ public static void addDirectedEdge(List
> graph, int u, int v) {
+ graph.get(u).add(new Edge(u, v));
+ }
+
+ public static void addUndirectedEdge(List
> graph, int u, int v) {
+ addDirectedEdge(graph, u, v);
+ addDirectedEdge(graph, v, u);
+ }
+
+ // ==================== Main ====================
+
+ //
+ // 0 --- 1 --- 2
+ // | | |
+ // 3 4 5
+ // \ / \ /
+ // 6 --- 7
+ //
+ // Shortest path from 0 to 7: [0, 1, 4, 7]
+ //
+ public static void main(String[] args) {
+ int n = 8;
+ List
> graph = createEmptyGraph(n);
+
+ addUndirectedEdge(graph, 0, 1);
+ addUndirectedEdge(graph, 1, 2);
+ addUndirectedEdge(graph, 0, 3);
+ addUndirectedEdge(graph, 1, 4);
+ addUndirectedEdge(graph, 2, 5);
+ addUndirectedEdge(graph, 3, 6);
+ addUndirectedEdge(graph, 4, 6);
+ addUndirectedEdge(graph, 4, 7);
+ addUndirectedEdge(graph, 5, 7);
+ addUndirectedEdge(graph, 6, 7);
+
+ BreadthFirstSearchAdjacencyList solver = new BreadthFirstSearchAdjacencyList(graph);
+
+ System.out.println(solver.reconstructPath(0, 7)); // [0, 1, 4, 7]
+ System.out.println(solver.reconstructPath(3, 5)); // [3, 6, 7, 5]
+ }
+}
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterative.java b/src/main/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterative.java
deleted file mode 100644
index 3ca3d1898..000000000
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/BreadthFirstSearchAdjacencyListIterative.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/**
- * An implementation of BFS with an adjacency list.
- *
- *
> graph;
-
- public BreadthFirstSearchAdjacencyListIterative(List
> graph) {
- if (graph == null) throw new IllegalArgumentException("Graph can not be null");
- n = graph.size();
- this.graph = graph;
- }
-
- /**
- * Reconstructs the path (of nodes) from 'start' to 'end' inclusive. If the edges are unweighted
- * then this method returns the shortest path from 'start' to 'end'
- *
- * @return An array of nodes indexes of the shortest path from 'start' to 'end'. If 'start' and
- * 'end' are not connected then an empty array is returned.
- */
- public List
> createEmptyGraph(int n) {
- List
> graph = new ArrayList<>(n);
- for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
- return graph;
- }
-
- // Add a directed edge from node 'u' to node 'v' with cost 'cost'.
- public static void addDirectedEdge(List
> graph, int u, int v, int cost) {
- graph.get(u).add(new Edge(u, v, cost));
- }
-
- // Add an undirected edge between nodes 'u' and 'v'.
- public static void addUndirectedEdge(List
> graph, int u, int v, int cost) {
- addDirectedEdge(graph, u, v, cost);
- addDirectedEdge(graph, v, u, cost);
- }
-
- // Add an undirected unweighted edge between nodes 'u' and 'v'. The edge added
- // will have a weight of 1 since its intended to be unweighted.
- public static void addUnweightedUndirectedEdge(List
> graph, int u, int v) {
- addUndirectedEdge(graph, u, v, 1);
- }
-
- /* BFS example. */
-
- public static void main(String[] args) {
- // BFS example #1 from slides.
- final int n = 13;
- List
> graph = createEmptyGraph(n);
-
- addUnweightedUndirectedEdge(graph, 0, 7);
- addUnweightedUndirectedEdge(graph, 0, 9);
- addUnweightedUndirectedEdge(graph, 0, 11);
- addUnweightedUndirectedEdge(graph, 7, 11);
- addUnweightedUndirectedEdge(graph, 7, 6);
- addUnweightedUndirectedEdge(graph, 7, 3);
- addUnweightedUndirectedEdge(graph, 6, 5);
- addUnweightedUndirectedEdge(graph, 3, 4);
- addUnweightedUndirectedEdge(graph, 2, 3);
- addUnweightedUndirectedEdge(graph, 2, 12);
- addUnweightedUndirectedEdge(graph, 12, 8);
- addUnweightedUndirectedEdge(graph, 8, 1);
- addUnweightedUndirectedEdge(graph, 1, 10);
- addUnweightedUndirectedEdge(graph, 10, 9);
- addUnweightedUndirectedEdge(graph, 9, 8);
-
- BreadthFirstSearchAdjacencyListIterative solver;
- solver = new BreadthFirstSearchAdjacencyListIterative(graph);
-
- int start = 10, end = 5;
- List
> graph, int start, int n) {
- boolean[] visited = new boolean[n];
- Queue
> graph) {
-
- int at = queue.poll();
-
- if (at == DEPTH_TOKEN) {
- queue.offer(DEPTH_TOKEN);
- return 1;
- }
-
- // This node is already visited.
- if (visited[at]) return 0;
-
- // Visit this node.
- visited[at] = true;
-
- // Add all neighbors to queue.
- List
> graph = new ArrayList<>();
- for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
-
- addUndirectedEdge(graph, 0, 1);
- addUndirectedEdge(graph, 0, 2);
- addUndirectedEdge(graph, 0, 3);
- addUndirectedEdge(graph, 2, 9);
- addUndirectedEdge(graph, 8, 2);
- addUndirectedEdge(graph, 3, 4);
- addUndirectedEdge(graph, 10, 11);
- addUndirectedEdge(graph, 12, 13);
- addUndirectedEdge(graph, 3, 5);
- addUndirectedEdge(graph, 5, 7);
- addUndirectedEdge(graph, 5, 6);
- addUndirectedEdge(graph, 0, 10);
- addUndirectedEdge(graph, 11, 12);
-
- System.out.printf("BFS depth: %d\n", bfs(graph, 12, n));
- }
-
- // Helper method to setup graph
- private static void addUndirectedEdge(List
> graph, int from, int to) {
- graph.get(from).add(to);
- graph.get(to).add(from);
- }
-}
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyList.java b/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyList.java
index 49151169e..dba299230 100644
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyList.java
+++ b/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyList.java
@@ -1,9 +1,33 @@
/**
- * Finds all the bridges on an undirected graph.
+ * Bridge Edges (Cut Edges) — Adjacency List
*
- *
ids[u] < low[v]
+ *
+ * > graph;
private boolean solved;
+ private int id;
+ private int[] low, ids;
private boolean[] visited;
- private List
> graph;
- private List
> graph, int n) {
- if (graph == null || n <= 0 || graph.size() != n) throw new IllegalArgumentException();
+ if (graph == null || n <= 0 || graph.size() != n) {
+ throw new IllegalArgumentException();
+ }
this.graph = graph;
this.n = n;
}
- // Returns a list of pairs of nodes indicating which nodes form bridges.
- // The returned list is always of even length and indexes (2*i, 2*i+1) form a
- // pair. For example, nodes at indexes (0, 1) are a pair, (2, 3) are another
- // pair, etc...
- public List
> createGraph(int n) {
+ List
> graph = new ArrayList<>(n);
+ for (int i = 0; i < n; i++) {
+ graph.add(new ArrayList<>());
+ }
+ return graph;
+ }
+ public static void addEdge(List
> graph, int from, int to) {
+ graph.get(from).add(to);
+ graph.get(to).add(from);
+ }
+
+ // ==================== Main ====================
+
+ //
+ // 0 --- 1
+ // | /
+ // 2 -------- 5 --- 6
+ // | | |
+ // 3 --- 4 8 --- 7
+ //
+ // Bridges: (2,3), (3,4), (2,5)
+ //
+ public static void main(String[] args) {
int n = 9;
List
> graph = createGraph(n);
@@ -88,29 +147,10 @@ public static void main(String[] args) {
addEdge(graph, 8, 5);
BridgesAdjacencyList solver = new BridgesAdjacencyList(graph, n);
- List
> createGraph(int n) {
- List
> graph = new ArrayList<>();
- for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
- return graph;
- }
+ List
> graph, int from, int to) {
- graph.get(from).add(to);
- graph.get(to).add(from);
+ for (int[] bridge : bridges) {
+ System.out.printf("Bridge between nodes: %d and %d\n", bridge[0], bridge[1]);
+ }
}
}
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterative.java b/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterative.java
deleted file mode 100644
index 0fb3d5b7c..000000000
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterative.java
+++ /dev/null
@@ -1,136 +0,0 @@
-/**
- * Finds all the bridges on an undirected graph.
- *
- *
> graph;
- private List
> graph, int n) {
- if (graph == null || n <= 0 || graph.size() != n) throw new IllegalArgumentException();
- this.graph = graph;
- this.n = n;
- }
-
- // Returns a list of pairs of nodes indicating which nodes form bridges.
- // The returned list is always of even length and indexes (2*i, 2*i+1) form a
- // pair. For example, nodes are indexes (0, 1) are a pair, (2, 3) are another
- // pair, etc...
- public List
> graph = createGraph(n);
-
- addEdge(graph, 0, 1);
- addEdge(graph, 0, 2);
- addEdge(graph, 1, 2);
- addEdge(graph, 1, 3);
- addEdge(graph, 2, 3);
- addEdge(graph, 1, 4);
- addEdge(graph, 2, 7);
- addEdge(graph, 4, 6);
- addEdge(graph, 4, 5);
- addEdge(graph, 5, 6);
- addEdge(graph, 7, 8);
- addEdge(graph, 7, 9);
-
- BridgesAdjacencyListIterative solver = new BridgesAdjacencyListIterative(graph, n);
- List
> createGraph(int n) {
- List
> graph = new ArrayList<>();
- for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
- return graph;
- }
-
- // Add undirected edge to graph.
- public static void addEdge(List
> graph, int from, int to) {
- graph.get(from).add(to);
- graph.get(to).add(from);
- }
-}
diff --git a/src/main/java/com/williamfiset/algorithms/graphtheory/LazyPrimsAdjacencyMatrix.java b/src/main/java/com/williamfiset/algorithms/graphtheory/LazyPrimsAdjacencyMatrix.java
deleted file mode 100644
index 7f789c359..000000000
--- a/src/main/java/com/williamfiset/algorithms/graphtheory/LazyPrimsAdjacencyMatrix.java
+++ /dev/null
@@ -1,95 +0,0 @@
-/**
- * An implementation of the lazy Prim's algorithm with an adjacency matrix which upon visiting a new
- * node adds all the edges to the min priority queue and also removes already seen edges when
- * polling.
- *
- *
> graph = createEmptyGraph(n);
- solver = new BreadthFirstSearchAdjacencyListIterative(graph);
+ solver = new BreadthFirstSearchAdjacencyList(graph);
List
> graph = createEmptyGraph(n);
- addUnweightedUndirectedEdge(graph, 0, 1);
- solver = new BreadthFirstSearchAdjacencyListIterative(graph);
+ addUndirectedEdge(graph, 0, 1);
+ solver = new BreadthFirstSearchAdjacencyList(graph);
List
> graph = BreadthFirstSearchAdjacencyListIterative.createEmptyGraph(n);
- addUnweightedUndirectedEdge(graph, 0, 1);
- addUnweightedUndirectedEdge(graph, 2, 1);
- solver = new BreadthFirstSearchAdjacencyListIterative(graph);
+ List
> graph = BreadthFirstSearchAdjacencyList.createEmptyGraph(n);
+ addUndirectedEdge(graph, 0, 1);
+ addUndirectedEdge(graph, 2, 1);
+ solver = new BreadthFirstSearchAdjacencyList(graph);
List
> graph1, int n) {
int u = (int) (random() * n);
int v = (int) (random() * n);
if (!edgeMatrix[u][v]) {
- addUnweightedUndirectedEdge(graph1, u, v);
+ addUndirectedEdge(graph1, u, v);
graph2[u][v] = graph2[v][u] = 1;
edgeMatrix[u][v] = edgeMatrix[v][u] = true;
}
diff --git a/src/test/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterativeTest.java b/src/test/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterativeTest.java
deleted file mode 100644
index 8e9de87c0..000000000
--- a/src/test/java/com/williamfiset/algorithms/graphtheory/BridgesAdjacencyListIterativeTest.java
+++ /dev/null
@@ -1,162 +0,0 @@
-package com.williamfiset.algorithms.graphtheory;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import com.google.common.collect.ImmutableList;
-import java.util.*;
-import org.apache.commons.lang3.tuple.Pair;
-import org.junit.jupiter.api.*;
-
-public class BridgesAdjacencyListIterativeTest {
-
- // Initialize graph with 'n' nodes.
- public static List
> createGraph(int n) {
- List
> graph = new ArrayList<>();
- for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
- return graph;
- }
-
- // Add undirected edge to graph.
- public static void addEdge(List
> graph, int from, int to) {
- graph.get(from).add(to);
- graph.get(to).add(from);
- }
-
- // Every edge should be a bridge if the input a tree
- @Test
- public void testTreeCase() {
-
- int n = 12;
- List
> graph = createGraph(n);
- addEdge(graph, 1, 0);
- addEdge(graph, 0, 2);
- addEdge(graph, 2, 5);
- addEdge(graph, 5, 6);
- addEdge(graph, 5, 11);
- addEdge(graph, 5, 4);
- addEdge(graph, 4, 10);
- addEdge(graph, 4, 3);
- addEdge(graph, 3, 7);
- addEdge(graph, 7, 8);
- addEdge(graph, 7, 9);
-
- BridgesAdjacencyList solver = new BridgesAdjacencyList(graph, n);
- List
> graph = createGraph(n);
- addEdge(graph, 1, 0);
- addEdge(graph, 0, 2);
- addEdge(graph, 3, 1);
- addEdge(graph, 2, 5);
- addEdge(graph, 5, 6);
- addEdge(graph, 5, 11);
- addEdge(graph, 5, 4);
- addEdge(graph, 4, 10);
- addEdge(graph, 4, 3);
- addEdge(graph, 3, 7);
- addEdge(graph, 7, 8);
- addEdge(graph, 7, 9);
- addEdge(graph, 11, 6);
-
- BridgesAdjacencyList solver = new BridgesAdjacencyList(graph, n);
- List
> graph = createGraph(n);
- addEdge(graph, 0, 1);
- addEdge(graph, 1, 2);
- addEdge(graph, 2, 3);
- addEdge(graph, 2, 5);
- addEdge(graph, 2, 0);
- addEdge(graph, 3, 4);
- addEdge(graph, 5, 6);
- addEdge(graph, 6, 7);
- addEdge(graph, 7, 8);
- addEdge(graph, 8, 5);
-
- BridgesAdjacencyList solver = new BridgesAdjacencyList(graph, n);
- List
> graph = createGraph(n);
- addEdge(graph, 0, 1);
- addEdge(graph, 2, 1);
-
- addEdge(graph, 3, 4);
-
- addEdge(graph, 5, 7);
- addEdge(graph, 5, 6);
- addEdge(graph, 6, 7);
- addEdge(graph, 8, 7);
- addEdge(graph, 8, 9);
- addEdge(graph, 8, 10);
-
- BridgesAdjacencyList solver = new BridgesAdjacencyList(graph, n);
- List