RecapDifficulty: Advanced1 min

Recap: Graph Modelling

Name the vertex, the edge, its direction and its weight, and the question then names the algorithm: search, order or group.

A problem hides a graph when it has things, links between pairs of them, and a question about reaching, ordering or grouping. Modelling decides the vertex, the edge, whether the edge has a direction and whether it has a weight; the question then picks the algorithm.

What the problem says, and the graph and algorithm it leads to
The problem saysVertexEdgeAlgorithm
Fewest steps from one thing to anotherThe thingOne step, no direction or weightBreadth-first search
Fewest moves in a puzzleA state: everything the next move depends onOne move, built when reachedBreadth-first search over states
Do this before thatA taskFrom what comes first to what needs itTopological sort; a short order means a loop
Records that share a detail belong togetherA recordA shared detailUnion-find
Fewest changes between routesA routeTwo routes that meetBreadth-first search from every route at the start

A state must work as a key: a tuple in Python, a record in Java and a string in TypeScript. Depth-first search with a visited set finds a route but not the fewest moves, and the choice of vertex decides what a search counts.