From 55f7e2b38b37e04552f01e41512830c2d95270c5 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 22:52:29 +0200 Subject: [PATCH 01/12] Implement Bidirectional BFS algorithm This class implements the Bidirectional Breadth-First Search (BFS) algorithm to determine if a path exists between two nodes in an unweighted graph. It explores the graph from both the start and goal nodes simultaneously, improving efficiency for large graphs. --- .../graphs/BidirectionalBFS.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java new file mode 100644 index 000000000000..9d325416d6a3 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -0,0 +1,75 @@ +/* +BidirectionalBFS.java + +This class implements the Bidirectional Breadth-First Search (BFS) algorithm to efficiently +determine whether a path exists between two nodes in an unweighted graph. Instead of +searching from the start node alone, it simultaneously explores the graph from both the +start and goal nodes, meeting in the middle. This approach often reduces the number of +nodes visited compared to traditional BFS, making it faster for large graphs. The main +method provides an example graph and demonstrates usage by printing whether a path +exists between the specified start and goal nodes. + */ + + + +import java.util.*; + +public class BidirectionalBFS { + + public static boolean bidirectionalBFS(Map> graph, int start, int goal) { + if (start == goal) return true; + + Set visitedStart = new HashSet<>(); + Set visitedGoal = new HashSet<>(); + + Queue queueStart = new LinkedList<>(); + Queue queueGoal = new LinkedList<>(); + + queueStart.add(start); + queueGoal.add(goal); + + visitedStart.add(start); + visitedGoal.add(goal); + + while (!queueStart.isEmpty() && !queueGoal.isEmpty()) { + // Expand from start side + if (expandFrontier(graph, queueStart, visitedStart, visitedGoal)) return true; + // Expand from goal side + if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart)) return true; + } + + return false; // no path found + } + + private static boolean expandFrontier(Map> graph, Queue queue, + Set visitedThisSide, Set visitedOtherSide) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + int current = queue.poll(); + for (int neighbor : graph.getOrDefault(current, new ArrayList<>())) { + if (visitedOtherSide.contains(neighbor)) return true; + if (!visitedThisSide.contains(neighbor)) { + visitedThisSide.add(neighbor); + queue.add(neighbor); + } + } + } + return false; + } + + public static void main(String[] args) { + Map> graph = new HashMap<>(); + graph.put(0, Arrays.asList(1, 2)); + graph.put(1, Arrays.asList(0, 3)); + graph.put(2, Arrays.asList(0, 3, 4)); + graph.put(3, Arrays.asList(1, 2, 5)); + graph.put(4, Arrays.asList(2, 5)); + graph.put(5, Arrays.asList(3, 4)); + + int start = 0; + int goal = 5; + + boolean pathExists = bidirectionalBFS(graph, start, goal); + System.out.println("Path from " + start + " to " + goal + ": " + pathExists); + } +} From 50c76d7df3d24a68a94c0b0db0dfdd99c9218ec3 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 22:58:37 +0200 Subject: [PATCH 02/12] Refactor Bidirectional BFS implementation Updated the Bidirectional BFS implementation to improve code clarity and structure. --- .../graphs/BidirectionalBFS.java | 105 ++++++++++-------- 1 file changed, 61 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java index 9d325416d6a3..77c732a3b352 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -1,23 +1,33 @@ -/* -BidirectionalBFS.java - -This class implements the Bidirectional Breadth-First Search (BFS) algorithm to efficiently -determine whether a path exists between two nodes in an unweighted graph. Instead of -searching from the start node alone, it simultaneously explores the graph from both the -start and goal nodes, meeting in the middle. This approach often reduces the number of -nodes visited compared to traditional BFS, making it faster for large graphs. The main -method provides an example graph and demonstrates usage by printing whether a path -exists between the specified start and goal nodes. - */ - - - -import java.util.*; +import java.util.Map; +import java.util.List; +import java.util.Set; +import java.util.Queue; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.ArrayList; +import java.util.Arrays; -public class BidirectionalBFS { - - public static boolean bidirectionalBFS(Map> graph, int start, int goal) { - if (start == goal) return true; +/** + * Implementation of Bidirectional Breadth-First Search (BFS) algorithm. + * Checks if a path exists between a start node and a goal node in an undirected graph. + */ +public class BidirectionalBFS +{ + /** + * Checks if a path exists between start and goal using bidirectional BFS. + * + * @param graph The adjacency list of the graph + * @param start The starting node + * @param goal The goal node + * @return true if a path exists, false otherwise + */ + public static boolean bidirectionalBFS(Map> graph, int start, int goal) + { + if (start == goal) + { + return true; + } Set visitedStart = new HashSet<>(); Set visitedGoal = new HashSet<>(); @@ -31,24 +41,47 @@ public static boolean bidirectionalBFS(Map> graph, int st visitedStart.add(start); visitedGoal.add(goal); - while (!queueStart.isEmpty() && !queueGoal.isEmpty()) { + while (!queueStart.isEmpty() && !queueGoal.isEmpty()) + { // Expand from start side - if (expandFrontier(graph, queueStart, visitedStart, visitedGoal)) return true; + if (expandFrontier(graph, queueStart, visitedStart, visitedGoal)) + { + return true; + } // Expand from goal side - if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart)) return true; + if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart)) + { + return true; + } } - return false; // no path found + return false; // No path found } + /** + * Helper function to expand one level of BFS frontier. + * + * @param graph The adjacency list of the graph + * @param queue The BFS queue for this side + * @param visitedThisSide Set of nodes visited from this side + * @param visitedOtherSide Set of nodes visited from the other side + * @return true if the frontiers meet, false otherwise + */ private static boolean expandFrontier(Map> graph, Queue queue, - Set visitedThisSide, Set visitedOtherSide) { + Set visitedThisSide, Set visitedOtherSide) + { int size = queue.size(); - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { int current = queue.poll(); - for (int neighbor : graph.getOrDefault(current, new ArrayList<>())) { - if (visitedOtherSide.contains(neighbor)) return true; - if (!visitedThisSide.contains(neighbor)) { + for (int neighbor : graph.getOrDefault(current, new ArrayList<>())) + { + if (visitedOtherSide.contains(neighbor)) + { + return true; + } + if (!visitedThisSide.contains(neighbor)) + { visitedThisSide.add(neighbor); queue.add(neighbor); } @@ -56,20 +89,4 @@ private static boolean expandFrontier(Map> graph, Queue> graph = new HashMap<>(); - graph.put(0, Arrays.asList(1, 2)); - graph.put(1, Arrays.asList(0, 3)); - graph.put(2, Arrays.asList(0, 3, 4)); - graph.put(3, Arrays.asList(1, 2, 5)); - graph.put(4, Arrays.asList(2, 5)); - graph.put(5, Arrays.asList(3, 4)); - - int start = 0; - int goal = 5; - - boolean pathExists = bidirectionalBFS(graph, start, goal); - System.out.println("Path from " + start + " to " + goal + ": " + pathExists); - } } From d6e0c7eb3b8889747a43a86e6384c9ad4418e924 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:03:53 +0200 Subject: [PATCH 03/12] Update BidirectionalBFS.java --- .../graphs/BidirectionalBFS.java | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java index 77c732a3b352..fd701d99c15f 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -6,7 +6,6 @@ import java.util.HashSet; import java.util.LinkedList; import java.util.ArrayList; -import java.util.Arrays; /** * Implementation of Bidirectional Breadth-First Search (BFS) algorithm. @@ -22,7 +21,11 @@ public class BidirectionalBFS * @param goal The goal node * @return true if a path exists, false otherwise */ - public static boolean bidirectionalBFS(Map> graph, int start, int goal) + public static boolean bidirectionalBFS( + Map> graph, + int start, + int goal + ) { if (start == goal) { @@ -44,12 +47,23 @@ public static boolean bidirectionalBFS(Map> graph, int st while (!queueStart.isEmpty() && !queueGoal.isEmpty()) { // Expand from start side - if (expandFrontier(graph, queueStart, visitedStart, visitedGoal)) + if (expandFrontier( + graph, + queueStart, + visitedStart, + visitedGoal + )) { return true; } + // Expand from goal side - if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart)) + if (expandFrontier( + graph, + queueGoal, + visitedGoal, + visitedStart + )) { return true; } @@ -61,14 +75,18 @@ public static boolean bidirectionalBFS(Map> graph, int st /** * Helper function to expand one level of BFS frontier. * - * @param graph The adjacency list of the graph - * @param queue The BFS queue for this side - * @param visitedThisSide Set of nodes visited from this side + * @param graph The adjacency list of the graph + * @param queue The BFS queue for this side + * @param visitedThisSide Set of nodes visited from this side * @param visitedOtherSide Set of nodes visited from the other side * @return true if the frontiers meet, false otherwise */ - private static boolean expandFrontier(Map> graph, Queue queue, - Set visitedThisSide, Set visitedOtherSide) + private static boolean expandFrontier( + Map> graph, + Queue queue, + Set visitedThisSide, + Set visitedOtherSide + ) { int size = queue.size(); for (int i = 0; i < size; i++) From dd452ee62b14e4c7de291a0dcb41862ee45481bb Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:04:26 +0200 Subject: [PATCH 04/12] Add tests for BidirectionalBFS functionality This test verifies the functionality of the BidirectionalBFS algorithm by checking for paths in a sample graph. --- BidirectionalBFSTest.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 BidirectionalBFSTest.java diff --git a/BidirectionalBFSTest.java b/BidirectionalBFSTest.java new file mode 100644 index 000000000000..45acebcf0c6e --- /dev/null +++ b/BidirectionalBFSTest.java @@ -0,0 +1,29 @@ +import java.util.Map; +import java.util.List; +import java.util.HashMap; +import java.util.Arrays; + +/** + * Simple test for BidirectionalBFS. + */ +public class BidirectionalBFSTest +{ + public static void main(String[] args) + { + Map> graph = new HashMap<>(); + graph.put(0, Arrays.asList(1, 2)); + graph.put(1, Arrays.asList(0, 3)); + graph.put(2, Arrays.asList(0, 3, 4)); + graph.put(3, Arrays.asList(1, 2, 5)); + graph.put(4, Arrays.asList(2, 5)); + graph.put(5, Arrays.asList(3, 4)); + + // Test 1 + boolean result1 = BidirectionalBFS.bidirectionalBFS(graph, 0, 5); + System.out.println("Path 0->5 exists: " + result1); // true + + // Test 2 + boolean result2 = BidirectionalBFS.bidirectionalBFS(graph, 0, 6); + System.out.println("Path 0->6 exists: " + result2); // false + } +} From 5cc3041bcdf73afc56147a287c80d4b2665ea2d8 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:06:42 +0200 Subject: [PATCH 05/12] Rename BidirectionalBFSTest.java to examples/BidirectionalBFSTest.java --- BidirectionalBFSTest.java => examples/BidirectionalBFSTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename BidirectionalBFSTest.java => examples/BidirectionalBFSTest.java (100%) diff --git a/BidirectionalBFSTest.java b/examples/BidirectionalBFSTest.java similarity index 100% rename from BidirectionalBFSTest.java rename to examples/BidirectionalBFSTest.java From c14ae3c792f2c3b8c845f99ff18c93ed18dafc51 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:09:38 +0200 Subject: [PATCH 06/12] Rename examples/BidirectionalBFSTest.java to src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java --- .../datastructures/graphs}/BidirectionalBFSTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {examples => src/main/java/com/thealgorithms/datastructures/graphs}/BidirectionalBFSTest.java (100%) diff --git a/examples/BidirectionalBFSTest.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java similarity index 100% rename from examples/BidirectionalBFSTest.java rename to src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java From 632e93c5bbf352c4f698f78e5ed4d332940ae91c Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:11:18 +0200 Subject: [PATCH 07/12] Update BidirectionalBFS.java --- .../datastructures/graphs/BidirectionalBFS.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java index fd701d99c15f..570ee49c1001 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -8,9 +8,13 @@ import java.util.ArrayList; /** - * Implementation of Bidirectional Breadth-First Search (BFS) algorithm. - * Checks if a path exists between a start node and a goal node in an undirected graph. + * Implementation of the Bidirectional Breadth-First Search (BiBFS) algorithm. + * This algorithm performs BFS from both the start and goal nodes simultaneously, + * greatly reducing search space in large unweighted graphs. + * + * Wikipedia reference: https://en.wikipedia.org/wiki/Bidirectional_search */ + public class BidirectionalBFS { /** From 3a0f3a361f0ab1e291dbac8b68056761c76f98d2 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:15:44 +0200 Subject: [PATCH 08/12] Update BidirectionalBFS.java --- .../thealgorithms/datastructures/graphs/BidirectionalBFS.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java index 570ee49c1001..b3ea48a42b7f 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -1,3 +1,5 @@ +package com.thealgorithms.graphs; + import java.util.Map; import java.util.List; import java.util.Set; From 715fd1bf67b1655f2e68a1930ce4e00d04f2741b Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:16:18 +0200 Subject: [PATCH 09/12] Implement unit tests for BidirectionalBFS Added unit tests for BidirectionalBFS to check path existence. --- .../graphs/BidirectionalBFSTest.java | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java index 45acebcf0c6e..e2fcf2b42ace 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java @@ -1,14 +1,20 @@ +package com.thealgorithms.graphs; + import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.Arrays; +import org.junit.Test; +import static org.junit.Assert.*; + /** - * Simple test for BidirectionalBFS. + * Unit tests for BidirectionalBFS. */ public class BidirectionalBFSTest { - public static void main(String[] args) + @Test + public void testPathExists() { Map> graph = new HashMap<>(); graph.put(0, Arrays.asList(1, 2)); @@ -18,12 +24,19 @@ public static void main(String[] args) graph.put(4, Arrays.asList(2, 5)); graph.put(5, Arrays.asList(3, 4)); - // Test 1 - boolean result1 = BidirectionalBFS.bidirectionalBFS(graph, 0, 5); - System.out.println("Path 0->5 exists: " + result1); // true + assertTrue(BidirectionalBFS.bidirectionalBFS(graph, 0, 5)); + } - // Test 2 - boolean result2 = BidirectionalBFS.bidirectionalBFS(graph, 0, 6); - System.out.println("Path 0->6 exists: " + result2); // false + @Test + public void testPathDoesNotExist() + { + Map> graph = new HashMap<>(); + graph.put(0, Arrays.asList(1)); + graph.put(1, Arrays.asList(0)); + // node 2 is isolated + graph.put(2, Arrays.asList()); + + assertFalse(BidirectionalBFS.bidirectionalBFS(graph, 0, 2)); } } + From 2393b6ef2852f32bff5c50eeeac672e478ebb390 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:18:15 +0200 Subject: [PATCH 10/12] Update BidirectionalBFSTest.java --- .../graphs/BidirectionalBFSTest.java | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java index e2fcf2b42ace..45acebcf0c6e 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java @@ -1,20 +1,14 @@ -package com.thealgorithms.graphs; - import java.util.Map; import java.util.List; import java.util.HashMap; import java.util.Arrays; -import org.junit.Test; -import static org.junit.Assert.*; - /** - * Unit tests for BidirectionalBFS. + * Simple test for BidirectionalBFS. */ public class BidirectionalBFSTest { - @Test - public void testPathExists() + public static void main(String[] args) { Map> graph = new HashMap<>(); graph.put(0, Arrays.asList(1, 2)); @@ -24,19 +18,12 @@ public void testPathExists() graph.put(4, Arrays.asList(2, 5)); graph.put(5, Arrays.asList(3, 4)); - assertTrue(BidirectionalBFS.bidirectionalBFS(graph, 0, 5)); - } + // Test 1 + boolean result1 = BidirectionalBFS.bidirectionalBFS(graph, 0, 5); + System.out.println("Path 0->5 exists: " + result1); // true - @Test - public void testPathDoesNotExist() - { - Map> graph = new HashMap<>(); - graph.put(0, Arrays.asList(1)); - graph.put(1, Arrays.asList(0)); - // node 2 is isolated - graph.put(2, Arrays.asList()); - - assertFalse(BidirectionalBFS.bidirectionalBFS(graph, 0, 2)); + // Test 2 + boolean result2 = BidirectionalBFS.bidirectionalBFS(graph, 0, 6); + System.out.println("Path 0->6 exists: " + result2); // false } } - From bca2f220dc151a6bba718c18ada231b6e4e80e09 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:19:02 +0200 Subject: [PATCH 11/12] Delete src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java --- .../graphs/BidirectionalBFSTest.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java deleted file mode 100644 index 45acebcf0c6e..000000000000 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFSTest.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.util.Map; -import java.util.List; -import java.util.HashMap; -import java.util.Arrays; - -/** - * Simple test for BidirectionalBFS. - */ -public class BidirectionalBFSTest -{ - public static void main(String[] args) - { - Map> graph = new HashMap<>(); - graph.put(0, Arrays.asList(1, 2)); - graph.put(1, Arrays.asList(0, 3)); - graph.put(2, Arrays.asList(0, 3, 4)); - graph.put(3, Arrays.asList(1, 2, 5)); - graph.put(4, Arrays.asList(2, 5)); - graph.put(5, Arrays.asList(3, 4)); - - // Test 1 - boolean result1 = BidirectionalBFS.bidirectionalBFS(graph, 0, 5); - System.out.println("Path 0->5 exists: " + result1); // true - - // Test 2 - boolean result2 = BidirectionalBFS.bidirectionalBFS(graph, 0, 6); - System.out.println("Path 0->6 exists: " + result2); // false - } -} From bc05bce1fa79b0da291cf8366ab4a52ddb3d6866 Mon Sep 17 00:00:00 2001 From: Zenith Date: Sat, 29 Nov 2025 23:23:11 +0200 Subject: [PATCH 12/12] Refactor BidirectionalBFS class for improved readability --- .../graphs/BidirectionalBFS.java | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java index b3ea48a42b7f..8d5e31dec0cf 100644 --- a/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java +++ b/src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java @@ -16,9 +16,8 @@ * * Wikipedia reference: https://en.wikipedia.org/wiki/Bidirectional_search */ +public class BidirectionalBFS { -public class BidirectionalBFS -{ /** * Checks if a path exists between start and goal using bidirectional BFS. * @@ -28,13 +27,11 @@ public class BidirectionalBFS * @return true if a path exists, false otherwise */ public static boolean bidirectionalBFS( - Map> graph, - int start, - int goal - ) - { - if (start == goal) - { + Map> graph, + int start, + int goal + ) { + if (start == goal) { return true; } @@ -50,27 +47,13 @@ public static boolean bidirectionalBFS( visitedStart.add(start); visitedGoal.add(goal); - while (!queueStart.isEmpty() && !queueGoal.isEmpty()) - { + while (!queueStart.isEmpty() && !queueGoal.isEmpty()) { // Expand from start side - if (expandFrontier( - graph, - queueStart, - visitedStart, - visitedGoal - )) - { + if (expandFrontier(graph, queueStart, visitedStart, visitedGoal)) { return true; } - // Expand from goal side - if (expandFrontier( - graph, - queueGoal, - visitedGoal, - visitedStart - )) - { + if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart)) { return true; } } @@ -88,24 +71,19 @@ public static boolean bidirectionalBFS( * @return true if the frontiers meet, false otherwise */ private static boolean expandFrontier( - Map> graph, - Queue queue, - Set visitedThisSide, - Set visitedOtherSide - ) - { + Map> graph, + Queue queue, + Set visitedThisSide, + Set visitedOtherSide + ) { int size = queue.size(); - for (int i = 0; i < size; i++) - { + for (int i = 0; i < size; i++) { int current = queue.poll(); - for (int neighbor : graph.getOrDefault(current, new ArrayList<>())) - { - if (visitedOtherSide.contains(neighbor)) - { + for (int neighbor : graph.getOrDefault(current, new ArrayList<>())) { + if (visitedOtherSide.contains(neighbor)) { return true; } - if (!visitedThisSide.contains(neighbor)) - { + if (!visitedThisSide.contains(neighbor)) { visitedThisSide.add(neighbor); queue.add(neighbor); }