Skip to content
Gold 7 min read · Algorithms 1 practice problem ↓

Graphs and topological sort

Dependency graphs, Kahn's algorithm, cycle detection and how schedulers use them.

Foundations · Python for data engineers · Gold · Algorithms interviewers ask, lesson 3 of 3

Read first: Heaps, top-k and k-way merges (skip if you know it)

In plain words first

Cooking a meal: you must chop before you make the masala, make the masala before the curry, and finish the curry and the rice before you can plate. Some steps wait for others; some (washing the rice, chopping) can start straight away.

A task (node)

One step: chop, curry, plate. In Airflow, a task; in dbt, a model.

A dependency (edge)

"chop must finish before masala." An arrow from the step that must come first.

A valid order

Any order where every step comes after all the steps it needs. It is called a topological order.

A cycle

Two steps that each wait for the other. Then no valid order exists, and a scheduler must refuse.

There is often more than one valid order: washing the rice could happen first or fourth. Any order that respects every arrow is correct.

Unlock the full lesson

You have read the free part. The rest of this advanced lesson:

  • Pipelines are graphs
  • The graph in plain dicts
  • Kahn's algorithm
  • Cycles
  • What schedulers add
  • Common mistakes
  • Key takeaways
  • Interview prep with 3 follow-up questions
  • Check yourself: 3 questions

Unlock it with the Complete pass (every advanced problem and lesson) or on its own.

Practise this

Problems that use this, graded on hidden tests

Keep going

Up next · new module: Performance · 5 min read
Big-O and performance for data engineers
Read complexity from code, find hidden quadratics, stream instead of loading, and measure.

Related lessons

Previous: Sliding windows, sweeps and binary search

Primary sources: graphlib