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 taskKnown hard problem
Pack files into the fewest 256 MB blocks without splitting anyBin packing
Spread tasks across workers so the last one finishes soonestMultiprocessor scheduling
Pick which summary tables to precompute within a storage budgetKnapsack
Choose the fewest source systems that together supply every needed columnSet cover
Group jobs into the fewest time windows so no 2 jobs writing the same table share oneGraph 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.

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

Which of these pipeline tasks is NP-hard?

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.