Advanced Graph Algorithms
π At a Glance
| Aspect | Details |
|---|---|
| Time to Read | 35 minutes |
| Prerequisites | Part 11 (Graph Traversal), Part 12 (Shortest Path) |
| Key Concepts | Strongly Connected Components, Articulation Points, Max Flow, Bipartite Matching |
| Difficulty | ββββ (Advanced) |
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β ADVANCED GRAPH ALGORITHMS LANDSCAPE β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β CONNECTIVITY FLOW NETWORKS β β βββββββββββββββββββ βββββββββββββββββββ β β β Strongly β β Maximum Flow β β β β Connected β β βββββββββββββββ β β β β Components β β Ford-Fulkerson β β β β βββββββββββββββ β β Edmonds-Karp β β β β Tarjan's O(V+E) β β Push-Relabel β β β β Kosaraju's β βββββββββββββββββββ β β βββββββββββββββββββ β β β β β β β βΌ βΌ β β βββββββββββββββββββ βββββββββββββββββββ β β β Critical Points β β Min Cut β β β β βββββββββββββββ β β βββββββββββββββ β β β β Articulation β β Max-Flow = β β β β Points β β Min-Cut β β β β Bridges β β (Duality) β β β βββββββββββββββββββ βββββββββββββββββββ β β β β β βΌ β β βββββββββββββββββββ β β β Bipartite β β β β Matching β β β β βββββββββββββββ β β β β Hungarian Alg β β β β Hopcroft-Karp β β β βββββββββββββββββββ β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π― What You'll Learn
After completing this article, you will be able to:
- Find SCCs: Implement Tarjan's and Kosaraju's algorithms
- Identify critical points: Find articulation points and bridges
- Solve flow problems: Apply Ford-Fulkerson and Edmonds-Karp
- Match bipartite graphs: Use Hungarian algorithm for optimal assignment
- Apply min-cut: Understand max-flow min-cut duality
π₯ Production Story: The Microservice Dependency Nightmare
A fintech company's platform grew to 200+ microservices. One Monday morning, a seemingly minor deployment caused a cascade failure affecting 40% of the system.
The Incident
08:15 - Payment service deployed new version 08:17 - Order service health checks failing 08:19 - Inventory service unresponsive 08:22 - Customer dashboard showing errors 08:25 - Trading halted, revenue loss: $50K/minute
The team had no visibility into service dependencies. They couldn't answer:
- Which services form tightly coupled groups?
- Which services, if they fail, would partition the system?
- What's the maximum throughput between any two services?
The Investigation
The architect analyzed the call graph:
JAVA(14 lines)CodeLoading syntax highlighter...
The Solution
They applied advanced graph algorithms:
JAVA(40 lines)CodeLoading syntax highlighter...
The Impact
| Metric | Before | After |
|---|---|---|
| Mean Time to Recovery | 45 minutes | 8 minutes |
| Cascade failures/month | 12 | 1 |
| Critical services identified | 0 | 23 |
| Deployment confidence | Low | High |
π§ Mental Model: The City Infrastructure
Think of advanced graph algorithms as city planning tools:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β CITY INFRASTRUCTURE β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β STRONGLY CONNECTED ARTICULATION POINTS β β COMPONENTS (Single Points of Failure) β β βββββββββββββββββββ βββββββββββββββββββ β β β Downtown β β β β β β βββββ βββββ β β A βββ B β β β β β A ββββ B β β β β β β β β βββ¬ββ βββ¬ββ β β βΌ β β β β β β²β± β β β [C] ββββββββ β C is β β β β β±β² β β β β critical β β β βββ΄ββ βββ΄ββ β β βΌ β β β β β C ββββ D β β β D βββ E β β β β βββββ βββββ β β β β β β All reachable β βββββββββββββββββββ β β β from each other β β β βββββββββββββββββββ β β β β MAX FLOW BIPARTITE MATCHING β β (Traffic Capacity) (Job Assignment) β β βββββββββββββββββββ βββββββββββββββββββ β β β 8 5 β β Workers Jobs β β β β A ββββ B ββββ β β A ββββ 1 β β β β β β T β β B βββ¬β 2 β β β β β3 β2 β β C ββββ 3 β β β β βΌ βΌ β β β β β β S ββββ C ββββ β β Maximum β β β β 6 4 β β matching = 3 β β β βββββββββββββββββββ βββββββββββββββββββ β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- SCC = Neighborhoods where you can walk from any point to any other
- Articulation Point = The only bridge connecting two parts of city
- Max Flow = Maximum traffic the road network can handle
- Bipartite Matching = Assigning taxi drivers to passengers optimally
π¬ Deep Dive: Strongly Connected Components
Understanding SCCs
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β SCC VISUALIZATION β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Original Graph: SCCs Identified: β β β β 1 βββ 2 βββββββββββ β β β β β SCC 1 β β β βββββ 3 βββ 4 β {1,2,3} ββββ {4} βββ {5,6} β β β βββββββββββ SCC2 SCC3 β β 5 βββ 6 β β ββββββ β β β β Within SCC: bidirectional reachability β β Between SCCs: DAG (no cycles possible) β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Kosaraju's Algorithm
Kosaraju's uses two DFS passes - one on original graph, one on reversed:
JAVA(67 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β KOSARAJU'S INTUITION β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Pass 1: Record finish times β β βββββββββββββββββββββββββββββ β β Vertices that finish later can reach more vertices β β (they're "upstream" in the SCC DAG) β β β β SCC_A βββ SCC_B βββ SCC_C β β β² β β β β β Finishes last (can reach B and C) β β β β Pass 2: DFS on reversed graph β β βββββββββββββββββββββββββββββ β β Starting from highest finish time: β β - Can only reach vertices in SAME SCC β β - Reverse edges prevent crossing to other SCCs β β β β SCC_A βββ SCC_B βββ SCC_C (reversed) β β β β β βββ Start here, stays in SCC_A β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Tarjan's Algorithm
Tarjan's is more elegant - single DFS pass using low-link values:
JAVA(75 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β TARJAN'S LOW-LINK β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Discovery order: 1 β 2 β 3 β 4 β β β β 1 βββββ 2 ids: 1 2 3 4 β β β β lowLinks: 1 1 1 4 β β βββββββ 3 βββββ 4 β β β β Back edge 3β1 updates lowLinks: β β - lowLink[3] = min(3, id[1]) = 1 β β - lowLink[2] = min(2, lowLink[3]) = 1 β β - lowLink[1] = min(1, lowLink[2]) = 1 β β β β SCC roots: vertices where id[v] == lowLink[v] β β - Vertex 1: id=1, lowLink=1 β ROOT of SCC {1,2,3} β β - Vertex 4: id=4, lowLink=4 β ROOT of SCC {4} β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Comparison
| Aspect | Kosaraju's | Tarjan's |
|---|---|---|
| DFS passes | 2 | 1 |
| Extra space | O(V) for reverse graph | O(V) for stack |
| Simplicity | Easier to understand | More elegant |
| Constants | Slightly higher | Lower |
| Output order | Reverse topological | Reverse topological |
π¬ Deep Dive: Articulation Points and Bridges
Articulation Points
JAVA(81 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β ARTICULATION POINT CONDITIONS β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Condition 1: Root with 2+ children β β ββββββββββββββββββββββββββββββββββ β β βββββ β β β R β β Root β β β± β² β β βββββ βββββ β β β A β β B β Two subtrees β β βββββ βββββ β β β β If R is removed, A and B are disconnected β β (children in DFS tree have no other connection) β β β β Condition 2: low[v] >= discovery[u] β β βββββββββββββββββββββββββββββββββββ β β 1 β β β±β β β 2 β discovery[1]=0, low[1]=0 β β β±β β discovery[2]=1, low[2]=0 (back edge to 1) β β 3 β β discovery[3]=2, low[3]=2 (no back edge!) β β β β β β βββ low[3] >= discovery[2] β 2 is articulation β β (3 can't reach above 2 without going through 2) β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Bridges
JAVA(63 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β BRIDGE vs ARTICULATION POINT β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Articulation: low[v] >= discovery[u] β β Bridge: low[v] > discovery[u] β β β β Example where u is articulation but (u,v) is NOT bridge: β β β β 1 βββ 2 β β β β±β β β β β± β Multiple edges between 2 and 3 β β β β± β low[3] = discovery[2] (can reach 2 via other) β β 3 β±ββββ β β β β Vertex 2: articulation (low[3] >= discovery[2]) β β Edge 2-3: NOT bridge (low[3] = discovery[2], not >) β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π¬ Deep Dive: Maximum Flow
The Flow Network
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β FLOW NETWORK CONCEPTS β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Source (S): produces flow β β Sink (T): consumes flow β β Capacity: maximum flow an edge can carry β β Flow: actual flow through an edge β β β β capacity β β 10 β β S ββββββββββ A ββββββββββ T β β flow=7 8 β β β β Constraints: β β 1. Capacity: flow(e) β€ capacity(e) for all edges β β 2. Conservation: flow_in(v) = flow_out(v) for v β S,T β β 3. Non-negative: flow(e) β₯ 0 β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Ford-Fulkerson Method
The idea: repeatedly find augmenting paths and push flow:
JAVA(70 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β RESIDUAL GRAPH INTUITION β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Original: After flow=3: Residual: β β β β cap=5 flow=3 res=2 (can push 2 more) β β S ββββββ T S ββββββ T S ββββββ T β β ββββββ β β res=3 (can "undo" 3) β β β β Backward edge allows: β β - Correcting suboptimal path choices β β - "Rerouting" flow through better paths β β β β Example where backward edges are essential: β β β β ββββ B ββββ β β β 1β1 β β β 1 β β β 1 β β S ββββ΄βββ A ββββ΄βββ T β β 1 β β β β Path 1: SβBβAβT (flow=1) β β Without backward: stuck at max_flow=1 β β With backward: SβAβBβT uses backward AβB, total flow=2 β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Edmonds-Karp Optimization
Using BFS guarantees O(VEΒ²) complexity:
JAVA(87 lines)CodeLoading syntax highlighter...
Complexity Analysis
| Algorithm | Time | Notes |
|---|---|---|
| Ford-Fulkerson (DFS) | O(E Γ max_flow) | Can be slow with large capacities |
| Edmonds-Karp (BFS) | O(VEΒ²) | Polynomial, practical |
| Dinic's | O(VΒ²E) | Better for dense graphs |
| Push-Relabel | O(VΒ²E) or O(VΒ³) | Best for very dense graphs |
π¬ Deep Dive: Min-Cut Max-Flow Theorem
The Duality
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β MAX-FLOW MIN-CUT DUALITY β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β ββββββ 5 ββββββ β β β β β β S ββ€ββββ 10 ββββββββΌββ T β β β β β β ββββββ 3 ββββββ β β β β Max flow = 18 (5 + 10 + 3) β β Min cut = 18 (cut all edges between S and T) β β β β More complex example: β β ββββ 10 βββ B βββ 10 βββ β β β β β β β S ββ€ 5 βββ T β β β β β β β ββββ 10 βββ C βββ 10 βββ β β β β Bottleneck is edge BβC (capacity 5) β β Max flow = 15 (10 through B, 5 can cross to C, + 10 through C) β β Actually: SβBβT: 10, SβCβT: 10, blocked by B-C β β Min cut = {BβC, one of S edges} but optimally just 15 β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Finding the Min-Cut
After max-flow, perform BFS from source on residual graph:
JAVA(33 lines)CodeLoading syntax highlighter...
π¬ Deep Dive: Bipartite Matching
Maximum Bipartite Matching
Find the largest set of edges with no shared vertices:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β BIPARTITE MATCHING β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Workers Jobs Matching β β β β A βββββββββββ 1 A βββββββ 1 β β β β² β β β β β² β β β B ββββ²βββββββ 2 B βββββββ 2 β β β β² β± β β β β² β± β β C βββββββ³ββββ 3 C βββββββ 3 β β β² β β β² β β D βββββββββ²ββ 4 D (unmatched) β β β β Maximum matching = 3 β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Hungarian Algorithm (Kuhn's Algorithm)
JAVA(58 lines)CodeLoading syntax highlighter...
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β AUGMENTING PATH IN MATCHING β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€ β β β Current matching: A-1, B-2 β β C wants to match but only connects to 1 and 2 β β β β A βββββββ 1 Try Cβ1: 1 matched to A β β β Can A find alternative? β β B βββββββ 2 Aβ2: 2 matched to B β β β Can B find alternative? β β C βββββββ 1,2 Bβ3: 3 unmatched! Success! β β β β β (unmatched) 3 New matching: A-2, B-3, C-1 β β β β Augmenting path: C β 1 β A β 2 β B β 3 β β (alternating unmatched, matched, unmatched, matched, unmatch) β β β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Reducing to Max-Flow
Bipartite matching can be solved as max-flow:
JAVA(29 lines)CodeLoading syntax highlighter...
Hopcroft-Karp Algorithm
For better complexity O(EβV):
JAVA(96 lines)CodeLoading syntax highlighter...
β οΈ Common Mistakes
Mistake 1: Wrong Low-Link Update in Tarjan's
JAVA(9 lines)CodeLoading syntax highlighter...
Mistake 2: Missing Reverse Edges in Flow
JAVA(14 lines)CodeLoading syntax highlighter...
Mistake 3: Articulation Point Root Check
JAVA(9 lines)CodeLoading syntax highlighter...
Mistake 4: Visited Array in Bipartite Matching
JAVA(11 lines)CodeLoading syntax highlighter...
π Debug This: SCC with Incorrect DFS Ordering
This Tarjan's SCC implementation has a subtle bug. Can you find it?
JAVA(54 lines)CodeLoading syntax highlighter...
π Click to reveal the bug
lowLinks[neighbor] instead of ids[neighbor].JAVA(9 lines)CodeLoading syntax highlighter...
Consider graph: 0 β 1 β 2 β 0 DFS from 0: Visit 0: id=0, low=0, stack=[0] Visit 1: id=1, low=1, stack=[0,1] Visit 2: id=2, low=2, stack=[0,1,2] Back edge 2β0: With bug: low[2] = min(2, low[0]) = min(2, 0) = 0 β Actually works here, but... Consider: 0 β 1 β 2 β 1 (where 2β1 is back edge) With correct algorithm, when we see back edge 2β1: low[2] = min(low[2], id[1]) = min(2, 1) = 1 But if low[1] was updated before we return: The buggy version might use an outdated low[1]
The fix ensures we always reference the stable discovery time, not the potentially-changing low-link value.
π» Exercises
Exercise 1: Find All SCCs ββ
Implement both Kosaraju's and Tarjan's algorithms and verify they produce the same SCCs.
JAVA(6 lines)CodeLoading syntax highlighter...
Vertices: 8 Edges: 0β1, 1β2, 2β0, 2β3, 3β4, 4β5, 5β3, 6β5, 6β7, 7β6 Expected SCCs: {0,1,2}, {3,4,5}, {6,7}
Exercise 2: Critical Infrastructure ββ
Given a network of servers and connections, find:
- All articulation points (critical servers)
- All bridges (critical connections)
JAVA(10 lines)CodeLoading syntax highlighter...
Exercise 3: Maximum Flow with Multiple Sources βββ
Modify Edmonds-Karp to handle multiple sources and sinks.
JAVA(6 lines)CodeLoading syntax highlighter...
Exercise 4: Job Assignment Problem βββ
i can do jobs in skills[i], find maximum number of jobs that can be completed (each worker does at most one job).JAVA(9 lines)CodeLoading syntax highlighter...
Exercise 5: Minimum Vertex Cover ββββ
In a bipartite graph, find the minimum set of vertices that covers all edges. Use KΓΆnig's theorem: minimum vertex cover = maximum matching.
JAVA(7 lines)CodeLoading syntax highlighter...
- Start BFS from unmatched left vertices
- Alternating between unmatched and matched edges
- Minimum vertex cover = (matched left not reached) βͺ (right reached)
π€ Interview Questions
Q1: "When would you use Tarjan's vs Kosaraju's SCC algorithm?"
- Single DFS pass
- Lower constant factors
- More complex to implement correctly
- Outputs SCCs in reverse topological order
- Two DFS passes (original + reversed graph)
- Easier to understand and implement
- Requires building reverse graph (extra O(V+E) space)
- More intuitive proof of correctness
Q2: "Explain the max-flow min-cut theorem and a practical application."
- Maximum flow is limited by some bottleneck
- That bottleneck is exactly the minimum cut
Problem: Separate foreground from background in image Model: - Each pixel is a vertex - Source connects to "definitely foreground" pixels (β capacity) - Sink connects to "definitely background" pixels (β capacity) - Adjacent pixels connected with capacity = similarity Min-cut finds optimal boundary between foreground/background (minimizes cutting through similar pixels)
Q3: "How would you find if a graph has a unique topological ordering?"
JAVA(26 lines)CodeLoading syntax highlighter...
Q4: "Design a system to match drivers to riders optimally."
JAVA(31 lines)CodeLoading syntax highlighter...
- Real-time updates as drivers/riders appear
- Batch matching every few seconds
- Consider fairness (don't always assign closest)
Q5: "What's the relationship between articulation points and 2-edge-connected components?"
2-vertex-connected: Remains connected after removing any single vertex 2-edge-connected: Remains connected after removing any single edge Relationship: - Every bridge's endpoints are articulation points (unless degree 1) - Articulation point doesn't always imply bridge exists Example: A --- B | X | (X marks diagonal edges) C --- D No bridges (every edge is in a cycle) No articulation points (2-vertex-connected) vs: A --- B --- E | | C --- D B is articulation point B-E is a bridge
π Quick Reference
Algorithm Complexity
| Algorithm | Time | Space | Use Case |
|---|---|---|---|
| Kosaraju's SCC | O(V+E) | O(V+E) | Find strongly connected components |
| Tarjan's SCC | O(V+E) | O(V) | Find SCCs, single pass |
| Articulation Points | O(V+E) | O(V) | Find critical vertices |
| Bridges | O(V+E) | O(V) | Find critical edges |
| Ford-Fulkerson | O(E Γ max_flow) | O(VΒ²) | Max flow (integer capacities) |
| Edmonds-Karp | O(VEΒ²) | O(VΒ²) | Max flow (polynomial) |
| Hungarian | O(VΒ³) | O(VΒ²) | Min cost bipartite matching |
| Hopcroft-Karp | O(EβV) | O(V) | Max bipartite matching |
Key Formulas
SCC Root Condition: ids[v] == lowLinks[v] Articulation (root): children > 1 Articulation (non-root): lowLinks[child] >= ids[v] Bridge Condition: lowLinks[v] > ids[parent] Max-Flow = Min-Cut: Always true (duality theorem) KΓΆnig's Theorem: Min Vertex Cover = Max Matching (bipartite)
Code Templates
JAVA(14 lines)CodeLoading syntax highlighter...
π What's Next?
- Social network analysis
- Recommendation systems
- PageRank algorithm
- Community detection
- Real-world case studies
π Review Schedule
| Day | Focus | Time |
|---|---|---|
| 0 | Full read + exercises | 90 min |
| 1 | Quick Reference + SCCs | 10 min |
| 3 | Implement Tarjan's from memory | 20 min |
| 7 | Max-flow problem | 15 min |
| 14 | Debug exercise + bipartite matching | 15 min |
| 30 | All interview questions | 10 min |