Codacoda
Back to Academy

searching

Breadth-First Search

Breadth-First Search (BFS) is a graph traversal algorithm that explores all vertices at the present depth level before moving on to vertices at the next depth level. It uses a queue to process nodes in order of their distance from the source. BFS naturally finds the shortest path in unweighted graphs.

Use Cases

  • Finding the shortest path in unweighted graphs
  • Level-order traversal of trees
  • Social network analysis (finding degrees of separation)

Complexity Analysis

MetricBestAverageWorst
TimeO(V + E)O(V + E)O(V + E)
SpaceO(V)

Visualization

4251810263ABCDEF
Speed:1x
Graph readyStep 1 / 8

Implementation

Output

Click "Run Code" to see output...