Introduction
Project planning tools answer three questions: in what order can tasks run, what is the shortest possible duration, and how long will it take with the people available? This scheduler answers all three with classic graph algorithms.
Problem statement
Objectives
- Implement Kahn's topological sort
- Detect and report cycles
- Compute earliest/latest start times and slack (critical path method)
- Write a list scheduler with heaps
Features
- Dependency validation and cycle reporting
- Stable topological order
- Earliest start, finish and slack per task
- Critical path
- Multi-worker schedule and Gantt chart
Concepts demonstrated
- Directed acyclic graphs
- Topological sorting
- Critical path method
- Greedy list scheduling
System architecture
parse builds Task objects and links each to the tasks that depend on it. topological_order repeatedly removes tasks with no remaining prerequisites (alphabetical ties) and, if some remain, find_cycle follows dependencies until it repeats. critical_path runs a forward pass for earliest starts and a backward pass for latest starts and each task's tail (longest chain to the end). schedule simulates time: whenever a worker is idle it starts the ready task with the longest tail; otherwise it advances to the next finish event.
Folder structure
Complete source code
main.py
What each file does
- main.py
- Parsing, topological sorting with cycle detection, the critical-path passes, the worker schedule and the report.
How to run it
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# A small website project workers 2 task plan 2 : gather requirements task design 3 after plan : wireframes and colours task db 4 after plan : database schema task api 5 after db : back-end API task ui 4 after design : front-end pages task content 2 after plan : write page text task wire 2 after api,ui : connect pages to the API task test 3 after wire,content : testing task launch 1 after test : deploy
Testing
- Run
python main.py < tests/sample-input.txtand compare withtests/expected-output.txt. - The critical path is plan -> db -> api -> wire -> test -> launch, giving 17 days.
- content has 9 days of slack and design and ui have 2.
- With two workers the schedule still finishes in 17 days because db (on the critical path) starts first.
Common errors
The scheduler starts short tasks first and the project takes longer
Prioritise ready tasks by their tail (longest remaining chain), not by duration or name.
Infinite loop on circular dependencies
Detect leftover tasks after the topological sort and report the cycle instead of scheduling.
Possible improvements
- Read tasks from CSV
- Assign tasks to named workers with skills
- Add calendar dates and skip weekends
Additional challenges
- 1Show how the duration changes from 1 to 5 workers.
- 2Allow tasks to be split across days.
- 3Export the schedule as a Mermaid Gantt diagram.
Built and tested it yourself? Mark it to update your dashboard.