Fundamentals of Computational Complexity
1Introduction
This lecture explains how to evaluate growth in running time and memory use as the input size increases. Rather than relying only on elapsed seconds measured on a particular computer, asymptotic growth permits analysis of an algorithm as inputs become large.
2Input Size and Computational Resources
The {input size} is a nonnegative integer that measures the scale of a problem instance. An appropriate measure depends on the problem: a one-dimensional array may use its number of elements, whereas a two-dimensional array may require both its numbers of rows and columns. Not every input is therefore adequately described by a single parameter.
{Time complexity} is a function describing how the number of executed basic operations grows with input size. {Space complexity} is a function describing growth in memory required during execution. When the analysis excludes storage occupied by the input itself and counts only additional storage, the measure is called {auxiliary-space complexity}.
3Variation among Inputs
Inputs of the same size can require different amounts of a resource.
- {Best-case complexity} is the minimum resource use among inputs of size .
- {Worst-case complexity} is the maximum resource use among inputs of size .
- {Average-case complexity} is the expected resource use under a specified probability distribution on inputs of size .
Average-case complexity is undefined until the probability distribution is specified. Moreover, best, average, and worst cases classify inputs, whereas , , and describe asymptotic relations between functions. These are separate concepts.
4Asymptotic Notation
Let and be nonnegative for all sufficiently large .
- means that positive constants exist such that for every . It states an asymptotic upper bound.
- means that positive constants exist such that for every . It states an asymptotic lower bound.
- means that both and hold. It states equality of asymptotic growth rates up to constant factors.
Thus, denotes an upper bound and does not necessarily assert a tight growth rate. For example, is true, but is more precise.
5Examples
Let . For ,
so . Constant terms and constant factors do not alter the asymptotic growth rate.
For linear search on an array of length , a match at the first element gives a best-case running time of , while a match only at the final element or no match gives a worst-case running time of . By contrast, binary search on a sorted array halves the candidate interval at each comparison. After comparisons, at most candidates remain, yielding a worst-case running time of , including the empty-array case.
An algorithm that only reads its input and uses a fixed number of variables has auxiliary-space complexity . Allocating another array of length requires auxiliary space. Time and space must be analyzed separately.
6Connections to Subsequent Lectures
The next lecture derives the time and auxiliary-space complexity of recursion from the number of recursive calls and the maximum call depth. The subsequent sorting and search lectures compare applicability conditions, correctness, and complexity together.
data/lecture/information/algorithm/foundation/recursion-basics.lecture.n.md7Summary
- Input size must be defined by a measure appropriate to the problem.
- Time and space complexity measure different computational resources.
- Best, average, and worst cases classify inputs; , , and describe asymptotic relations between functions.
- gives an upper bound, gives a lower bound, and gives a matching upper and lower bound.