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

Lessons

  1. 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. 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. 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. 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

Module quiz

1. Which algorithm finds the fewest moves in an unweighted graph?
2. How many components does a graph with 5 vertices and no edges have?
3. Dijkstra's algorithm needs the edge weights to be:
4. In a directed graph DFS meets a vertex whose colour is 1 (on the current path). What does it mean?
5. What do path compression and union by size give union-find?
6. n = 300 and you need the distance between every pair of vertices. What fits?

‹ Math and bitsFinal assessment ›