Calculating complexity
Iterative Programs
Example-1 (Maximum from a list)
Consider the following function
max_list.py | |
---|---|
- Input size is the length of
L
. - Always take
n
, i.e., length ofL
steps to complete. - Worst case Complexity is \(O(n)\).
Example-2 (Check whether the loop has duplicates)
duplicates_in_list.py | |
---|---|
- Input size is len of
L
, i.e.,n
- Worst case is that no duplicates exist and both the loops execute completely.
- Time is \((n-1)+(n-2)+(n-3)+\dots+1=\cfrac{n(n-1)}{2}\)
- Worst case complexity is \(O(n^2)\)
Example-3 (Matrix Multiplication)
matrix_multiplication.py | |
---|---|
- Input size: Input matrices has the size \(m \text{ x } n,n\text{ x }p\).
- Output matrix is \(m\text{ x }p\).
- Overall time is \(O(mnp)\), i.e., \(O(n^3)\), if \(m = p = n\).
Example-4 (Count number of bits in binary representation of a number n
)
- This problem takes \(\log(n)\) steps to reach \(1\) from \(n\).
Tip
For number theoretic problems, input size is the number of digits in the number.
Recursive Programs
Example-1 (Towers of Hanoi)
Tower of Hanoi is a mathematical puzzle where we have three rods (A, B, and C) and N disks. Initially, all the disks are stacked in decreasing value of diameter i.e., the smallest disk is placed on the top and they are on rod A. The objective of the puzzle is to move the entire stack to another rod (here considered C), obeying the following simple rules:
- Only one disk can be moved at a time.
- Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
- No disk may be placed on top of a smaller disk.
Read more about Towers of Hanoi.
To solve this problem we use a recursive approach. You can learn more about the approach from the Proffessor's video.
Approach
- Let the initial peg be
A
, final peg beB
and transit peg beC
. - Move \(n-1\) discs from from peg
A
toC
. - Then move the last disc from
A
toB
. - Now
C
has \(n-1\) discs andB
has 1 disc. - Now move \(n-2\) discs from
C
toA
. And the last disc fromC
toB
and so on.
\[
\text{Now let } M(n) - \text{ Number of moves to move }n\text{ discs and }M(1)=1.
\\\ \\ \text{So, using the recursive approach, }\\\ \\M(n) = M(n-1)+M(1)+M(n-1) = 2M(n-1)+1
\]
Similarly if you unwind the \(M(n-1)\) and so on, then \(M(n) = 2^{n-1}+(2^{n-1}-1) = 2^{n-1}\)