Lesson 14: Graphs — BFS & DFS
Think of a map of cities connected by roads, or friends on a social network. Anything that's 'points and relationships between them' is a 'graph': nodes connected by edges. Two ways to tour a graph: BFS spreads ring by ring like a ripple in water — great for the shortest path; DFS dives deep like ex
BFS is like a ripple in water: you start at one point and spread out ring by ring, the nearest first. DFS is like walking a maze — you go all the way down one path, and if you get stuck you back up and try another.
- graph traversal
- Systematically visiting every node reachable from a start node, using BFS (a queue) or DFS (a stack/recursion).
- adjacency dict
- A graph representation where each node maps to its list of neighbors: {node: [neighbors]} — O(1) access to neighbors.
- visited set
- A set marking which nodes were already scanned, to avoid revisiting them and prevent infinite loops.