← All topics

Lesson

Graphs — BFS & DFS

Explore nodes and edges — connectivity, components, and traversals.

Medium → HardTime O(V+E)Space O(V)
Practice problems
Loading visualization…

Loading solution…

Representation

Prefer adjacency lists. Grid graphs are implicit: 4-direction neighbors with bounds checks.

BFS vs DFS

BFS for shortest unweighted path / levels. DFS for components, topology prep, and exhaustive search.

Studio

Graph BFS paints frontier vs visited as the queue expands.

Pitfalls

  • !Missing visited → infinite loops
  • !Directed vs undirected edges
  • !1-index vs 0-index nodes

Practice from this topic

Showing 24 of the related problems in the 1000-question bank.

View all →

Got the lesson?

Mark Graphs — BFS & DFS as understood when the ideas stick.