πŸ•ΈοΈ Part 5: Graph Algorithms

Learn to see graphs in problems and solve them efficiently. BFS, DFS, trees, Union-Find, and Kruskal's MST β€” the core of USACO Silver.

πŸ“š 4 Chapters Β· ⏱️ Estimated 2-3 weeks Β· 🎯 Target: Reach USACO Silver level

Part 5: Graph Algorithms

Estimated time: 2–3 weeks

Graphs are everywhere in competitive programming: mazes, networks, family trees, city maps. Part 5 teaches you to see graphs in problems and solve them efficiently.


What Topics Are Covered

ChapterTopicThe Big Idea
Chapter 5.1Introduction to GraphsRepresenting graphs; adjacency lists; types of graphs
Chapter 5.2BFS & DFSTraversal, shortest paths, flood fill, connected components
Chapter 5.3Trees & Special GraphsTree traversals; Union-Find; Kruskal's MST
Chapter 5.4Shortest PathsDijkstra, Bellman-Ford, Floyd-Warshall, SPFA

What You'll Be Able to Solve After This Part

After completing Part 5, you'll be ready to tackle:

  • USACO Bronze:

    • Flood fill (count connected regions in a grid)
    • Reachability problems (can cow A reach cow B?)
    • Simple BFS shortest paths in grids/graphs
  • USACO Silver:

    • BFS/DFS on implicit graphs (states rather than explicit nodes)
    • Multi-source BFS (distance to nearest obstacle/fire)
    • Union-Find for dynamic connectivity
    • Graph connectivity under edge additions
    • Tree problems (subtree sums, depths, LCA)

Key Algorithms Introduced

TechniqueChapterTime ComplexityUSACO Relevance
DFS (recursive & iterative)5.2O(V + E)Connectivity, cycle detection
BFS5.2O(V + E)Shortest path (unweighted)
Grid BFS5.2O(R Γ— C)Maze problems, flood fill
Multi-source BFS5.2O(V + E)Distance to nearest source
Connected components5.2O(V + E)Counting disconnected regions
Tree traversals (pre/post-order)5.3O(N)Subtree aggregation
Union-Find (DSU)5.3O(Ξ±(N)) β‰ˆ O(1)Dynamic connectivity
Kruskal's MST5.3O(E log E)Minimum spanning tree
Dijkstra's algorithm5.4O((V + E) log V)SSSP on non-negative weighted graphs
Bellman-Ford5.4O(V Γ— E)SSSP with negative edges; detect negative cycles
Floyd-Warshall5.4O(VΒ³)All-pairs shortest paths on small graphs
SPFA5.4O(V Γ— E) worstPractical Bellman-Ford with queue optimization

Prerequisites

Before starting Part 5, make sure you can:

  • Use vector<vector<int>> for adjacency lists (Chapters 2.3–3.1)
  • Use queue and stack from STL (Chapter 3.1, 3.5)
  • Work with 2D arrays and grid traversal (Chapter 2.3)
  • Understand basic nested loops (Chapter 2.2)
  • Use priority_queue (Chapter 3.1) β€” needed for Chapter 5.4 (Dijkstra)

Tips for This Part

  1. Chapter 5.1 is mostly setup β€” read it to understand graph representation, but the real algorithms start in Chapter 5.2.
  2. Chapter 5.2 (BFS) is one of the most important chapters for USACO Silver. Grid BFS appears in roughly 1/3 of Silver problems.
  3. The dist[v] == -1 pattern for unvisited nodes in BFS is the key. Never mark visited when you pop β€” always when you push.
  4. Chapter 5.3's Union-Find is faster to code than BFS for connectivity questions. Memorize the 15-line template β€” you'll use it constantly.
  5. Chapter 5.4 (Dijkstra) is essential for weighted shortest path problems. Use priority_queue<pair<int,int>> with the standard template β€” it's the most common Silver/Gold graph algorithm.

πŸ’‘ Key Insight: Most USACO graph problems are actually grid problems in disguise. A grid cell (r,c) becomes a graph node; adjacent cells become edges. BFS on this implicit graph finds shortest paths.

πŸ† USACO Tip: Whenever you see "shortest path," "minimum steps," or "fewest moves" in a problem, think BFS immediately. Whenever you see "are these connected?" or "how many groups?", think DSU.