Graph Fundamentals and Traversals

Lesson 16 of 20

Six stops, six walkways: BFS sweeps the graph in waves from A while DFS dives corridor-deep — adjacency lists included.

The big picture

Graphs in one screen

A graph is the least pretentious data structure: dots and lines. Everything from metro maps to your friends list is one — and two traversals unlock it all.

Vertices + edges

Six campus stops joined by six walkways. Any “things connected to things” — friends, flights, web links — is a graph.

The adjacency list

adj.get(u) lists u’s neighbors — List<List<Integer>> in Java. Six lists, one per vertex, O(V + E) memory.

Degree math

A vertex’s degree is its neighbor count. All degrees sum to 2 × edges: 12 here means exactly 6 edges.

BFS: the wave

A queue sweeps outward layer by layer from the source — hop counts come out shortest in unweighted graphs.

DFS: the dive

A stack (or recursion) follows one corridor to its end, then backtracks to the last fork.

1736

Euler bridges Königsberg

A puzzle about seven bridges becomes vertices and edges on paper — the first graph.

1957

Moore exits the maze

Edward F. Moore uses a queue to explore a maze in rings — breadth-first search is born.

1972

Tarjan goes deep

Depth-first search becomes a machine for cycles, bridges and connected components.

today

Graphs everywhere

Social feeds, GPS routing, web crawling and garbage collectors all traverse graphs.