In
computer science
Computer science is the study of computation, automation, and information. Computer science spans theoretical disciplines (such as algorithms, theory of computation, information theory, and automation) to Applied science, practical discipli ...
(specifically
computational complexity theory
In theoretical computer science and mathematics, computational complexity theory focuses on classifying computational problems according to their resource usage, and relating these classes to each other. A computational problem is a task solved by ...
), the worst-case complexity measures the
resources (e.g. running time,
memory
Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembered, ...
) that an
algorithm
In mathematics and computer science, an algorithm () is a finite sequence of rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algorithms are used as specificat ...
requires given an input of arbitrary size (commonly denoted as in
asymptotic notation). It gives an upper bound on the resources required by the algorithm.
In the case of running time, the worst-case
time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
indicates the longest running time performed by an algorithm given ''any'' input of size , and thus guarantees that the algorithm will finish in the indicated period of time. The order of growth (e.g. linear,
logarithmic Logarithmic can refer to:
* Logarithm, a transcendental function in mathematics
* Logarithmic scale, the use of the logarithmic function to describe measurements
* Logarithmic spiral,
* Logarithmic growth
* Logarithmic distribution, a discrete pr ...
) of the worst-case complexity is commonly used to compare the
efficiency
Efficiency is the often measurable ability to avoid wasting materials, energy, efforts, money, and time in doing something or in producing a desired result. In a more general sense, it is the ability to do things well, successfully, and without ...
of two algorithms.
The worst-case complexity of an algorithm should be contrasted with its
average-case complexity
In computational complexity theory, the average-case complexity of an algorithm is the amount of some computational resource (typically time) used by the algorithm, averaged over all possible inputs. It is frequently contrasted with worst-case comp ...
, which is an average measure of the amount of resources the algorithm uses on a random input.
Definition
Given a
model of computation and an algorithm
that halts on each input
, the mapping
is called the time complexity of
if, for every
input string ,
halts after exactly
steps.
Since we usually are interested in the dependence of the
time complexity
In computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations performed by ...
on different input lengths, abusing terminology, the time complexity is sometimes referred to the mapping
, defined by the maximal complexity
:
of inputs
with length or size
.
Similar definitions can be given for space complexity, randomness complexity, etc.
Ways of speaking
Very frequently, the complexity
of an algorithm
is given in asymptotic
Big-O Notation
Big ''O'' notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity. Big O is a member of a family of notations invented by Paul Bachmann, Edmund Land ...
, which gives its growth rate in the form
with a certain real valued comparison function
and the meaning:
* There exists a positive real number
and a natural number
such that
:
Quite frequently, the wording is:
* „Algorithm
has the worst-case complexity
.“
or even only:
* „Algorithm
has complexity
.“
Examples
Consider performing
insertion sort on
numbers on a
random access machine
In computer science, random-access machine (RAM) is an abstract machine in the general class of register machines. The RAM is very similar to the counter machine but with the added capability of 'indirect addressing' of its registers. Like the cou ...
. The best-case for the algorithm is when the numbers are already sorted, which takes
steps to perform the task. However, the input in the worst-case for the algorithm is when the numbers are reverse sorted and it takes
steps to sort them; therefore the worst-case time-complexity of insertion sort is of
.
See also
*
Analysis of algorithms
In computer science, the analysis of algorithms is the process of finding the computational complexity of algorithms—the amount of time, storage, or other resources needed to execute them. Usually, this involves determining a function that re ...
References
*
Thomas H. Cormen
Thomas H. Cormen is the co-author of ''Introduction to Algorithms'', along with Charles Leiserson, Ron Rivest, and Cliff Stein. In 2013, he published a new book titled '' Algorithms Unlocked''. He is a professor of computer science at Dartmout ...
,
Charles E. Leiserson,
Ronald L. Rivest, and
Clifford Stein.
Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. . Chapter 2.2: Analyzing algorithms, pp.25-27.
* Oded Goldreich. Computational Complexity: A Conceptual Perspective. Cambridge University Press, 2008. {{ISBN, 0-521-88473-X, p.32.
Analysis of algorithms