1- /*
2- BidirectionalBFS.java
3-
4- This class implements the Bidirectional Breadth-First Search (BFS) algorithm to efficiently
5- determine whether a path exists between two nodes in an unweighted graph. Instead of
6- searching from the start node alone, it simultaneously explores the graph from both the
7- start and goal nodes, meeting in the middle. This approach often reduces the number of
8- nodes visited compared to traditional BFS, making it faster for large graphs. The main
9- method provides an example graph and demonstrates usage by printing whether a path
10- exists between the specified start and goal nodes.
11- */
12-
13-
14-
15- import java .util .*;
1+ import java .util .Map ;
2+ import java .util .List ;
3+ import java .util .Set ;
4+ import java .util .Queue ;
5+ import java .util .HashMap ;
6+ import java .util .HashSet ;
7+ import java .util .LinkedList ;
8+ import java .util .ArrayList ;
9+ import java .util .Arrays ;
1610
17- public class BidirectionalBFS {
18-
19- public static boolean bidirectionalBFS (Map <Integer , List <Integer >> graph , int start , int goal ) {
20- if (start == goal ) return true ;
11+ /**
12+ * Implementation of Bidirectional Breadth-First Search (BFS) algorithm.
13+ * Checks if a path exists between a start node and a goal node in an undirected graph.
14+ */
15+ public class BidirectionalBFS
16+ {
17+ /**
18+ * Checks if a path exists between start and goal using bidirectional BFS.
19+ *
20+ * @param graph The adjacency list of the graph
21+ * @param start The starting node
22+ * @param goal The goal node
23+ * @return true if a path exists, false otherwise
24+ */
25+ public static boolean bidirectionalBFS (Map <Integer , List <Integer >> graph , int start , int goal )
26+ {
27+ if (start == goal )
28+ {
29+ return true ;
30+ }
2131
2232 Set <Integer > visitedStart = new HashSet <>();
2333 Set <Integer > visitedGoal = new HashSet <>();
@@ -31,45 +41,52 @@ public static boolean bidirectionalBFS(Map<Integer, List<Integer>> graph, int st
3141 visitedStart .add (start );
3242 visitedGoal .add (goal );
3343
34- while (!queueStart .isEmpty () && !queueGoal .isEmpty ()) {
44+ while (!queueStart .isEmpty () && !queueGoal .isEmpty ())
45+ {
3546 // Expand from start side
36- if (expandFrontier (graph , queueStart , visitedStart , visitedGoal )) return true ;
47+ if (expandFrontier (graph , queueStart , visitedStart , visitedGoal ))
48+ {
49+ return true ;
50+ }
3751 // Expand from goal side
38- if (expandFrontier (graph , queueGoal , visitedGoal , visitedStart )) return true ;
52+ if (expandFrontier (graph , queueGoal , visitedGoal , visitedStart ))
53+ {
54+ return true ;
55+ }
3956 }
4057
41- return false ; // no path found
58+ return false ; // No path found
4259 }
4360
61+ /**
62+ * Helper function to expand one level of BFS frontier.
63+ *
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
67+ * @param visitedOtherSide Set of nodes visited from the other side
68+ * @return true if the frontiers meet, false otherwise
69+ */
4470 private static boolean expandFrontier (Map <Integer , List <Integer >> graph , Queue <Integer > queue ,
45- Set <Integer > visitedThisSide , Set <Integer > visitedOtherSide ) {
71+ Set <Integer > visitedThisSide , Set <Integer > visitedOtherSide )
72+ {
4673 int size = queue .size ();
47- for (int i = 0 ; i < size ; i ++) {
74+ for (int i = 0 ; i < size ; i ++)
75+ {
4876 int current = queue .poll ();
49- for (int neighbor : graph .getOrDefault (current , new ArrayList <>())) {
50- if (visitedOtherSide .contains (neighbor )) return true ;
51- if (!visitedThisSide .contains (neighbor )) {
77+ for (int neighbor : graph .getOrDefault (current , new ArrayList <>()))
78+ {
79+ if (visitedOtherSide .contains (neighbor ))
80+ {
81+ return true ;
82+ }
83+ if (!visitedThisSide .contains (neighbor ))
84+ {
5285 visitedThisSide .add (neighbor );
5386 queue .add (neighbor );
5487 }
5588 }
5689 }
5790 return false ;
5891 }
59-
60- public static void main (String [] args ) {
61- Map <Integer , List <Integer >> graph = new HashMap <>();
62- graph .put (0 , Arrays .asList (1 , 2 ));
63- graph .put (1 , Arrays .asList (0 , 3 ));
64- graph .put (2 , Arrays .asList (0 , 3 , 4 ));
65- graph .put (3 , Arrays .asList (1 , 2 , 5 ));
66- graph .put (4 , Arrays .asList (2 , 5 ));
67- graph .put (5 , Arrays .asList (3 , 4 ));
68-
69- int start = 0 ;
70- int goal = 5 ;
71-
72- boolean pathExists = bidirectionalBFS (graph , start , goal );
73- System .out .println ("Path from " + start + " to " + goal + ": " + pathExists );
74- }
7592}
0 commit comments