Retrieval: which graph algorithm?

Advanced Graphs

Retrieval: which graph algorithm?

Two problems rebuilt cold, plus the preconditions that pick the algorithm for you.

Key idea

Read the constraints, not the story

Every choice in this unit is forced by a precondition stated in the problem. Find the precondition and the algorithm follows.

SituationUseBecause
Unweighted, fewest stepsbreadth-first searchequal edges make first arrival cheapest
Non-negative weights, cheapest pathheap-based greedypopping finalizes, nothing can improve it
Negative weights, or a cap on edges usedrelaxation in roundsfinalization is not valid
Connect everything at least total costspanning treenot a path problem at all
Use every edge exactly oncerecord on dead end, reversegreedy forward walk can strand

Tip

And the meta-skill

Two of this unit's problems hide the graph. When a problem has no explicit edges but describes constraints, ordering, or transformations, write down what a node is and what an edge means before anything else.

Which algorithm?

Flights between cities have prices, some routes offer a rebate making the effective price negative, and you need the cheapest total from one city to another. Which approach is correct?

Network Delay Time, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

Loading the workspace…

Course Schedule II, from scratch

You have solved this one before. Rebuild it from scratch without looking at your old submission. If the approach does not come back within a few minutes, that is the signal that it needs another pass.

Loading the workspace…
← Previous