Introduction
Build systems, CI pipelines and workflow engines all schedule tasks with dependencies. Underneath they combine graph algorithms (topological order, cycle detection, critical path) with concurrency (start each task as soon as its inputs are ready). This project builds both halves and keeps the output deterministic by printing results only after everything has finished.
Problem statement
NAME COST [after DEP,...] [flaky N], separated by run. For each job, reject duplicate names, unknown dependencies and cycles (showing the cycle). Plan topological waves of tasks that can run in parallel and compute the critical path and the best possible speed-up. Execute the tasks on a four-thread pool, starting each as soon as its dependencies complete; retry flaky tasks up to three attempts, mark tasks that still fail, and skip tasks whose dependencies didn't complete. Print each task's status and result.Objectives
- Detect cycles with a three-colour depth-first search
- Produce topological waves with Kahn's algorithm
- Compute a critical path with dynamic programming
- Compose CompletableFutures to respect dependencies
Features
- Graph validation with cycle reporting
- Parallel waves, critical path and speed-up bound
- Dependency-driven parallel execution
- Retries, failures and skipped dependents
Concepts demonstrated
- Directed acyclic graphs
- Topological sorting
- Critical path
- Asynchronous composition
- Fault handling
System architecture
TaskGraph validates dependencies, finds cycles by DFS (a grey node seen again is a back edge), computes waves with Kahn's algorithm (sorting names within a wave for stable output), and finds the critical path by computing each task's earliest finish over the topological order. Executor creates one CompletableFuture per task in wave order, each starting with allOf(dependencies).thenApplyAsync(...) on the pool; the task body checks that every dependency succeeded, simulates transient failures for flaky tasks, and computes a deterministic CPU-bound result (counting primes). Outcomes go into a ConcurrentHashMap and are printed by Main in wave order once all futures have joined.
Folder structure
Complete source code
src/Executor.java
What each file does
- src/TaskGraph.java
- Task definitions, validation, cycle detection, waves and the critical path.
- src/Executor.java
- CompletableFuture-based execution with retries, failure propagation and the simulated work.
- src/Main.java
- Parses jobs and task lines, prints the plan and the results.
How to run it
Compile
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 build pipeline: NAME COST [after DEPS] [flaky N] checkout 1 deps 3 after checkout compile 4 after deps lint 2 after checkout unit 3 after compile integration 5 after compile flaky 2 docs 2 after checkout package 2 after unit,integration,lint deploy 1 after package,docs run # A task that always fails takes its dependents down with it fetch 2 parse 3 after fetch flaky 5 report 1 after parse backup 2 after fetch run # Invalid: a cycle a 1 after c b 1 after a c 1 after b run # Invalid: unknown dependency x 1 after y run
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Job 1: the critical path is checkout → deps → compile → integration → package → deploy (16 of 23 units); integration succeeds after 2 retries.
- Job 2: parse fails three times, so report is skipped while backup still runs.
- Jobs 3 and 4 are rejected for a cycle (a → c → b → a) and an unknown dependency.
Common errors
A task starts before its dependency finishes
Build each task's future from allOf(its dependencies' futures), not from the order of submission.
The output order changes between runs
Collect outcomes in a concurrent map and print them afterwards in a fixed (wave) order.
Possible improvements
- Add per-task timeouts with orTimeout
- Cache results so unchanged tasks are skipped (incremental builds)
- Export the graph in Graphviz DOT format
Additional challenges
- 1Cancel all remaining tasks as soon as one fails (fail-fast mode).
- 2Limit tasks tagged 'io' to two at a time with a Semaphore.
- 3Measure real elapsed time and compare it with the critical-path bound.
Built and tested it yourself? Mark it to update your dashboard.