Graph algorithms
Putting tasks in a valid order, finding shortest paths with and without negative edges, and connecting every vertex as cheaply as possible.
About 9 hours of study
Learning objectives
- Run Kahn's algorithm and detect a cycle
- Find shortest paths with Dijkstra and Floyd–Warshall
- Say when Bellman–Ford is needed
- Build a minimum spanning tree with Kruskal or Prim
Lessons
- 1
Topological sort
Order the tasks of a directed acyclic graph so every task comes after the ones it depends on, with Kahn's algorithm.
- 2
Shortest paths: Dijkstra and Floyd–Warshall
The cheapest route on a weighted graph: Dijkstra from one start vertex, Floyd–Warshall between every pair.
- 3
Minimum spanning trees
Join every vertex at the smallest total cost: Kruskal with union-find, and Prim growing one tree.
Module practice
- Is this order allowed?Easy
- Order the tasksMedium
- How long will the project take?Hard
- Repair Floyd–WarshallEasy
- Shortest distancesMedium
- Shortest paths with negative edgesHard
- Is it a spanning tree?Easy
- Minimum spanning treeMedium
- Finish the networkHard
Module quiz
‹ Backtracking, greedy and dynamic programmingNumbers and recurrences ›