When Problems Are Inherently Hard
The intermediate tier showed a search over every combination doubling its work with each added item. For the family of problems called NP-hard, nobody has found an algorithm that runs in polynomial time, O(n^k) for some fixed k, in the worst case. They stand or fall together: a polynomial-time algorithm for any of the problems in this section would give one for all of them, and decades of searching have produced none. When a data task turns out to be one in disguise, stop hunting for a fast exact algorithm and choose a heuristic.
Spotting them in pipeline work
| Pipeline task | Known hard problem |
|---|---|
| Pack files into the fewest 256 MB blocks without splitting any | Bin packing |
| Spread tasks across workers so the last one finishes soonest | Multiprocessor scheduling |
| Pick which summary tables to precompute within a storage budget | Knapsack |
| Choose the fewest source systems that together supply every needed column | Set cover |
| Group jobs into the fewest time windows so no 2 jobs writing the same table share one | Graph coloring |
Spreading files across workers shows how fast the search space explodes. Each file can go to any worker, so the number of placements is the worker count raised to the number of files, and adding 1 file multiplies the search by the worker count.
- 1.1 trillion
- 1.2 septillion
Exact on tiny inputs, a heuristic at scale
Longest processing time first (LPT) is a greedy heuristic: sort the files largest first and hand each to the least-loaded worker, kept at the top of a heap. The program compares it with exhaustive search on 7 files and 3 workers.
import heapq
from itertools import product
sizes = [7, 6, 5, 4, 4, 3, 3]
workers = 3
def slowest(assignment):
loads = [0] * workers
for size, worker in zip(sizes, assignment):
loads[worker] += size
return max(loads)
placements = list(product(range(workers), repeat=len(sizes)))
best = min(slowest(p) for p in placements)
heap = [(0, w) for w in range(workers)]
for size in sorted(sizes, reverse=True):
load, w = heapq.heappop(heap)
heapq.heappush(heap, (load + size, w))
greedy = max(load for load, w in heap)
print("placements tried:", len(placements))
print("optimal slowest worker:", best)
print("LPT slowest worker:", greedy)Output:
placements tried: 2187 optimal slowest worker: 11 LPT slowest worker: 12
Exhaustive search checked 2,187 placements to find a slowest worker of 11 GB. LPT made 7 heap pops and 7 pushes and landed on 12. It runs in O(n log n) at any size, and Ronald Graham proved in 1969 that its slowest worker never exceeds 4/3 of the optimum.
Knowing how close you are
No placement beats the larger of 2 lower bounds: the total size split evenly and rounded up, and the single largest file. Here that is max(ceil(32 / 3), 7) = 11, so LPT's 12 sits at most 1 GB above optimal, and you learn that without the exhaustive search. On 10,000 files the check is 1 pass.
Other moves when exact is out of reach
- Use a greedy rule with a proven bound: first-fit decreasing packing never uses more than 11/9 of the optimal bin count plus 1.
- Search exactly while n is small, cutting any branch that already exceeds the best answer found.
- Exploit small integers: when sizes and budget are whole numbers, memoized search runs in time proportional to items × budget. That is polynomial in the budget's value but exponential in its digit count, so a budget of 10,000 is fine and one in the trillions is out of reach.
- Improve and stop: start from greedy, move 1 item between workers, keep moves that lower the maximum, and halt at a time limit.
Which of these pipeline tasks is NP-hard?
- Remove duplicate rows by primary key
- Sort 1 billion rows by event timestamp
- Group 300 files into the fewest 1 GB blocks without splitting a file
- Find the 10 customers with the highest revenue
About this lesson
This section is part of the Complexity: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.