Algorithms from zero
Graphs: multiple-choice review
Six questions that cut across the whole unit. Each one picks the algorithm or representation a problem actually demands — not a definition to recite, but a modeling decision to defend.
Confirm you can connect representation choice, traversal order, ordering, weighted shortest paths, and dynamic connectivity — the synthesis the eight lessons built toward.
A social graph has 50 million users and ~200 friendships each (so E is roughly 5 billion directed entries, far below V squared). The hot operation is 'iterate a user's friends'. Which representation, and why?
You must find the route between two map locations with the fewest road segments, ignoring distance. You only need hop count. Which algorithm is the right first reach, and what makes it correct?
A build system runs Kahn's algorithm over its dependency DAG and the output order lists only 18 of the 20 build targets. What does that tell you?
A pricing graph has some edges that represent refunds (negative cost). An engineer runs Dijkstra and gets answers that are sometimes too high. Why, and what is the fix?
Edges arrive one at a time over a stream, and after each edge you must answer 'are u and v in the same component yet?' instantly. Which structure fits, and why is re-running BFS the wrong choice?
A teammate claims their union-find is O(1) per operation because they added path compression. What is the precise complexity, and what is missing or overstated?
The unit’s through-line is one modeling decision tree: pick the representation by density (list for sparse, matrix for dense or O(1) edge lookup); pick the traversal by what you need (BFS for fewest-edge paths and levels, DFS for depth and cycle structure); add weights and BFS yields to Dijkstra — but only with non-negative edges, else Bellman-Ford. Topological sort orders a DAG and detects cycles for free, and union-find answers dynamic connectivity in amortized O(alpha(n)) where re-running BFS would be wasteful. Match the algorithm to the problem’s shape before writing a line of code.