Skip to content

Commit d6e0c7e

Browse files
authored
Update BidirectionalBFS.java
1 parent 50c76d7 commit d6e0c7e

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/main/java/com/thealgorithms/datastructures/graphs/BidirectionalBFS.java

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.util.HashSet;
77
import java.util.LinkedList;
88
import java.util.ArrayList;
9-
import java.util.Arrays;
109

1110
/**
1211
* Implementation of Bidirectional Breadth-First Search (BFS) algorithm.
@@ -22,7 +21,11 @@ public class BidirectionalBFS
2221
* @param goal The goal node
2322
* @return true if a path exists, false otherwise
2423
*/
25-
public static boolean bidirectionalBFS(Map<Integer, List<Integer>> graph, int start, int goal)
24+
public static boolean bidirectionalBFS(
25+
Map<Integer, List<Integer>> graph,
26+
int start,
27+
int goal
28+
)
2629
{
2730
if (start == goal)
2831
{
@@ -44,12 +47,23 @@ public static boolean bidirectionalBFS(Map<Integer, List<Integer>> graph, int st
4447
while (!queueStart.isEmpty() && !queueGoal.isEmpty())
4548
{
4649
// Expand from start side
47-
if (expandFrontier(graph, queueStart, visitedStart, visitedGoal))
50+
if (expandFrontier(
51+
graph,
52+
queueStart,
53+
visitedStart,
54+
visitedGoal
55+
))
4856
{
4957
return true;
5058
}
59+
5160
// Expand from goal side
52-
if (expandFrontier(graph, queueGoal, visitedGoal, visitedStart))
61+
if (expandFrontier(
62+
graph,
63+
queueGoal,
64+
visitedGoal,
65+
visitedStart
66+
))
5367
{
5468
return true;
5569
}
@@ -61,14 +75,18 @@ public static boolean bidirectionalBFS(Map<Integer, List<Integer>> graph, int st
6175
/**
6276
* Helper function to expand one level of BFS frontier.
6377
*
64-
* @param graph The adjacency list of the graph
65-
* @param queue The BFS queue for this side
66-
* @param visitedThisSide Set of nodes visited from this side
78+
* @param graph The adjacency list of the graph
79+
* @param queue The BFS queue for this side
80+
* @param visitedThisSide Set of nodes visited from this side
6781
* @param visitedOtherSide Set of nodes visited from the other side
6882
* @return true if the frontiers meet, false otherwise
6983
*/
70-
private static boolean expandFrontier(Map<Integer, List<Integer>> graph, Queue<Integer> queue,
71-
Set<Integer> visitedThisSide, Set<Integer> visitedOtherSide)
84+
private static boolean expandFrontier(
85+
Map<Integer, List<Integer>> graph,
86+
Queue<Integer> queue,
87+
Set<Integer> visitedThisSide,
88+
Set<Integer> visitedOtherSide
89+
)
7290
{
7391
int size = queue.size();
7492
for (int i = 0; i < size; i++)

0 commit comments

Comments
 (0)