Graph algorithms
Adjacency lists, BFS, DFS and components, Dijkstra and Floyd–Warshall, and union-find with Kruskal's spanning tree.
About 9 hours of study
Learning objectives
- Store graphs and run BFS and DFS
- Find shortest paths
- Merge groups with union-find
Lessons
- 1
Graphs with the STL Intermediate
Adjacency lists as vector<vector<int>>, and breadth-first search for fewest moves on a grid or a graph.
- 2
Depth-first search and components Intermediate
Recursive depth-first search on an adjacency list or a grid, counting connected components, measuring them and finding cycles.
- 3
Shortest paths: Dijkstra and Floyd Intermediate
Dijkstra with a priority_queue for one start, Floyd–Warshall for all pairs on small graphs, and counting shortest paths.
- 4
Union-find and spanning trees Advanced
Disjoint set union with path compression and union by size: merge groups, ask if two items are together, and build a cheapest network with Kruskal.
Module practice
- CP: Degrees of the verticesEasy
- CP: Escape the mazeMedium
- CP: Break one wallHard
- CP: Count the groupsEasy
- CP: The largest islandMedium
- CP: Can the tasks be ordered?Hard
- CP: Cheapest cost to every cityEasy
- CP: Distances between any two citiesMedium
- CP: How many cheapest routes?Hard
- CP: Are they in the same group?Easy
- CP: Groups after each linkMedium
- CP: The cheapest networkHard