Download as pdf or txt
Download as pdf or txt
You are on page 1of 59

Recursion

Tahir Iqbal
Bahria University Lahore Campus
Covering Chessboard with Triminos
Covering Chessboard with Triminos
Covering 2xN Grid with Dominos
• In how many ways we can cover a 2𝑥𝑁 grid with dominos.
• Grid has 2 rows and 𝑁 columns.
• We want to cover it with 1𝑥2 domino-tiles.
Covering 2xN Grid with Dominos
• In how many ways we can cover a 2𝑥𝑁 grid with dominos.
Covering 2xN Grid with Dominos
• In how many ways we can cover a 2𝑥𝑁 grid with dominos.
Example
Stone-picking Game
Example
Game of Nim
Rules:
There are 3 piles of stone.
Each player, on his turn, may pick any number of
stones from any one pile.
Player, who picks up the last stone in the game, wins
the game.

Who has the winning strategy, player-1 or player-2?


Example
Game of Chomp
Rules:
A player may choose any cookie, and then
he will have to all cookies on the right and
below the chosen cookie.

Player who eats the poisonous cookie will


lose the game.

Who has the winning strategy, player-1 or


player-2?
Handshaking Problem
• In a party, guests are to made sit in two rows according to following rules:
• A person is made to sit in Row-1 if he shakes hand with the last person in Row-1.
• A person is made to sit in Row-2 if he DOES NOT shake hand with the last person in
Row-2.
• The host may request the last person(s) to switch to other row, without violating the
above 2 rules.
• Question: When a new person comes, can he be made sit in Row-1 or Row-
2 following the above rules?
Handshaking Problem
• Question: When a new person comes, can he be made sit in Row-1 or Row-2
following the above rules?
• Solution: (using induction)
Base Case: How we are going to form the rows in the beginning
Handshaking Problem
• Question: When a new person comes, can he be made sit in Row-1 or Row-2
following the above rules?
• Solution: (using induction)
Base Case: How we are going to form the rows in the beginning
Hypothesis: Suppose we have number of people sitting in Row-1 and Row-2
following the rules defined in the beginning.
Mr X is the last person sitting in Row-1 and Mr Y is the last person sitting in Row-2.
Handshaking Problem
• Question: When a new person comes, can he be made sit in Row-1 or Row-2 following the
above rules?
• Solution: (using induction)
Base Case: How we are going to form the rows in the beginning
Hypothesis: Suppose we have number of people sitting in Row-1 and Row-2 following the
rules defined in the beginning.
Mr X is the last person sitting in Row-1 and Mr Y is the last person sitting in Row-2.
Inductive Step: A new-comer, Mr Z comes. We need to show that he can be made sit in either
Row-1 or Row-2, without violating the rules.

Discuss all possible cases …


Induction: 5 Pirates and Division of Gold Coins (Assignment Question)
Backward induction problem

Rules of the game:


• The pirates are ordered in seniority: Senior most is the leader
• At least 50% votes are needed for coin-splitting proposal
• If proposal is not accepted, pirates kill the senior pirate and start over again.
• The most senior (surviving) pirate proposes his own division plan, and they
vote by the same rules and either divide the loot or kill the senior pirate.
• The process continues until one plan is accepted‫۔‬
• NOTE: The pirates are all extremely logical and greedy, and all want to live
and have maximum number of coins.
Induction vs. Recursion
• In inductive stem, we have 𝑃 𝑘 → 𝑃 𝑘 + 1 , an implication.
We prove this implication.
Induction vs. Recursion
• In inductive stem, we have 𝑃 𝑘 → 𝑃 𝑘 + 1 , an implication.
We prove this implication.
• What if it is not an implication/proposition, bit it is a computational step.
Compute at 𝑘 → Compute at 𝑘 + 1

It can use k − 1 , k − 2 , ⋯ or any of less than k values to compute at k + 1

In programing, we use smaller values to compute larger values.


Power Function
• How to compute 2n ?
Power Function
• How to compute 2n ?
2𝑛 = 2 ∗ 2𝑛−1
Write code for this !!!
Recursion – Function calls – traversing a tree
• We make function calls (going down
the tree), till we reach the base value.
We do not need to go below the base
case.

• Combine the answers of base case,


and get one step up (function is
returning value)
Example –
• A frog needs to cross a pond, in which there are
stones at regular gaps.
Frog may jump from a stone to next stone, or it may
make a bigger jump by skipping one stone, or even
bigger, skipping two stones, but not more.

In how many ways 𝑋𝑘 , it may reach till 𝑘 −th stone?


Example –
• In how many ways 𝑋𝑘 , it may reach till 𝑘 −th stone?

• Number of ways to reach the 1st stone: 𝑋1 = 1 (we just place it on 1st stone)
• Number of ways to reach the 2nd stone: 𝑋2 = 1 (a single hop)
• Number of ways to reach the 3rd stone: 𝑋3 = 2 (2 single hops, or a double hop)
• Number of ways to reach the 4th stone: 𝑋4 = 4 (How??)

• Number of ways to reach the kth stone: 𝑋𝑘 = ?


Example –
• In how many ways 𝑋𝑘 , it may reach till 𝑘 −th stone?

• Number of ways to reach the 1st stone: 𝑋1 = 1 (we just place it on 1st stone)
• Number of ways to reach the 2nd stone: 𝑋2 = 1 (a single hop)
• Number of ways to reach the 3rd stone: 𝑋3 = 2 (2 single hops, or a double hop)
• Number of ways to reach the 4th stone: 𝑋4 = 4 (How??)

• Number of ways to reach the kth stone: 𝑋𝑘 = 𝑋𝑘−1 + 𝑋𝑘−2 + 𝑋𝑘−3


Power Function – revisited
• How to compute 𝑎𝑛 ?
• 𝑎𝑛 = 𝑎 ∗ 𝑎𝑛−1

Write Code !
Power Function – revisited
• How to compute 𝑎𝑛 ?
• 𝑎𝑛 = 𝑎 ∗ 𝑎𝑛−1
Write Code !

• Any other way of computing 𝑎𝑛 ?


Power Function – revisited
• How to compute 𝑎𝑛 ?
• 𝑎𝑛 = 𝑎 ∗ 𝑎𝑛−1
𝑛 𝑛
• 𝑎 = 𝑎 ∗𝑎
𝑛 2 2

How to Code ?

Be warned: what happens when the power 𝒏 is odd?


Power Function
int power (int a, int n)
else
{
{
if (n == 0) if (n % 2 == 0)
return 1; return power(a, n/2) * power(a, n/2);
if (n == 1) else

return a; return power(a, n/2) * power(a, n/2) * a;


}
}
Power Function
– another way –
int power (int a, int n)
else
{
{
if (n == 0) int ans = power(a, n/2);
return 1; if (n % 2 == 0)
return ans * ans;
if (n == 1)
else
return a; return ans * ans * a;
}
}
Power Function
int power (int a, int n) int power (int a, int n)
{
How many recursive
{
if (n == 0) calls are made?
if (n == 0)
return 1;
return 1;
if (n == 1)
return a; if (n == 1)

else return a;
{ else
int ans = power(a, n/2); {
if (n % 2 == 0) if (n % 2 == 0)
return ans * ans;
return power(a, n/2) * power(a, n/2);
else
else
return ans * ans * a;
return power(a, n/2) * power(a, n/2)
}
* a;
} }
}
Power Function
int power (int a, int n) int power (int a, int n)
{ How many recursive {

calls are made?


if (n == 0)
if (n == 0)
return 1;
return 1;
if (n == 1)
return a; if (n == 1)

else return a;
{
int ans = power(a, n/2);
𝑂 𝑁 vs 𝑂 log 𝑁 else
{
if (n % 2 == 0) if (n % 2 == 0)
return ans * ans;
return power(a, n/2) * power(a, n/2);
else
return ans * ans * a; (Make trees of both) else
return power(a, n/2) * power(a, n/2)
}
* a;
} }
}
Power Function
Fibonacci Sequence
• 𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2
Write code …
Fibonacci Sequence
• 𝐹𝑛 = 𝐹𝑛−1 + 𝐹𝑛−2
Write code …

In your code, use Memorization to


save computations?
Factorial
Compute 𝑁! (factorial of 𝑁).

Recursive relation?

Write code?
Factorial
int factorial (int n)
{
if ( (n == 0) || (n == 1) )
return 1;
else
{
return n * factorial(n-1);
}
}
Combinations (Later with Counting)
Combinations (Later with Counting)
int choose (int n, int k)
{
if ( (k == 0) || (n == 0 ) )
return 1;
else
return choose (n-1, k) + choose(n-1, k-1) ;
}
Permutations (Later with Counting)
• Example: {1,2,3} --- permutations?
Recursion as “Navigating through a Maze”
• We can look at all recursive programs as variations on the process of
navigating through a maze.
How does a computer solve a maze like the
following?
The first step is to convert the graphic into a
numerical representation.
• The following maybe be done manually or through a program.
This information can be stored in a 2D array
0 1 2 3 4 5 ...
0 0 0 0 0 0 0
1 0 0 0 0 0 1
2 0 0 0 0 0 0
3 0 0 0 0 1 0
4 0 0 0 1 0 1
5 0 1 0 0 1 0

A section of the matrix storing the maze


Recursive Algorithm to Navigate through the
Maze
Important Realizations
• Entering a chamber in the maze represents a function call.
• When we hit a dead-end, how will we know where we came from?
• For example, if you are in chamber 12, how will you know if you came from 2, 1, 6, or 13? And then, if
you came from 13, how would you know where you came to 13 from, and so on?
• Answer: We must have a thread tied to first chamber where we began.
• We will unroll the thread as we enter a new chamber. (function call)
• We will roll it back as we return to the older chamber. (return)
• In an iterative program we will have to explicitly make the logic of the thread.
• In a recursive program, the thread is always automatically there: the stack.
We can draw the navigation process as a picture
-- and it is always a tree!
We can draw the navigation process as a picture
-- and it is always a tree!
How much time does the recursive program for
maze navigation take?

Looking at the maze or the stacks in not really useful in analyzing a program for time and space!
From the tree of the maze problem, you can compute the
total time for the recursive program

Each function call represents a chamber in


the maze.
Each function call takes constant time “c” in
this case.
=> T(n) = cn, linear time in the number of
chambers in the maze

Just sum up the times of all functions calls on the tree.


Tower of Hanoi
In the temple of Banaras …
Tower of Hanoi
Tower of Hanoi
void hanoi( int n, char source, char destination, char intermediate)
{
if (n == 1)
// move disk from source to destination
else
hanoi( n-1, source, intermediate, destination);
hanoi( n-1, intermediate, destination, source);
}
8 Queens Placements
Covering 2xN Grid with Dominos
• In how many ways we can cover a 2𝑥𝑁 grid with dominos.
Covering 2xN Grid with Dominos
• In how many ways we can cover a 2𝑥𝑁 grid with dominos.

You might also like