Begin Algo
Algorithms · 11 lessons

Graph AlgorithmsTraversal, shortest paths, ordering

Graphs describe maps, social networks, task dependencies — anything where things relate to other things. This topic starts with the two basic traversals and works up through cycle detection, topological sort, the shortest-path family and minimum spanning trees.

PrerequisitesQueueStackRecursionAdjacency list

Why learn Graph Algorithms

Where it shows up
Map navigation

Junctions are nodes and roads are edges. "Fewest turns" is BFS; "fastest in minutes" has to account for edge weights, and that is Dijkstra.

→ Lesson: BFS
"People you may know"

Everyone two hops from you is a friend of a friend. BFS working outward ring by ring maps exactly onto degrees of separation.

→ Lesson: BFS
Install and build order

A depends on B, B depends on C, so npm or make has to install C first. That is topological sort — and you need cycle detection first to be sure there is no circular dependency.

→ Lesson: Topological Sort
Laying network cable

Connect every data centre for the lowest total cost. That is a minimum spanning tree, and Kruskal's version uses union-find.

→ Lesson: MST: Kruskal & Prim

Lessons

11 lessons
#AlgorithmDifficulty
01BFS Breadth-first searchSpreads outward one ring at a time; made for shortest paths on unweighted graphsUsed for: Fewest steps, degrees of separation, crawling level by level02DFS Depth-first searchGo as deep as possible then back out; the basis of components and topological sortUsed for: Walking folders, flood fill, detecting circular dependencies03Grid as Graph Grids as graphsTreat a 2D array as a graph, where the four directions are the edgesUsed for: Counting islands, mazes, connected regions in an image04Cycle Detection Cycle detectionThree-colour marking on directed graphs, union-find on undirected onesUsed for: Circular dependencies, deadlock detection05Topological Sort Topological sortKahn's in-degree method and the DFS finishing-order methodUsed for: Package install order, course prerequisites, build pipelines06Bipartite Check Bipartite checkingTwo-colour it so neighbours never share a colourUsed for: Matching problems, splitting things that conflict07Dijkstra Single-source shortest pathsOn non-negative weights, settle distances one at a time with a priority queueUsed for: Fastest route in navigation, network routing08Bellman-Ford Shortest paths with negative weightsRelax V−1 rounds; if round V still relaxes, there is a negative cycleUsed for: Detecting currency arbitrage, paths with negative edges09Floyd-Warshall All-pairs shortest pathsA triple loop of DP, good for small dense graphsUsed for: Routing tables for small networks, any-pair distance lookups10Shortest Path in DAG Shortest paths on a DAGTopologically sort first, then relax in order — negative weights are fineUsed for: Critical path in project scheduling11MST: Kruskal & Prim Minimum spanning treesKruskal sorts edges and uses union-find; Prim uses a heapUsed for: Laying cable or fibre, cluster analysis