2.7 KiB
2.7 KiB
date | type |
---|---|
02.09.2024 | theoretical |
\mathcal{O}
Complexity
- Time Complexity: amount of time an algorithm takes to complete as a function of the size of its input.
- Space Complexity: amount of memory it uses as a function of the size of its input.
Big O
-
Formal Definition:
Big O notation,
\mathcal{O}(f(n))
, describes the upper bound of the time or space complexity of an algorithm in terms of the input sizen
. It characterizes the worst-case scenario of an algorithm's growth rate.Formally, a function
T(n)
is said to be\mathcal{O}(f(n))
if there exist positive constantsc
andn_0
such that for alln \geq n_0
:0 \leq T(n) \leq c \cdot f(n).
This means that
T(n)
will grow at most as fast asf(n)
, up to a constant multiplec
, for sufficiently largen
.
Informally:
Big-O notation provides an upper limit on:
- Time: How long an algorithm will take.
- Space: How much memory it will require. It helps compare algorithms by focusing on their growth rate and ignoring constant factors.
Other Notations for Growth Rates
-
Big-O (
\mathcal{O}(f(n))
):- Describes the upper bound (worst-case) of
T(n)
. - Informally:
T(n)
grows no faster thanf(n)
. - Formal Definition:
T(n) \text{ is } \mathcal{O}(f(n)) \iff \exists c > 0, n_0 > 0 \text{ such that } T(n) \leq c \cdot f(n) \text{ for all } n \geq n_0.
- Describes the upper bound (worst-case) of
-
Big-Omega (
\Omega(f(n))
):- Describes the lower bound (best-case) of
T(n)
. - Informally:
T(n)
grows at least as fast asf(n)
. - Formal Definition:
T(n) \text{ is } \Omega(f(n)) \iff \exists c > 0, n_0 > 0 \text{ such that } T(n) \geq c \cdot f(n) \text{ for all } n \geq n_0.
- Describes the lower bound (best-case) of
-
Big-Theta (
\Theta(f(n))
):- Describes the exact bound (tight bound) of
T(n)
. - Informally:
T(n)
grows exactly as fast asf(n)
. - Formal Definition:
T(n) \text{ is } \Theta(f(n)) \iff T(n) \text{ is } \mathcal{O}(f(n)) \text{ and } T(n) \text{ is } \Omega(f(n)).
- Describes the exact bound (tight bound) of
-
Little-O (
o(f(n))
):- Describes a loose upper bound of
T(n)
. - Informally:
T(n)
grows strictly slower thanf(n)
. - Formal Definition:
T(n) \text{ is } o(f(n)) \iff \forall c > 0, \exists n_0 > 0 \text{ such that } T(n) < c \cdot f(n) \text{ for all } n \geq n_0.
- Describes a loose upper bound of
Think of growth rates as vehicles on a highway:
\mathcal{O}(f(n))
:T(n)
cannot go faster thanf(n)
.\Omega(f(n))
:T(n)
cannot go slower thanf(n)
.\Theta(f(n))
:T(n)
moves at the exact same speed asf(n)
.o(f(n))
:T(n)
moves slower thanf(n)
and never catches up.