Proving runtime of algorithms
How to prove the Big-O notation, i.e. the runtime of algorithms
There are 3 major methods and 1 advanced method:
Method 1: Substitution
In this method, we will utilize the definition of Big-O notation and mathematical induction.
By definition, Big-O notation defines a lower bound for a function:
\[f(n) = O(n) \Longleftrightarrow f(n) \le c*n, with \ c \ge 0\ and\ n \ge n_{0}\]i.e. $f(n) = O(n)$ if there exist $c$ and $n_{0}$ such that $c*n$ is always larger than $f(n)$.
Notice that i am using $O(n)$ as an example, you can use the above equation to prove $O(n^{2}), O(n*lgn)…$ as well.
We will use mathematical induction to prove that there exists a c and $n_{0}$ that satisfies the above equation.
In another word, we want to show that the left hand side $f(n)$ is less than or equal the right hand side $c*n$.
Example:
Proving merge sort runtime is $O(n \log n)$
To prove that Merge Sort has a runtime of $O(n \log n)$ using the substitution method, we’ll follow these steps:
Understand the Recurrence Relation:
\[T(n) = 2T\left(\frac{n}{2}\right) + O(n)\]
The time complexity of Merge Sort can be described by the recurrence relation:This means that to sort an array of size (n), we recursively sort two subarrays each of size (\frac{n}{2}), and then merge them, which takes linear time.
Assume a Form for the Solution:
\[T(n) \leq k \cdot n \log n\]
We aim to show that $T(n)$ is bounded above by $O(n \log n)$. Let’s assume:where (k) is some constant.
- Apply the Substitution Method:
Substitute our assumed upper bound into the recurrence relation to see if it holds. Expand the Recurrence Relation:
\[T(n) = 2T\left(\frac{n}{2}\right) + O(n) \leq 2\left(k \cdot \frac{n}{2} \log \frac{n}{2}\right) + O(n)\]
Using the assumption, substitute $(T\left(\frac{n}{2}\right))$:\Simplify $(2 \cdot \frac{n}{2})$:
\[T(n) \leq k \cdot n \log \frac{n}{2} + O(n)\]Simplify the Logarithmic Term:
\[T(n) \leq k \cdot n (\log n - \log 2) + O(n)\]
Use the logarithmic identity $(\log \frac{n}{2} = \log n - \log 2)$:Expand the terms:
\[T(n) \leq k \cdot n \log n - k \cdot n \log 2 + O(n)\]Compare with the Assumed Bound:
\[k \cdot n \log n \geq k \cdot n \log n - k \cdot n \log 2 + O(n)\]
Our goal is to show that:Subtract (k \cdot n \log n) from both sides:
\[0 \geq -k \cdot n \log 2 + O(n)\]Rearranging terms:
\[k \cdot n \log 2 \geq O(n)\]Determine the Constant (k):
\[k \geq \frac{c}{\log 2}\]
To satisfy the inequality, choose (k) such that:where (c) is the constant from the linear term (O(n)).
Conclusion:
\[T(n) = O(n \log n)\]
By choosing an appropriate constant (k), the substitution method confirms that Merge Sort’s runtime satisfies:
Comments powered by Disqus.