Overview
Curated: · Written: · Reviewed:
Advanced Graphs
In one line
Use MST for the cheapest set of links that connects everything, Dijkstra or A-star for one-source routes with non-negative weights, SCC and union-find for reachability, and max flow for capacity and assignment questions. First check whether the problem reduces to one of these standard forms, and check hardness before designing anything new.
Key definitions
- Cut property: the lightest edge crossing any vertex partition belongs to some minimum spanning tree.
- SCC: a maximal set of vertices that are mutually reachable. Condensation contracts each SCC to one vertex, giving a DAG.
- Residual edge: the paired reverse edge with capacity equal to the flow pushed, which lets flow be rerouted.
- Admissible heuristic: never overestimates the remaining cost. Consistent: h(u) <= c(u,v) + h(v), so each vertex is finalised once.
- Low-link: the lowest discovery index reachable from a DFS subtree using one back edge. If low[v] > disc[u] for tree edge (u,v), that edge is a bridge.
- Eulerian versus Hamiltonian: the Eulerian test is a degree check in linear time, while Hamiltonian is NP-complete.
Formulas and complexity
At n = 24 the TSP table is about 3 GiB, and the transitions are about 2^24 x 24^2 ≈ 9.7 x 10^9.
How it works
- Kruskal sorts edges and adds each one whose endpoints lie in different components. Prim grows one tree by repeatedly taking the lightest crossing edge.
- Augmenting-path flow pushes bottleneck flow along residual paths. Shortest (BFS) augmenting paths bound the number of augmentations regardless of capacity values. The max flow equals the min cut capacity.
- The low-link pass runs one DFS and records disc and low. A child whose low is at least the parent's disc makes the parent an articulation point.
- Tarjan's algorithm emits SCCs in reverse topological order. Run DP over the condensation DAG to handle component-level propagation.
When to use and when not to
| Question | Use | Avoid when |
|---|---|---|
| Cheapest links connecting all sites | MST (Kruskal sparse, Prim dense) | You need per-pair route distance |
| One-source shortest path | Dijkstra | Negative edge weights |
| All-pairs distances | Floyd-Warshall if dense, repeated Dijkstra if sparse | Sparse graphs with Floyd-Warshall |
| Capacity or assignment limits | Max flow; Hopcroft-Karp for bipartite | Greedy matching, which stops at maximal, not maximum |
| Longest path | DAG only, relax in topological order | General digraph (NP-hard) |
| Exact tour over many stops | Bitmask DP up to about n = 20 | Large n: approximation with a stated ratio, metric instances only |
Traps interviewers probe
- Assuming the MST path is a shortest path. In a triangle with weights 1, 1 and 1.5, the MST path A-B-C costs 2 against the direct 1.5. Fix: choose the tool from the objective, total cost or per-pair distance.
- Implementing only one union-find optimisation. Rank alone or compression alone gives a logarithmic bound. Fix: use both, and test with a chain workload.
- Placing the intermediate-vertex loop in Floyd-Warshall anywhere but outermost. With i, j, k order, d[0][1] stays 10 on the example instead of 3. Fix: k outermost, checked against repeated Dijkstra.
- Undoing unions after path compression. Compression rewrites parents without a record, so rollback leaves stale roots. Fix: union by rank with an explicit stack of (child, old rank).
- Quoting the 2x MST tour bound on a non-metric instance. In the depot example d(P,S)=40 exceeds d(P,R)+d(R,S)=7, so the guarantee does not apply. Fix: verify the triangle inequality first.
Interview tips
Open by naming the standard problem and its hardness class, then the structure that decides it, for example: this is a min-cut question, so the cut capacity bounds the flow.
Expected follow-ups: Kruskal or Prim at this density; what breaks with negative weights; what happens when capacities scale by a factor of a million; whether an approximation ratio holds on this instance; and how you verified the result.
30-second outline:
- Name the reduction, such as MST, flow or DP over subsets.
- State the bound and the data structure.
- State the size or density at which it stops being practical.
- Name the approximation ratio and the assumption it needs, or say the result is exact.
- Say how you verified it: a brute-force reference on thousands of small random graphs.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.