All episodes · From a List to Your Map App · Episode 21

Which Step Comes First? Topological Sorting and the Critical Path

Pressing play loads the video from YouTube.

Building a bookshelf, some steps have to wait for others. Which order works, and how fast can the whole thing be done?
We draw the six steps as a graph, then keep taking out whatever waits for nothing, using a queue, to get a valid order. One bad rule (the sides waiting for the doors) creates a loop, and the queue runs dry with five steps stuck. Finally we give each step its minutes: the longest chain, sides, back, doors and done, sets the total at 33 minutes. That's the critical path, and the shelves have two minutes to spare.

Every animation in this video is drawn by code, and the narration is an AI voice.

Transcript

Building a bookshelf, some steps have to wait for others. The doors can't go on before the back and the shelves. So which order works, and how fast can the whole thing be done? Today: topological sorting.

Draw each step as a card, with an arrow from a step to anything that has to wait for it. Six steps, six arrows.

Start with every step that waits for nothing, here sides and feet, and put them in a queue from episode three. Take one out, do it, and cross off its arrows. Any step with nothing left to wait for joins the queue. Repeat until the queue is empty: sides, feet, back, shelves, doors, done. A valid order.

But add one bad rule: the sides need the doors first. Now the doors wait for the sides, and the sides wait for the doors. No step in that loop is ever free. The queue runs dry with steps left over, and that's how we know there's no possible order.

Now give each step its minutes. With a few helpers, steps that don't wait for each other can happen at the same time. The longest chain decides the total: sides, back, doors, done. Thirty-three minutes. That's the critical path: be one minute late on it, and everything is a minute late. The shelves have two minutes to spare.

Do what's free, cross off arrows, repeat, and keep an eye on the longest chain. That's how project plans and build tools choose an order. Next time: remember what you've already worked out, and some slow problems suddenly become fast. See you there.

Next episode: dynamic programming. Remember what you've already worked out, and slow problems suddenly become fast.

Buy the Developer a Coffee

Entirely up to you: it works the same either way.

Every cup turns into the next update.

WeChat Pay QR code
WeChat Pay
Alipay QR code
Alipay
PayPal QR code
PayPal