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
On this page
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)
chop, curry, plate. In Airflow, a task; in dbt, a model.A dependency (edge)
A valid order
A cycle
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 readBig-O and performance for data engineers
Read complexity from code, find hidden quadratics, stream instead of loading, and measure.
Related lessons
Heaps, top-k and k-way mergesAirflow · 5 min read
A DAG file, line by line
Previous: Sliding windows, sweeps and binary search
Primary sources: graphlib