Skip to content

Chapter 3: Graph Foundations

Overview

Graphs are everywhere in competitive programming — grids, trees, networks, dependency chains. This chapter teaches you to see graph structure in problems and apply the right traversal or shortest-path algorithm. BFS and DFS are the backbone; Dijkstra and Bellman-Ford handle weighted paths; Kruskal builds minimum spanning trees. If a problem involves connections between things, you'll reach for something in this chapter.

Coming from: Chapter 2 — Data Structures — segment trees, DSU, and the data structures that power efficient algorithms
Going to: Chapter 4 — Dynamic Programming — DP state design from 1D recurrences to bitmask and tree DP
Bridge: BRIDGE-to-dp.md — the link between graph structure and dynamic programming

Topics at a Glance

#TopicDifficultyKey Trigger Phrase
01Graph RepresentationBeginner"Store a graph" — adjacency list vs. matrix
02BFSBeginner"Shortest path in unweighted graph"
03DFS and Tree DFSBeginner"Visit all reachable nodes" or "cycle detection"
04Bipartite GraphsIntermediate"Two-color the graph" or "split into two groups"
05Topological SortIntermediate"Ordering with prerequisites" (DAG)
06DP on DAGIntermediate"Longest path in DAG" or "count paths"
07Dijkstra's AlgorithmIntermediate"Shortest path" + non-negative weights
08Bellman-FordIntermediate"Shortest path" + negative weights or "detect negative cycle"
09Floyd-WarshallIntermediate"All-pairs shortest paths" with n ≤ 500
10MST — KruskalIntermediate"Minimum cost to connect all nodes"
11MST — PrimIntermediate"MST on dense graphs"
12Euler PathIntermediate"Traverse every edge exactly once"
13Functional GraphsIntermediate"Each node has exactly one outgoing edge" (permutation cycles)
14LCA with Binary LiftingIntermediate"Lowest common ancestor" or "distance on tree"

If You Only Read 3 Files

  1. DFS and Tree DFS — because DFS is the foundation of almost every graph algorithm: cycle detection, connected components, topological sort, bridges, SCCs. If you only learn one traversal, make it DFS.
  2. Dijkstra's Algorithm — because "shortest path" is one of the most common problem types on Codeforces, and Dijkstra with a priority queue is the standard tool from Div 2 C onward.
  3. LCA with Binary Lifting — because tree path queries (distance, maximum edge, etc.) appear constantly, and binary lifting is the clean O(n log n) approach that extends to many related problems.

Cross-Chapter Connections


File Listing

FileTopicLevel
graph-representationGraph Representation
bfsBreadth-First Search
dfs-and-tree-dfsDFS and Tree DFS
topological-sortTopological Sort⭐⭐
bipartite-graphsBipartite Graphs⭐⭐
dijkstraDijkstra's Algorithm⭐⭐
bellman-fordBellman-Ford Algorithm⭐⭐
floyd-warshallFloyd-Warshall Algorithm⭐⭐
mst-kruskalMST — Kruskal's Algorithm⭐⭐
mst-primMST — Prim's Algorithm⭐⭐
dp-on-dagDP on DAG⭐⭐
euler-pathEuler Path and Circuit⭐⭐
functional-graphsFunctional Graphs⭐⭐
lca-binary-liftingLCA with Binary Lifting⭐⭐

Suggested Reading Order

  1. graph-representation — adjacency list, matrix, and edge list
  2. bfs — level-by-level traversal and shortest paths in unweighted graphs
  3. dfs-and-tree-dfs — depth-first traversal, the backbone of graph algorithms
  4. topological-sort — linear ordering of DAG vertices
  5. bipartite-graphs — two-coloring and matching foundations
  6. dijkstra — shortest paths with non-negative weights
  7. bellman-ford — shortest paths with negative weights
  8. floyd-warshall — all-pairs shortest paths in O(n³)
  9. mst-kruskal — minimum spanning tree via sorted edges + DSU
  10. mst-prim — minimum spanning tree via greedy vertex growth
  11. dp-on-dag — dynamic programming along topological order
  12. euler-path — traverse every edge exactly once
  13. functional-graphs — graphs with out-degree 1 and cycle detection
  14. lca-binary-lifting — lowest common ancestor via sparse ancestor table

Built for the climb from Codeforces 1100 to 2100.