Esc
  • Loading…
↑ ↓ to moveEnter to openEsc to close

Graph

Nodes and edges for modeling networks, dependencies, and paths between things.

  • 1 Lesson

Dry-run engine

Breadth-First Search

A graph in memory is just an array of neighbour lists — BFS reads them level by level.

O(V + E) Time
Input · Start
bfs(A)
Output · Visit order
Press play to run it
How it works

Dequeue the front node, then scan its adjacency list and enqueue every neighbour not yet visited. Marking on enqueue guarantees each node enters the queue exactly once.

adjacency_list[6]

  • curr—
  • Queue[A]
  • Visited{A}
  • Order—

The graph lives in memory as an adjacency list: one array of neighbours per node. BFS enqueues A and marks it visited at distance 0.

1 / 7

Course Contents