Breadth-First Search
Breadth-First Search (BFS) is a graph traversal algorithm that explores all vertices at the current depth level before moving to vertices at the next depth level. It uses a queue data structure and is useful for finding shortest paths in unweighted graphs.
Breadth-First Search Visualization
Complexity
O(V + E)O(V)Pseudocode
function BFS(graph, start):
queue = [start]
visited = {start}
while queue is not empty:
current = queue.dequeue()
for each neighbor in graph[current]:
if neighbor not in visited:
visited.add(neighbor)
queue.enqueue(neighbor)Related Algorithms
Depth-First Search
Depth-First Search (DFS) is a graph traversal algorithm that explores as far as possible along each branch before backtracking. It uses a stack (or recursion) and is useful for topological sorting, detecting cycles, and solving maze problems.
Dijkstra's Algorithm
Dijkstra's algorithm finds the shortest path from a source vertex to all other vertices in a weighted graph with non-negative edge weights. It uses a priority queue and is widely used in routing and network protocols.
A* Algorithm
A* is an informed search algorithm that finds the shortest path using both the actual distance from the start and an estimated distance to the goal (heuristic). It's optimal and efficient for pathfinding in games and robotics.