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 of a cost function such as the time complexity . 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 and an encoding that maps each instance of a problem to a string . The size of is the length of this string. With , the size is a number of bits. The set of instances of size is then finite, because there are at most strings of length . So the maximum over 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 symbols fits in bits, so the length in bits is at most times the length in symbols. Conversely, a symbol can carry bits, so the length in symbols is at most the length in bits divided by , 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 has bits in binary, and symbols in unary. If is the number of bits, then . So the unary size is exponential in the binary size. For , 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 in base is , and .
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 .
- A graph with vertex set and edge set : the two numbers and . An adjacency list has entries (or for an undirected graph), and an adjacency matrix has entries. The bit length of an adjacency list is larger by a factor of about , because each vertex name in it needs bits. An adjacency matrix has exactly 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, , and not its value .
- A set of numbers whose values are not bounded by a machine word: the total number of bits, or the number of items together with the bit length 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 , trial division divides by up to . It makes at most divisions. In terms of the value, this is , which looks small. In terms of the size , it is up to divisions, which is exponential. For the prime , the size is bits, and the test makes divisions (the divisors to ). 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 rows and columns, where is the target and is the number of items. Its time is . The input has about bits or more, and can be as large as for bits. So the time is polynomial in and in the value , and exponential in the size. Such a bound is called pseudo-polynomial. For and the table has more than cells, although the input has fewer than bits (for 100 items of 64 bits each plus the target).
- Counting loops over a value. A routine that computes the th Fibonacci number with a loop of additions makes a number of additions that is linear in the value . Its input has only bits, so it makes a number of additions exponential in the size of the input. Since has bits, the loop also costs bit operations in the logarithmic cost model.
- Items that are not of constant size. Sorting strings of length with a comparison sort makes comparisons, but one comparison of two equal strings reads characters. The input has characters, and the cost of merge sort on strings can be character comparisons. The measure " items" hides .
- 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 vertices can have between and edges. The size must grow with the amount of data that the algorithm may read.
Related definitions
- Time complexity and space complexity are functions of the input size.
- Worst-case analysis and best-case analysis take the maximum or minimum over all inputs of one size.
- Pseudo-polynomial time is a bound that is polynomial in the values of the numbers of the input and in the size, and so it is exponential in the bit length in general.
- Unary encoding writes the integer as symbols. A problem that is hard with binary numbers can be easy with unary numbers, and strongly hard problems stay hard in unary.
- Multivariate asymptotic notation handles inputs with several size parameters, such as and .
- The word RAM and the logarithmic cost model set the cost of an operation on numbers, and so they decide when "number of items" is a fair measure.
References
- 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)
- Michael Sipser, Introduction to the Theory of Computation, third edition, Chapter 7 (Time Complexity, encodings of the input)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag AE8FW3 Edit this page History Report an error Markdown JSON