Skip to content
Tech Interview Prep
Technical interview guide

Advanced Graphs

Weighted shortest paths and connectivity beyond plain BFS/DFS: Dijkstra, Union-Find, and minimum spanning trees.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v8
Editorial status
Reviewed

Scope: Language-neutral, with implementation notes referring to CPython 3.14 containers.

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

Kruskal: O(Elog⁡E),Prim (heap): O(Elog⁡V),Prim (matrix): O(V2)\text{Kruskal: } O(E\log E), \quad \text{Prim (heap): } O(E\log V), \quad \text{Prim (matrix): } O(V^2) Repeated Dijkstra: O(V(V+E)log⁡V) versus Floyd-Warshall: O(V3)\text{Repeated Dijkstra: } O(V(V+E)\log V) \text{ versus Floyd-Warshall: } O(V^3) Ford-Fulkerson (DFS): O(E⋅f),Edmonds-Karp: O(VE2),Dinic: O(V2E),Hopcroft-Karp: O(EV)\text{Ford-Fulkerson (DFS): } O(E\cdot f), \quad \text{Edmonds-Karp: } O(VE^2), \quad \text{Dinic: } O(V^2E), \quad \text{Hopcroft-Karp: } O(E\sqrt{V}) Bitmask DP over n vertices: O(2nn2) time,224⋅24⋅8≈3.2×109 bytes\text{Bitmask DP over } n \text{ vertices: } O(2^n n^2) \text{ time}, \quad 2^{24}\cdot 24\cdot 8 \approx 3.2\times 10^{9} \text{ bytes}

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.
cheapest connection route cost capacity or assignment Problem Objective MST: Kruskal or Prim Dijkstra or A-star Max flow or Hopcroft-Karp

When to use and when not to

QuestionUseAvoid when
Cheapest links connecting all sitesMST (Kruskal sparse, Prim dense)You need per-pair route distance
One-source shortest pathDijkstraNegative edge weights
All-pairs distancesFloyd-Warshall if dense, repeated Dijkstra if sparseSparse graphs with Floyd-Warshall
Capacity or assignment limitsMax flow; Hopcroft-Karp for bipartiteGreedy matching, which stops at maximal, not maximum
Longest pathDAG only, relax in topological orderGeneral digraph (NP-hard)
Exact tour over many stopsBitmask DP up to about n = 20Large n: approximation with a stated ratio, metric instances only

Traps interviewers probe

  1. 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.
  2. Implementing only one union-find optimisation. Rank alone or compression alone gives a logarithmic bound. Fix: use both, and test with a chain workload.
  3. 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.
  4. 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).
  5. 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:

  1. Name the reduction, such as MST, flow or DP over subsets.
  2. State the bound and the data structure.
  3. State the size or density at which it stops being practical.
  4. Name the approximation ratio and the assumption it needs, or say the result is exact.
  5. 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.