Advanced Graph Algorithms
Beyond Basic Graphs
Once you’ve mastered BFS, DFS, and shortest paths, it’s time for advanced graph techniques. These algorithms appear in harder CF problems (1700+) and ICPC regionals.Pattern Recognition Signals:
- “Strongly connected” → SCC (Kosaraju/Tarjan)
- “Bridge/cut vertex” → Bridge finding algorithm
- “Boolean satisfiability” → 2-SAT
- “Bipartite matching” → Hungarian/Kuhn’s algorithm
- “Maximum flow” → Ford-Fulkerson/Dinic
Strongly Connected Components (SCC)
An SCC is a maximal set of vertices where every vertex is reachable from every other. Intuition: In a directed graph, SCCs are “clusters” where you can get from any node to any other within the cluster. The graph of SCCs forms a DAG (no cycles possible, since any cycle would merge SCCs). Why it matters:- Simplify complex graphs by collapsing SCCs to single nodes (condensation)
- Solve 2-SAT problems
- Analyze dependencies and reachability
Kosaraju’s Algorithm
The Insight: If we process nodes in order of decreasing finish time from a DFS, and run DFS on the reversed graph, each DFS tree corresponds to an SCC. Why does reversing work? In the original graph, if there’s a path from SCC A to SCC B, then in the reversed graph there’s a path from B to A. By processing in finish-time order, we ensure we start from “sink” SCCs (no outgoing edges to other SCCs), so DFS on reversed graph stays within one SCC. Algorithm:- Run DFS on original graph, recording finish order (node goes on stack when its DFS completes)
- Process nodes in reverse finish order, running DFS on reversed graph
- Each DFS tree in step 2 is an SCC
Tarjan’s Algorithm
Single DFS, more efficient in practice. Key Concepts:- disc[u]: Discovery time of node u (when we first visit it)
- low[u]: Lowest discovery time reachable from u’s subtree via tree edges and at most one back edge
- onStack: Whether node is in the current DFS path (potential SCC members)
low[u] == disc[u]. This means no node in u’s subtree can reach anything discovered before u, so u and everything on the stack down to u form an SCC.
Visual Example:
Bridges and Articulation Points
Finding Bridges
An edge is a bridge if removing it disconnects the graph. Key Insight: Edge (u, v) where u is the parent of v in DFS tree is a bridge if and only iflow[v] > disc[u]. This means v cannot reach u or any ancestor of u through any path other than (u, v).
Why low[v] > disc[u] and not >=?
- If
low[v] == disc[u], v can reach u through another path (a back edge from v’s subtree to u) - Such a back edge would give us an alternative path, so (u, v) wouldn’t be a bridge
Finding Articulation Points
A vertex is an articulation point if removing it disconnects the graph. Two Cases:- Root of DFS tree: u is an AP iff it has 2+ children in the DFS tree. If root has only one child, removing root leaves one connected component.
-
Non-root node: u is an AP iff some child v has
low[v] >= disc[u]. This means v (and its subtree) cannot reach any ancestor of u—so removing u would disconnect v’s subtree.
>= for APs but > for bridges?
- For bridges: we’re removing an edge, so even if v can reach u via another path, the graph stays connected
- For APs: we’re removing vertex u itself, so even if v can reach exactly u (low[v] = disc[u]) via a back edge, that doesn’t help—u is still gone!
2-SAT
Solve boolean satisfiability with clauses of form (x OR y). The Problem: Given n boolean variables and m clauses of form “(a OR b)”, find an assignment that satisfies all clauses, or report impossibility. The Key Transformation: Each clause is equivalent to two implications:- (if a is false, b must be true)
- (if b is false, a must be true)
- Create 2n nodes: for each variable x, one node for x=true and one for x=false
- For clause (a OR b): add edges (NOT a → b) and (NOT b → a)
- x ⇒ … ⇒ NOT x ⇒ … ⇒ x
- This means x ⇔ NOT x, which is a contradiction!
- Find all SCCs using Tarjan/Kosaraju
- If any variable has both states in same SCC → UNSATISFIABLE
- Otherwise: in topological order of SCCs, assign TRUE to variables whose TRUE-node comes after FALSE-node (later in topo order = “stronger”)
| Statement | Clause | Edges to Add |
|---|---|---|
| At least one of a, b | (a ∨ b) | ¬a→b, ¬b→a |
| At most one of a, b | (¬a ∨ ¬b) | a→¬b, b→¬a |
| Exactly one of a, b | Both above | All 4 edges |
| a must be true | (a ∨ a) | ¬a→a |
| a implies b | (a ⇒ b) | a→b, ¬b→¬a |
2-SAT Example
Bipartite Matching
Kuhn’s Algorithm (Hungarian)
Maximum Flow (Dinic’s Algorithm)
Eulerian Path/Circuit
Path that visits every edge exactly once.Practice Problems
Intermediate (1400-1700)
Advanced (1700-2000)
Key Takeaways
SCC
Kosaraju or Tarjan for strongly connected components.
Bridges/APs
Use low-link values from DFS tree.
2-SAT
Model as implication graph, solve with SCC.
Max Flow
Dinic’s O(V²E) for general, O(E√V) for bipartite.
Next Up
Back to Overview
Return to the complete course curriculum.