Constraints & Complexity Budgets
Read n and the time limit, and know which complexities fit.
A problem's constraints are a message from its author. "" says exponential time is welcome, "" says at most, and "at most 30 queries" says binary search. This lesson turns the message into arithmetic, then shows where arithmetic stops: when a conditional lower bound says that no much faster algorithm is likely to exist.
What you’ll learn
- Turn time and memory limits into operation and memory budgets, and invert a budget to the largest feasible for , and .
- Account for constant factors, caches, language speed, hidden parameters and output size when a budget is tight.
- Prove the information-theoretic lower bound for yes/no queries, and use it to read the query limits of interactive problems.
- State SETH and the 3SUM and APSP conjectures, prove that SETH implies the orthogonal vectors conjecture, and use such conditional bounds to decide when to stop optimizing.
Budgets, exactly
Polynomials. exactly when . At :
Exponentials. exactly when , and :
. The equation has no closed form, but the map converges quickly from to its fixed point:
The largest integer with is , so the second iterate, , is already within about 1%. At this budget an algorithm handles about 22 times fewer items than a linear one, because there.
A faster machine. Multiplying the speed by multiplies the feasible by for , but only adds for :
| Data | Entries | Memory |
|---|---|---|
| 32-bit integers | 40 MB | |
| 64-bit integers | 80 MB | |
| a table of 32-bit integers | 100 MB | |
| Held–Karp's table for 20 cities, 32-bit entries | 84 MB | |
| a bitset over values | bits | 125 MB |
| a Python list of distinct integers | about 36 MB (an 8-byte pointer plus a 28-byte object each), against 4 MB in C++ |
An edit-distance DP stores the full table for two strings of length each, as 32-bit integers. About how many megabytes does the table need?
Show the answer
400 MB
entries of 4 bytes: about 400 MB, over a 256 MiB limit even though steps fit in a second. Each row depends only on the previous one, so two rows of entries, about 80 KB, compute the distance; recovering the alignment as well in linear space is Hirschberg's trick, in Reconstructing Solutions & Saving Space.
Constants, caches and hidden parameters
hides constants, and when two candidate complexities are close at the actual , the constants decide. Compare , say a segment tree with a binary search inside every query, with , say a square-root decomposition. The two are exactly equal at , where , and at they differ by 15%: against . At that size, choose the one with the smaller constant or the simpler code.
Constants come from many places. A sequential scan of an array streams through the cache at memory bandwidth, while each cache miss on a pointer chase costs on the order of a hundred nanoseconds, the time of hundreds of arithmetic operations; this is why an array-based heap beats a pointer-based one, and why the Optimal lesson The Memory Hierarchy & Locality exists. Python adds a factor of 10 to 100 per simple operation, which is why the same solution can pass in C++ and fail in Python.
| The statement says… | What it changes |
|---|---|
| "the sum of over all test cases is at most " | total work is , but no test may cost time proportional to the largest possible |
| values are at most | arrays indexed by value: counting sort, a sieve, frequency tables |
| strings over 26 lowercase letters | a factor of 26 is affordable: one prefix-count table per letter |
| a second parameter | states are fine: bitmasks over , not over |
| all queries are known in advance | offline tricks: sort the queries, sweep, or Mo's algorithm in |
| "print every pair" | the output alone may be : aim for |
| "answer modulo " | the answer is huge: counting, almost always DP or combinatorics |
There are test cases, each with , and the sum of over all tests is at most . Your program clears an array of size at the start of every test, then runs an algorithm. What happens?
- It is fine: each test costs
- It is too slow: the clearing alone costs about steps over tests
- It is fine: clearing memory is free
- It is too slow: summed over the tests exceeds the budget
Show the answer
B. It is too slow: the clearing alone costs about steps over tests
The useful work is at most steps, but clearings of entries are steps, about 20 seconds at per second. Clear only the first entries, or tag entries with the test number instead of clearing.
Query limits and the information-theoretic bound
In an interactive problem the judge hides something and answers your questions, and the statement caps how many you may ask. The cap is a hint, because counting the possible answer patterns bounds every strategy at once.
Strategies are trees. The first question is the root, each answer leads to the next question, and each leaf announces a candidate. The number of questions asked about is the depth of the leaf that reaches.
One leaf per candidate. Two candidates reaching the same leaf get the same announcement, which is wrong for one of them, so there are at least leaves. A tree of depth with at most children per node has at most leaves:
On average. In a binary tree, leaf depths satisfy Kraft's inequality, . The geometric mean is at most the arithmetic mean, so
As an adversary. The same bound, told as a game: the judge answers each question so that the larger group of candidates stays consistent, and after yes/no questions at least candidates remain. Binary search, which halves the candidates every time, meets the bound exactly.
| A limit like… | Suggests |
|---|---|
| yes/no queries: 20 for , 30 for | binary search; every query must halve the candidates |
| comparisons to find the second largest | a knockout tournament, then the maximum among the players the winner beat |
| comparisons to find both the minimum and the maximum | compare in pairs: the larger of each pair competes for the maximum, the smaller for the minimum |
| 3 weighings to find the odd coin among 12, heavier or lighter | 3 outcomes per weighing: cases, so a strategy is not ruled out (and one exists) |
| comparisons to sort items | the counting bound itself; the lesson The Ω(n log n) Sorting Lower Bound develops it |
When to stop: conditional lower bounds
Your best algorithm is and : should you keep looking for something faster? For most problems in P nobody can prove a superlinear lower bound in a general model of computation. Fine-grained complexity answers conditionally instead: it reduces a few core problems to many others, so that a faster algorithm for any of them would break a conjecture that has resisted decades of attack.
Split the variables into two halves of and list all assignments of each half:
Give each half-assignment a vector with one coordinate per clause , set to 1 exactly when that half-assignment fails to satisfy on its own:
The full assignment satisfies exactly when one of the halves does, that is when . So the formula is satisfiable exactly when some pair of vectors is orthogonal:
Building the vectors costs , and the assumed OV algorithm, with , finishes in the time below. For -SAT, the sparsification lemma (Impagliazzo, Paturi & Zane, 2001) splits a -CNF, for any , into at most -CNFs with clauses each. With the total is , below for large , for every : SETH fails.
OV in turn reduces to sequence problems: vectors can be encoded in strings of length so that a strongly subquadratic algorithm for edit distance (Backurs & Indyk, 2015), longest common subsequence (Abboud, Backurs & Vassilevska Williams; Bringmann & Künnemann, both 2015) or dynamic time warping would solve it. Likewise 3SUM reduces to many problems in computational geometry (Gajentaan & Overmars, 1995), and APSP is subcubically equivalent to finding a negative-weight triangle (Vassilevska Williams & Williams, 2010).
Read the constraints with this in mind. If and the problem is general edit distance, an algorithm would be a research breakthrough, so look for an escape the instance offers: a small answer (distance at most : compute a band of width , ), small values (3SUM with values in : polynomial multiplication by the FFT, ), special structure, or an approximation.
Key takeaways
- An operation budget is speed times time, about per second. Invert it: for , for , for , and about for , which is 4.5 million at . Count bytes too: 32-bit integers take 40 MB.
- A machine times faster multiplies the feasible by for but adds only for : algorithms, not machines, move exponential walls.
- Constants decide between close complexities ( and are equal at ), and hidden parameters (sums over tests, value ranges, alphabets, second parameters, offline queries, output size) change the budget.
- With yes/no answers, questions distinguish at most candidates, so a limit near queries means binary search.
- Fine-grained conditional lower bounds (SETH, 3SUM, APSP) tell you when to stop looking for a better exponent and start looking for structure in the instance.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Ignoring constant factors, what is the largest with ?
- about
- about
Show the answer
B. about
Iterating from gives , , : the exact answer is .
Your solution handles within the limit. On a machine 1,000 times faster, about how large an can it handle?
- 30,000
- about 40
- about 950
- 60
Show the answer
B. about 40
gives . A thousandfold speedup buys ten more items; a better algorithm, such as meet in the middle, would double .
A judge hides one of 1,000 possible values and answers any yes/no question you ask. What is the fewest number of questions that always suffices?
- 9
- 10
- 32
- 500
Show the answer
B. 10
questions have at most answer patterns, so forces , and halving the candidates each time achieves .
Your edit-distance DP takes time and . What does the Backurs–Indyk theorem tell you?
- Edit distance is NP-hard, so no polynomial-time algorithm exists
- An algorithm for all inputs would refute SETH, so look for structure in the constraints instead
- An algorithm exists by the FFT
- No algorithm can beat steps, unconditionally
Show the answer
B. An algorithm for all inputs would refute SETH, so look for structure in the constraints instead
The bound is conditional: strongly subquadratic edit distance would solve CNF-SAT with variables and clauses in time for some . So look for what this instance allows, such as a small bound on the distance (a band of width gives ), or settle for an approximation.
Further reading
- A. Laaksonen, Guide to Competitive Programming, 2nd ed. (Springer, 2020).
- R. Impagliazzo & R. Paturi, "On the complexity of k-SAT," Journal of Computer and System Sciences 62(2) (2001).
- R. Williams, "A new algorithm for optimal 2-constraint satisfaction and its implications," Theoretical Computer Science 348 (2005).
- A. Backurs & P. Indyk, "Edit Distance Cannot Be Computed in Strongly Subquadratic Time (unless SETH is false)," Proceedings of STOC (2015).
- V. Vassilevska Williams, "On some fine-grained questions in algorithms and complexity," Proceedings of the International Congress of Mathematicians (ICM 2018).