Algorithm Search⌘K

Definitionintro

Input size

Also called problem size, size of the input.

The measure n of an instance, such as the number of items or bits, against which running time is expressed.

The size of an input is the number that an analysis uses as the argument nn of a cost function such as the time complexity T(n)T(n). Formally it is the length of the encoding of the instance in bits. In practice it is a simpler measure that is polynomially related to that length, such as the number of items in an array or the number of vertices and edges of a graph. When each item fits in a machine word, it is within a constant factor of the length in words.

Definition

Fix a finite alphabet Σ\Sigma and an encoding ee that maps each instance xx of a problem to a string e(x)∈Σ∗e(x) \in \Sigma^*. The size of xx is the length ∣e(x)∣|e(x)| of this string. With Σ={0,1}\Sigma = \{0,1\}, the size is a number of bits. The set InI_n of instances of size nn is then finite, because there are at most 2n2^n strings of length nn. So the maximum over InI_n in a worst-case analysis always exists.

A change of the alphabet changes the size by a constant factor only. A symbol of an alphabet with k≥2k \ge 2 symbols fits in ⌈lg⁡k⌉\lceil \lg k \rceil bits, so the length in bits is at most ⌈lg⁡k⌉\lceil \lg k \rceil times the length in symbols. Conversely, a symbol can carry ⌊lg⁡k⌋≥1\lfloor \lg k \rfloor \ge 1 bits, so the length in symbols is at most the length in bits divided by ⌊lg⁡k⌋\lfloor \lg k \rfloor, rounded up. The usual rule is to accept any encoding in which the lengths are polynomially related to the binary length and in which one can convert between encodings in polynomial time. Under this rule, the class of problems solvable in polynomial time does not depend on the choice. This rule does not allow the unary encoding of numbers, as the next section shows.

Integers: bits, not value

A positive integer NN has ⌊lg⁡N⌋+1\lfloor \lg N \rfloor + 1 bits in binary, and NN symbols in unary. If bb is the number of bits, then 2b−1≤N<2b2^{b-1} \le N < 2^b. So the unary size is exponential in the binary size. For N=1000N = 1000, the binary size is 10 and the unary size is 1000. The base of a positional system matters only by a constant factor: the number of digits of NN in base kk is ⌊log⁡kN⌋+1\lfloor \log_k N \rfloor + 1, and log⁡kN=lg⁡N/lg⁡k\log_k N = \lg N / \lg k.

Conventions in practice

The following measures are in common use. Each is justified when the items fit in a constant number of machine words, as in the word RAM.

  • An array, list or set of items: the number of items nn.
  • A graph with vertex set VV and edge set EE: the two numbers n=∣V∣n = |V| and m=∣E∣m = |E|. An adjacency list has n+mn + m entries (or n+2mn + 2m for an undirected graph), and an adjacency matrix has n2n^2 entries. The bit length of an adjacency list is larger by a factor of about lg⁡n\lg n, because each vertex name in it needs ⌈lg⁡n⌉\lceil \lg n \rceil bits. An adjacency matrix has exactly n2n^2 bits. See multivariate asymptotic notation.
  • A string: its length. A matrix: its dimensions, or the number of entries.
  • A single integer, as in primality testing or factoring: its number of bits, ⌊lg⁡N⌋+1\lfloor \lg N \rfloor + 1, and not its value NN.
  • A set of numbers whose values are not bounded by a machine word: the total number of bits, or the number of items nn together with the bit length bb of the largest. In the logarithmic cost model each operation costs in proportion to the bits of its operands.

Examples and non-examples

  • Trial division. To factor NN, trial division divides NN by 2,3,…2, 3, \ldots up to ⌊N⌋\lfloor \sqrt{N} \rfloor. It makes at most ⌊N⌋−1\lfloor \sqrt{N} \rfloor - 1 divisions. In terms of the value, this is O(N)O(\sqrt{N}), which looks small. In terms of the size b=⌊lg⁡N⌋+1b = \lfloor \lg N \rfloor + 1, it is up to 2b/22^{b/2} divisions, which is exponential. For the prime N=261−1N = 2^{61} - 1, the size is b=61b = 61 bits, and the test makes 15185002481518500248 divisions (the divisors 22 to 15185002491518500249). If the number were written in unary, the same algorithm would take a time polynomial in the size.
  • Pseudo-polynomial time. The dynamic program for subset sum fills a table with n+1n + 1 rows and W+1W + 1 columns, where WW is the target and nn is the number of items. Its time is O(nW)O(nW). The input has about n⌈lg⁡(W+1)⌉n \lceil \lg(W+1) \rceil bits or more, and WW can be as large as 2b−12^{b} - 1 for bb bits. So the time is polynomial in nn and in the value WW, and exponential in the size. Such a bound is called pseudo-polynomial. For n=100n = 100 and W=264W = 2^{64} the table has more than 1.8×10211.8 \times 10^{21} cells, although the input has fewer than 70007000 bits (for 100 items of 64 bits each plus the target).
  • Counting loops over a value. A routine that computes the nnth Fibonacci number with a loop of n−1n - 1 additions makes a number of additions that is linear in the value nn. Its input nn has only ⌊lg⁡n⌋+1\lfloor \lg n \rfloor + 1 bits, so it makes a number of additions exponential in the size of the input. Since FnF_n has Θ(n)\Theta(n) bits, the loop also costs Θ(n2)\Theta(n^2) bit operations in the logarithmic cost model.
  • Items that are not of constant size. Sorting nn strings of length LL with a comparison sort makes Θ(nlog⁡n)\Theta(n \log n) comparisons, but one comparison of two equal strings reads LL characters. The input has nLnL characters, and the cost of merge sort on strings can be Θ(Lnlog⁡n)\Theta(L n \log n) character comparisons. The measure "nn items" hides LL.
  • Not a size. The value of a number input is a magnitude, not a size. The number of vertices alone is not the size of a graph input, since a graph on nn vertices can have between 00 and n(n−1)/2n(n-1)/2 edges. The size must grow with the amount of data that the algorithm may read.

References

  1. Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Section 2.2 (input size) and Chapter 31 (Number-Theoretic Algorithms, size of integer inputs)
  2. Michael Sipser, Introduction to the Theory of Computation, third edition, Chapter 7 (Time Complexity, encodings of the input)
  3. Kleinberg and Tardos, Algorithm Design, Section 6.4 (Subset Sums and Knapsacks, pseudo-polynomial running time)

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/AE8FW3/text.yaml