Graph Cheatsheet¶
Use this page when the graph model is mostly clear but the algorithm family still feels ambiguous.
Do Not Use When¶
- the graph model itself is still unclear
- the task is really a tree DP, flow modeling, or DSU structure question, not a generic graph-family choice
- you need proofs or full algorithm derivations more than a chooser
Choose By Edge Model¶
- unweighted shortest path -> BFS -> Shortest Paths hot sheet
- trusted topological order on a DAG -> Shortest Paths hot sheet
0/1weights ->0-1 BFS-> Shortest Paths hot sheet- nonnegative weights -> Dijkstra -> Shortest Paths hot sheet
- negative edges -> Bellman-Ford -> Shortest Paths hot sheet
- delete one edge/vertex and connectivity changes -> Low-Link hot sheet
- use every edge exactly once -> Eulerian hot sheet
- connectivity under offline add/remove edge events -> DSU Rollback hot sheet
- binary clauses over boolean choices -> Two-SAT hot sheet
- two rooted trees must match up to child permutation, or one unrooted comparison reduces to one or two center roots -> Tree Isomorphism hot sheet
- one rooted subtree aggregate becomes one contiguous interval -> Subtree Queries hot sheet
- one query marks only a small subset of vertices but still needs the union of their paths -> Virtual Tree hot sheet
- static tree problem split by one balanced pivot or
O(log n)centroid ancestors -> Centroid hot sheet - tree topology changes online but path queries still survive -> Link-Cut Tree hot sheet
- tree topology changes online but the live query is one subtree side of an existing edge -> Euler Tour Tree hot sheet
- one undirected weighted graph has no designated
s-t, and you only need the cheapest disconnection cut -> Global Min-Cut hot sheet - one undirected weighted graph needs many pairwise min-cuts -> Gomory-Hu hot sheet
- edge lower / upper bounds with conservation at every node -> Flow With Lower Bounds hot sheet
- two equal-sized sides with full strict preference lists and a no-blocking-pair objective -> Stable Marriage hot sheet
- one dense square cost matrix with exact one-to-one pairing -> Hungarian hot sheet
- one undirected graph with odd cycles where the objective is still maximum matching size -> General Matching hot sheet
Core Families¶
- connectivity / traversal -> BFS, DFS
- critical roads / cities -> Low-Link hot sheet
- use-every-edge-once walks -> Eulerian hot sheet
- DAG dependencies -> toposort
- undirected cheapest backbone -> MST
- directed compression -> SCC
- binary clause feasibility -> Two-SAT hot sheet
- rooted unordered tree shape comparison -> Tree Isomorphism hot sheet
- subtree-only rooted-tree aggregation -> Subtree Queries hot sheet
- marked-subset compression on one static rooted tree -> Virtual Tree hot sheet
- tree path queries -> LCA or HLD hot sheet
- balanced recursive tree splits / centroid ancestors -> Centroid hot sheet
- dynamic-tree path queries / connectivity under online
linkandcut-> Link-Cut Tree hot sheet - dynamic-tree subtree-side queries under online
linkandcut-> Euler Tour Tree hot sheet - capacities -> Flow hot sheet
- one global undirected cut with no designated pair -> Global Min-Cut hot sheet
- all-pairs min-cut compression on one undirected graph -> Gomory-Hu hot sheet
- lower-bounded circulation / mandatory edge throughput -> Flow With Lower Bounds hot sheet
- pairing -> Matching hot sheet
- stable one-to-one pairing under strict preferences -> Stable Marriage hot sheet
- weighted one-to-one assignment -> Hungarian hot sheet
- non-bipartite maximum matching -> General Matching hot sheet
Core Invariants¶
- BFS: first visit gives minimum number of edges in an unweighted graph
- Dijkstra: settled distances stay optimal when all edge weights are nonnegative
0-1 BFS: deque order stays nondecreasing by distance- Bellman-Ford: one more relaxation after
n - 1rounds signals a reachable negative cycle
Retrieval Cues¶
- "fewest edges" or "minimum moves" -> start from BFS
- "weights are only 0 and 1" -> do not jump to Dijkstra first
- "negative edge" or "check for negative cycle" -> Bellman-Ford family
- "cheapest transport under capacities" -> Min-Cost Flow hot sheet
- "each row and each column must be used exactly once, and the score is additive" -> Hungarian hot sheet
- "each side ranks the other side, and the answer must have no blocking pair" -> Stable Marriage hot sheet
- "the graph is undirected, odd cycles are allowed, and you still want the largest disjoint edge set" -> General Matching hot sheet
- "many or all vertex pairs ask for their undirected min cut" -> Gomory-Hu hot sheet
- "plain max flow is still the model, but you want a height/excess preflow engine rather than Dinic" -> Push-Relabel hot sheet
- "every edge has both minimum and maximum allowed flow" -> Flow With Lower Bounds hot sheet
- "remove one road or city and connectivity breaks" -> Low-Link hot sheet
- "use every road / teleporter exactly once" -> Eulerian hot sheet
- "every object has two modes and each constraint touches two literals" -> Two-SAT hot sheet
- "same unlabeled rooted tree shape, child order irrelevant" -> Tree Isomorphism hot sheet
- "same unrooted tree shape, and the only ambiguity should be one or two centers" -> Tree Isomorphism hot sheet
- "one rooted subtree" or "all descendants of one node" -> Subtree Queries hot sheet
- "one query marks a small subset of tree vertices, and only the branch points between them matter" -> Virtual Tree hot sheet
- "tree paths" -> LCA for ancestor/meet questions, HLD for repeated static path aggregates, Euler flattening for subtree-only aggregation
- "tree paths, but the split is through one balanced center or one logarithmic ancestor chain" -> Centroid hot sheet
- "tree paths, but the edges themselves change online" -> Link-Cut Tree hot sheet
- "link-cut tree still feels mysterious because the auxiliary splay tree is the missing piece" -> Splay Tree hot sheet + Link-Cut Tree hot sheet
- "the edges change online, but the query is still subtree-shaped on one incident edge" -> Euler Tour Tree hot sheet
- "same component after many merges" -> maybe DSU, not BFS/DFS
- "same component after adds and deletes, but all events are known first" -> DSU Rollback hot sheet
Invariants To Keep In Mind¶
- shortest-path code only updates
parent[v]whendist[v]improves - heap Dijkstra must skip stale entries
0-1 BFSkeeps deque order nondecreasing by distance- Bellman-Ford only gives a valid shortest-path answer when no reachable negative cycle keeps improving the target
Quick Anchors In This Repo¶
- shortest-path family -> Shortest Paths hot sheet + Message Route + QOS
- bridges / cut vertices / block-cut entry -> Low-Link hot sheet + Necessary Roads + bridges-articulation-lowlink.cpp
- Eulerian trail / cycle -> Eulerian hot sheet + Mail Delivery + eulerian-path-cycle.cpp
- overlap-state sequence construction -> De Bruijn Sequence hot sheet + De Bruijn Sequence + de-bruijn-sequence-binary.cpp
- MST -> Road Reparation
- SCC / DAG ordering -> Course Schedule
- 2-SAT / implication graph -> Two-SAT hot sheet + Giant Pizza + two-sat.cpp
- rooted tree shape comparison -> Tree Isomorphism hot sheet + Tree Isomorphism I + tree-isomorphism-rooted.cpp
- subtree-only tree aggregation -> Subtree Queries hot sheet + Subtree Queries + euler-tour-subtree.cpp
- marked-subset tree compression -> Virtual Tree hot sheet + Kingdom and its Cities + virtual-tree.cpp
- tree path decomposition -> Heavy-Light Decomposition + Path Queries II + hld-path-max.cpp
- HLD retrieval route -> HLD hot sheet
- centroid-tree recursion -> Centroid hot sheet + Ciel the Commander + centroid-decomposition.cpp
- dynamic tree path aggregate -> Link-Cut Tree hot sheet + Dynamic Tree Vertex Add Path Sum + link-cut-tree-path-sum.cpp
- auxiliary-splay refresher before dynamic trees -> Splay Tree hot sheet + Splay Tree
- dynamic tree subtree-side aggregate -> Euler Tour Tree hot sheet + Dynamic Tree Vertex Add Subtree Sum + euler-tour-tree-subtree-sum.cpp
- offline dynamic connectivity -> DSU Rollback hot sheet + Dynamic Connectivity
- flow / cuts -> Flow hot sheet + Police Chase
- preflow / height-label max-flow sibling -> Push-Relabel hot sheet + Maximum Flow + push-relabel-max-flow.cpp
- one global undirected cut with no designated pair -> Global Min-Cut hot sheet + Minimum Cut + stoer-wagner-global-mincut.cpp
- all-pairs cut compression -> Gomory-Hu hot sheet + MCQUERY + gomory-hu-tree.cpp
- lower-bounded circulation -> Flow With Lower Bounds hot sheet + Reactor Cooling + flow-lower-bounds.cpp
- min-cost transport -> Min-Cost Flow hot sheet + MINCOST
- matching / bipartite pairing -> Matching hot sheet
- stable bipartite pairing under strict preferences -> Stable Marriage hot sheet + Stable Marriage + gale-shapley-stable-marriage.cpp
- weighted assignment / dense cost matrix -> Hungarian hot sheet + Task Assignment + hungarian-assignment.cpp
- general non-bipartite matching -> General Matching hot sheet + General Matching + edmonds-blossom.cpp
- general-matching / edge-cover transformation compare point -> QBFLOWER
Next Stops¶
- Shortest Paths topic
- Graphs ladder
- Shortest Paths hot sheet
- HLD hot sheet
- Centroid hot sheet
- Link-Cut Tree hot sheet
- DSU Rollback hot sheet
- Flow hot sheet
- Push-Relabel hot sheet
- Global Min-Cut hot sheet
- Gomory-Hu hot sheet
- Flow With Lower Bounds hot sheet
- Min-Cost Flow hot sheet
- Matching hot sheet
- Stable Marriage hot sheet
- Hungarian hot sheet
- General Matching hot sheet
- Low-Link hot sheet
- Eulerian hot sheet
- Two-SAT hot sheet
- Tree Isomorphism hot sheet
- Subtree Queries hot sheet
- Virtual Tree hot sheet
- Euler Tour Tree hot sheet
- Template library
- Flow topic
- Global Min-Cut topic
- Gomory-Hu topic
- LCA topic
- Tree Isomorphism topic
- Virtual Tree topic
- Link-Cut Tree topic
- Euler Tour Tree topic