CoursesTrees & Graphs

Topological Sort

Lessons
9
Exercises
58
Minutes
62
  1. 1

    In-Degrees and Kahn's Peel

    Dependency edges point from prerequisite to dependent — count unmet prerequisites as in-degree, peel the zeros, and a valid order falls out.

    Complete codeMultiple choice
    6 exercises
    5 min
  2. 2

    Minimum Vertices to Reach All Nodes

    In a DAG, the smallest starting set is exactly the in-degree-0 nodes — nothing else can reach them, and everything sits downstream of them.

    Complete codeDebugMultiple choiceTrace
    6 exercises
    6 min
  3. 3

    Find All Possible Recipes

    Kahn's peel with a twist: supplies seed the ready queue, recipe-on-recipe ingredients form the edges, and a recipe cooks only when ALL its ingredients resolve.

    Complete codeDebugMultiple choiceTrace
    7 exercises
    7 min
  4. 4

    Course Schedule IV

    Is course u a prerequisite of course v — even through a chain? Precompute FULL reachability by propagating ancestor sets along topological order, then answer every query with one lookup.

    Complete codeMultiple choiceTrace
    6 exercises
    7 min
  5. 5

    All Ancestors of a Node in a DAG

    Who are v's ancestors? Flip the question: search FORWARD from each source u and stamp u on everything it reaches. Iterate sources 0..n-1 and every list comes out sorted for free.

    Complete codeMultiple choiceTrace
    7 exercises
    7 min
  6. 6

    Find Eventual Safe States

    Safe means EVERY path ends at a terminal — one path into a cycle disqualifies a node. Reverse the graph and run Kahn's peel from the terminals; whatever never gets peeled is on or feeds a cycle.

    Complete codeMultiple choiceTrace
    6 exercises
    7 min
  7. 7

    Topological Sort CheckpointCheckpoint

    No new concepts — carry the peel to unseen ground: package installs, alien alphabets, parallel semesters, and the question of whether an order is the ONLY order.

    Code orderMultiple choiceTrace
    6 exercises
    7 min
  8. 8

    Topological Sort ReviewReview

    Mixed recap of the island: in-degree bookkeeping, the ready-queue rule, what an incomplete peel proves, when to reverse the graph, why topo orders aren't unique, and the O(V+E) price tag.

    Complete codeMultiple choice
    6 exercises
    6 min
  9. 9

    Boss: Course ScheduleBoss

    The island's final test: can every course be finished? Read [a, b] as b BEFORE a — the arrow flows from prerequisite to dependent — then peel with Kahn's. Courses left unpeeled are the cycle's confession.

    Complete codeDebugCode orderMultiple choiceTrace
    8 exercises
    10 min
AlgoFox Pro

Trees & Graphs is part of AlgoFox Pro

You can read the whole course outline here. The exercises are in the app, along with your streak, your mistake review, and the progress that carries across your devices.

Pro starts with a 3 day free trial. The app store decides who is eligible for an introductory offer, and a lapsed subscription does not get another one.

Download AlgoFox on the App StoreGet AlgoFox on Google Play

Already subscribed in the app? Sign in with the same account and Pro works here too. Quick Play and Battle are free on the web, and so is the first course in every subject.