You are on page 1of 11

LECTURE 03

Factorial ● When to Use? i.e f calling itself


● Smaller sub-problem(s) has same structure as the parent

Recursion ● Definition: A function that calls itself

Recursive process:
Ingredients for recursion:
1. Base Case
- Does involve a recursive call! Has trivial answer
2. Recursive Step
- Connects solution to its subproblem!

Fibonacci in
Math vs in
Python

Recursion
Tree
Mutual
Recursion

ping(6)

while While <expression> : <body>


Loop
*need to
constantly
update the
value of the
counter variable
with the
statement
Variable += 1

Expression: Predicate (condition) to stay within the loop


Body: Statement(s) that will be evaluated if predicate is True

for for <var> in <sequence> : <body>


Loop 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
*do not need to
uo constantly
update the
value of the
counter variable

range range([start,] stop[, step])


function - creates a sequence of integers
- from start (inclusive) to stop (non-inclusive) • incremented by step
break & ● Can only be written in loops
continue

Recursion vs ● Recursive process occurs when there are deferred operations\


Iteration - I.e operations rely on previous results and has more than one function call
● Iterative process does not have deferred operations
- Only has one function call

Order of Definition: Rough measure of RESOURCES used by a computational process


Growth RESOURCES:
● Time Complexity - how long it takes to run a program (ALL operations require time)
● Space Complexity - how much memory do we need to run the program (variables
require space to store data)

- Examples:
Some
common
Functions

Finding Order ● Take the largest term & drop the constants
of Growth for
expressions

● Basically: Count the number of ‘basic computational steps”


Steps to Do & 1. Identify the basic computation step
Example
2. Try a few small values of n

3. Extrapolate for really large n

4. Look for “worst case” scenario:


a) Work out the steps in the computation
b) Generalize to n
● Orders of Growth in Space
- 2 main sources:
1. Function Calls – Stack – Look for pending operations & recursive function calls
2. a Structures – Heap – To be discussed laterA
● Moral of the Story
- Different ways of performing a computation (algorithms) can consume dramatically
different amounts of resources

Greatest ● QN: The GCD of two numbers 𝑎 and 𝑏, is the largest positive integer that divides both 𝑎
Common and without remainder
Divisor ● Naive Solution
Example 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
5. Order of Growth: O(min(a, b))
● Euclid’s Algorithm
- Given two numbers a and b, where 𝑎 = 𝑏𝑄 + 𝑟 (the remainder of the division). Then we
have,

Towers of ● Moving the 3 discs from one pole to another


Hanoi
Example

● Rules:
- Can only put one disc at a time 2.
- Cannot put larger disc over a smaller disc
● Claim: We can move 3 discs from A to B. Why?
● How to move 4 discs?
- Move 3 discs from A to B
- Move 1 disc from A to C
● Recursion: To move n discs from A to C (viaB):
- Move n-1 discs from A to B (via C)
- Move the disc from A to C
- Move n-1 discs from B to C (via A)

Exponentiation
Example

From another angle:

Fast
Exponentiation

Time a) Iterative Factorial


Complexity
All primitives
& Operations
require time:
Count by
operation

b) Recursive Factorial
Space a) Iterative Factorial
Complexity
Count by
variables and
functions
computed

*irrespective of what n has been passed it always needed 3 different variables


b) Recursive Factoial

Let S(n) denote the space needed for factorial w.r.t the input n. S(n) = 3 * const(variables)S
*number of variables are dependent on n

CONCLUDE

* ALL constants depend on CPU

Summary ● Recursion
- Solve the problem for a simple (base) case - Express (divide) a problem into one or more
smaller similar problems
● Iteration
- while and for loops
● Order of Growth
- Time and space requirements for computations w.r.t the input
- Different ways of performing a computation (algorithms) can consume dramatically
different amounts of resources.

You might also like