Bidirectional Search
About
Implementation Strategy
Implementation of Bidirectional Search
Graph Representation (Adjacency List)
import java.util.*;
class Graph {
private final Map<Integer, List<Integer>> adjList;
public Graph() {
this.adjList = new HashMap<>();
}
// Add edge in an undirected graph
public void addEdge(int source, int destination) {
adjList.putIfAbsent(source, new ArrayList<>());
adjList.putIfAbsent(destination, new ArrayList<>());
adjList.get(source).add(destination);
adjList.get(destination).add(source);
}
// Returns the adjacency list
public List<Integer> getNeighbors(int node) {
return adjList.getOrDefault(node, new ArrayList<>());
}
// Print the graph
public void printGraph() {
for (var entry : adjList.entrySet()) {
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
}
}Bidirectional Search Algorithm
Testing the Algorithm
Time Complexity Analysis
Why is Bidirectional Search Faster?
Last updated