CS1010S-Lec-03+Recursion%2C+Iteration+%26+Order+of+Growth
CS1010S-Lec-03+Recursion%2C+Iteration+%26+Order+of+Growth
Lecture 3
Recursion, Iteration &
Order of Growth
25 Jan 2023
Expected Level Progression
Difficulty Level
Don’t pay for
notes/exam
papers!
NUS Course Material
Ethical Behaviour and Respecting Copyright
All course participants (including permitted guest students) who have access to the course
materials on LumiNUS or any approved platforms by NUS for delivery of NUS modules are not
allowed to re-distribute the contents in any forms to third parties without the explicit consent
from the module instructors or authorized NUS officials
Examples of what is disallowed
• No posting on any websites (except otherwise explicitly allowed)
• No selling of material
• No sharing of questions/answers which could lead to cheating/plagiarism
Done with all the
missions?
Got a lot of time to burn?
Optional Trainings
Contests
Due 5 Feb 2023
Winning: 400 EXP
Participation: 50 EXP
Recap
Sub- Sub-
problem 1 problem 2
Key Insight:
Sub-sub Sub-sub
problem 3 problem 4
Smaller problems
are easier to solve
f calling
itself!
Rewrite:
𝑛 × 𝑛−1 !, 𝑛 >1
𝑛! = ቊ
1, 𝑛=1
Factorial
𝑛 ×𝑓 𝑛−1 , 𝑛>1
𝑓(𝑛) = ቊ
1, 𝑛=1
def factorial(n):
if n == 1:
return 1
else:
return n * factorial(n – 1)
Recursion
def factorial(n):
if n == 1: terminating condition
return 1 (base case)
else:
return n * factorial(n – 1)
recursive call
Function that calls itself is called a recursive function
Recursive Process
factorial(5)
5 * factorial(4)
5 * (4 * factorial(3))
5 * (4 * (3 * factorial(2))
5 * (4 * (3 * (2 * factorial(1)))
5 * (4 * (3 * (2 * 1)))
5 * (4 * (3 * 2))
5 * (4 * 6)
5 * 24
120
n computed with
2 computed with
Terminating
1
condition
(trivial answer!)
Ingredients for recursion
1. Base Case
Does involve a recursive call!
Has trivial answer
2. Recursive Step
Connects solution at to its subproblem!
Fibonacci Numbers
Leonardo Pisano Fibonacci (12th century) is credited for
the sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, …
fib(4) fib(3)
1 1 0 1 0
fib(1) fib(0)
1 0
leaves
Mutual Recursion
def ping(n): ping(6)
if (n == 0):
return n Ping! pong(5)
else: Pong! ping(4)
print("Ping!") Ping! pong(3)
pong(n - 1) Pong! ping(2)
Ping! pong(1)
def pong(n): Pong! ping(0)
if (n == 0):
return n
else:
print("Pong!")
ping(n - 1)
Factorial: Another way! res = 1
counter = 1
res = 1
res = 1 * 2
res = 1 * 2 * 3
res = 1 * 2 * 3 * 4
F
res = 1 * 2 * 3 * 4 * 5 counter <= n
…
T
def factorial(n):
res, counter = 1, 1
res = res * counter
while counter <= n:
res = res * counter
counter = counter + 1
return res counter = counter + 1
while loop while <expression>:
<body>
expression
Predicate (condition) to stay within the loop
body
Statement(s) that will be evaluated if predicate is True
Yet another way! non-inclusive.
Up to n.
def factorial(n):
res = 1
for counter in range(2, n+1):
res = res * counter
return res
def factorial(n):
res, counter = 1, 1
while counter <= n:
res = res * counter
counter = counter + 1
return res
for loop for <var> in <sequence>:
<body>
sequence
a sequence of values
var
variable that take each value in the sequence
body
statement(s) that will be evaluated for each value in the sequence
range function Examples
1
for j in range(10): 3 Continue with the
if j % 2 == 0: 5 next value!
continue 7
print(j) 9
print("done") done
Recursion
vs
Iteration
Recursive process occurs when there
are deferred operations.
Assignment a
𝑇 𝑛 = 2𝑎 + 𝑛 ∗ 𝑐 + 2𝑎 + 2𝑏
Arithmetic b
= 𝐴′ + 𝑛𝐵′
Relational c
def factorial(n): Recursive Factorial
if n == 1:
return 1
else: factorial(5)
return n * fact(n – 1) 5 * factorial(4)
5 * (4 * factorial(3))
5 * (4 * (3 * factorial(2))
5 * (4 * (3 * (2 * factorial(1)))
Operations Time needed (in ms) 5 * (4 * (3 * (2 * 1)))
Assignment a
5 * (4 * (3 * 2))
5 * (4 * 6)
Arithmetic b
5 * 24
Relational c 120
𝑇 𝑛 =𝑐+𝑏 +𝑇 𝑛−1 def factorial(n):
if n == 1: c units
= 𝑐 ′ + 𝑇(𝑛 − 1) return 1
else:
= 𝑐′ + 𝑐′ + 𝑇 𝑛 − 2
return n * fact(n – 1)
= 2𝑐 ′ + 𝑇 𝑛 − 2
= 2𝑐 ′ + 𝑐 ′ + 𝑇(𝑛 − 3) b units
= 3𝑐 ′ + 𝑇(𝑛 − 3)
…
= 𝑛 − 1 𝑐′ + 𝑇 𝑛 − 𝑛 − 1
= 𝑛 − 1 𝑐 ′ + 𝑇(1)
= 𝑛𝑐 ′ − 𝑐 ′ = 𝐴′ + 𝑛𝐵′
Space Complexity
def factorial(n):
res, counter = 1, 1
while counter <= n:
Iterative Factorial
res = res * counter
counter = counter + 1
return res res counter n
factorial(4)
res counter
1 1 Let S(n) denote the space
1 2
needed for factorial w.r.t.
the input n.
2 3
6 4
S(n) = 3 * const
24 5
def factorial(n):
if n == 1:
return 1
Recursive Factorial
else:
return n * fact(n – 1)
1
factorial(4)
1 the space
Let S(n) denote
needed for factorial w.r.t.
factorial(4) the input n.2
= 4 * factorial(3)
= 4 * 3 * factorial(2) 2
= 4 * 3 * 2 * factorial(1) S(n) = n * const
= 4 * 3 * 2 * (1) 3
= 4 * 3 * (2)
= 4 * (6) 6
= 24 24
4
But we are interested in –
- How time requirement grows with input!
- How space requirement grows with input!
Big-O Notation
𝑅 𝑛 has order of growth
O(𝑓 𝑛 ) written
𝑅 𝑛 = O (𝑓 𝑛 )
If there are positive constants 𝑘
and 𝑛0 such that
𝑅 𝑛 ≤𝑘𝑓 𝑛 ∀𝑛 ≥ 𝑛0 𝑛0 𝑛
Suppose a computation 𝐶 takes 3𝑛 + 5 steps
to complete, what is the order of growth?
O(3𝑛 + 5) = O(𝑛)
Since, 3𝑛 + 5 ≤ 100𝑛, ∀𝑛 ≥ 1
Let’s assume that:
𝑛 is doubled (i.e. increased to 2𝑛)
Order of growth
= O 3𝑛 + 4𝑛2 + 4
= O(3𝑛)
Tips
• Identify dominant terms, ignore smaller terms
• Ignore additive or multiplicative constants
- 4𝑛2 – 1000𝑛 + 300000 = O(𝑛2)
𝑛
- + 200𝑛 log 𝑛 = O(𝑛 log 𝑛)
7
log𝑐 𝑏
Note. log 𝑎 𝑏 =
log𝑐 𝑎
Base is not important!
More tricks in CS1231,
CS3230
Some involve sophisticated proofs
For now…
Count the number of “basic computational
steps”.
- Identify the basic computation steps
- Try a few small values of n
- Extrapolate for really large n
- Look for “worst case” scenario
Moral of the story
Different ways of performing a computation
(algorithms) can consume dramatically
different amounts of resources.
More Examples
Greatest Common Divisor
Naïve Solution.
1. Set GCD to 1.
2. Start with 1.
3. Check for every number if it divides and b. If it
does, set it as GCD.
4. Continue until you reach a or b.
Greatest Common Divisor
Euclid’s Algorithm.
GCD(206,40) = GCD(40,6)
= GCD(6,4)
= GCD(4,2)
= GCD(2,0)
= 2
Another example
Towers of Hanoi
Rules.
1. Can only put one disc at a time
2. Cannot put larger disc over a smaller disc
Towers of Hanoi
Rules.
1. Can only put one disc at a time
2. Cannot put larger disc over a smaller disc
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
Towers of Hanoi
A B C
• Time requirement?
• Space requirement? Notice any
similarity?
Exponentiation – another angle!
power(a, 13)
1 𝑒=0
𝑒
𝑒
𝑏 =൞ (𝑏 2 )2 𝑒 is even = a * power(a, 12)
𝑏 ⋅ 𝑏 𝑒−1 𝑒 is odd = a * power(b, 6)
= a * power(c, 3)
= a * c * power(c, 2)
= a * c * power(d, 1)
= a * c * d * power(d, 0)
Fast Exponentiation
def fast_expt(b, e):
if e == 0:
return 1
elif e % 2 == 0:
return fast_expt(b*b, e//2)
else:
return b * fast_expt(b, e-1)
• Iteration:
- while and for loops
Summary
• Order of growth:
- Time and space requirements for
computations w.r.t the input