Skip to content

Graphs

A graph is dots (nodes) joined by lines (edges): cities and roads, people and friendships, courses and prerequisites. Grids are graphs too. You explore with DFS (go deep) or BFS (go ring by ring, which finds shortest paths) and a visited set so you never loop forever.

After this topic: You can model a problem as a graph, traverse grids and adjacency lists, order tasks with topological sort and group nodes with union-find.

Do these first: Backtracking

0 of 13 solved0%

Step 1 · Read the lesson

Graphs: BFS / DFS / Topological Sort

Model relationships as nodes and edges; traverse with a visited set.

Step 2 · Solve the problems in order

Try each one for about 20 minutes first. Problems with a Run code tab are checked right here. If you are stuck, open Nudge, think again, then Idea. Go skeleton only gives the function shape, and Reference solution is for comparing after you have tried. Tick the box when you could solve it again without help.

  1. 3.Clone Graph Medium
  2. 4.Walls and Gates MediumLeetCode Premium
  3. 12.Graph Valid Tree MediumLeetCode Premium

Step 3 · What this unlocks