Recurrent problems: TOH, Pizza Cutting and Josephus Problems
1. Recurrent Problems
In recurrence, solution to a problem depends on the solution to smaller instance of the
same problem.
Tower of Hanoi: We are given a tower of eight disks, initially stacked in decreasing
size on one of three pegs. The objective is to transfer the entire tower to one of the other
pegs, moving only one disk at a time and never moving a large one onto a smaller.
Fig: Tower of Hanoi with 8 disks
Consider small cases:
0 0T , [0 disk requires 0 move]
2 3T
3 7T , [Transferring small 2 disks to middle peg requires 3 moves. Then moving the
largest disk from left peg to right peg requires only 1 move. Finally, moving 2 smaller
disks from middle peg to right peg requires another 3 moves. Thus, total 3+1+3=7 moves
require for moving 3 disks from one peg to another]
Similarly, for n disks to transfer from one peg to another we require:
0 0T
12 1n nT T , for n > 0
We can find out the closed form of any recurrence to get quick result from the problem.
12 1n nT T
= 22(2 1) 1nT
= 2
22 2 1nT
= 2
32 (2 1) 2 1nT
= 3 2
32 2 2 1nT
= ( 1) ( 2) 2
2 2 2 2 2 1n n n
n nT
= ( 1) ( 2) 2 1 0
02 2 2 2 2 2n n n
T
T1 1, [1 disk requires only 1 move]
Tn Tn1 1Tn1 2Tn1 1
Thus, the recurrence for Tower of Hanoi stands
Page 1 of 2
[Top disk from L to M; Bottom Disk from L to R; Finally, Top disk from M to R (see the Note below!)]
(These Two are
Constraints)
2. 0 1 2 ( 2) ( 1)
2 2 2 2 2n n
nT
=
2 1
2 1
n
[
1
0 1 2
1
n
n a
a a a a
a
]
= 2 1n
2 1n
nT is called the closed form solution of the “Tower of Hanoi” problem.
Mathematical induction is a general way to prove any closed form solution. It has three
parts.
Basis – prove the formula for smallest possible value.
Hypothesis – Consider that, the formula is true for first n values.
Induction – Try to prove the formula for (n+1)-th value.
For example, we are going prove the closed from of Tower of Hanoi solution.
Basis: 0
0 2 1 1 1 0T
Hypothesis: Let, 2 1n
nT
Induction: 1 1
1 2 1 2(2 1) 1 2 2 1 2 1n n n
n nT T
(Proved)
Try to prove
1
0 1 2
1
n
n a
a a a a
a
using Mathematical induction method.
☺Good Luck☺
Page 2 of 2
*** Also, Solve DOUBLE TOH and TRIPLE TOH ***
(OK, because We know T0 is 0)
(or, first n-1 values)
(or, n-th value, if hypothsis true for n-1 values)
Excluded
-1
3. Recurrent Problems
Lines in the Plane: We have to find out how many slices of pizza can a person obtain
by making n straight cuts with a knife. Academically, what is the maximum number of
regions, nL defined by n lines in the plane? We can start by looking at small cases.
0 1L 1 2L 2 4L 3 4 3 7L
If there exists 1n lines in the plane, then n-th line have to cut previous 1n lines to
produce maximum number of new region in the plane. The 1n intersection points
create n new region in the plane. Thus, the recurrence for line in the plane therefore
0 1L
1n nL L n , for 0n
We can find out the closed form of the recurrence through unfolding it to the end.
1n nL L n
= 2 ( 1)nL n n
= 3 ( 2) ( 1)nL n n n
= ( 1) ( 2) ( 1)n nL n n n n n
= 0 1 2 3 ( 2) ( 1)L n n n
= 1nS , where 1 2 3 ( 2) ( 1)nS n n n
nS is called the triangular number because it is number of bowling pins in an n-row
triangular array. For example the usual four-row array has 4 10S pins.
n 01 02 03 04 05 06 07 08 09 10
nS 01 03 06 10 15 21 28 36 45 55
We can evaluate nS using the following trick:
nS = 1 + 2 + 3 + +( 2)n +( 1)n + n
+ nS = n +( 1)n +( 2)n + + 3 + 2 + 1
2 nS =( 1)n +( 1)n + ( 1)n + +( 1)n +( 1)n +( 1)n
( 1)
2
n
n n
S
, for 0n
Now, we have our closed form for lines in the plane problem.
( 1)
1
2
n
n n
L
, for 0n
1
2
1
1
2
3
4
1a
2
3a
4a
4b
3b1b
But, if the newly added line goes thru any of the previous intersection
points, then there will be less than "n new regions". For example:
1a
1b
2
4
3a
3b
(Also, if the new line is parallel to any previous line
then, we will get less regions. Check it yourself!!!)
6 in stead of
7 regions
L2 = L1 + 2L1 = Lo + 1 L3 = L2 + 3
4. Suppose instead of straight lines we use bent lines, each containing one “zig”. We have to
find out the maximum number of regions, nZ created by n such bent lines in the plane.
Fig: Bent lines in the plane
From small cases, we realize that a bent line is like two straight lines except that region
merge when the “two” lines don’t extent past their intersection point. Region 2, 3, 4
which would be distinct with two lines, become single region when there is a bent line,
we lose two region. Thus
2 2n nZ L n
=
2 (2 1)
1 2
2
n n
n
= 2
2 1 2n n n
= 2
2 1n n , for 0n
Comparing closed forms of straight and bent lines in the plane, we find that for large n,
2
2
1
2
2
n
n
L n
Z n
So, we get about four times as many regions with bent lines as with straight lines.
☺Good Luck☺
1
2
3
4
Zn = Maximum no. of regions obtained from n intersecting Zig shapes
in the plane
Also Find,
ZZn = Maximum no. of regions obtained from n intersecting ZigZag shapes
Wn = Maximum no. of regions obtained from n intersecting W shapes
5. Recurrent Problems
Josephus Problem: We start with n people numbered 1 to n around a circle and we
eliminate every second remaining person until only one survives. For example, here is the
starting configuration for n = 10.
The elimination order is 2 4 6 8 10 3 7 1 9 , so 5th
person survives.
The problem is to determine the survivor’s number, J(n). In this example J(10) = 5.
Consider, we have 2n people, then after 1st
round of elimination, we are left with n
people and 3rd
person will be the next person waiting for elimination. This is just like
starting with n people, except that every person’s number has been doubled and
decreased by 1. Similarly, if we start with 2 1n people, then after 1st
round of
elimination number 1 will be eliminated. Then, we almost have the same situation with n
people like previous one, but this time their numbers are doubled and increased by 1.
Fig: Start with 2n people Fig: Start with 2 1n people
(2 ) 2 ( ) 1J n J n , for 1n (2 1) 2 ( ) 1J n J n , for 1n
Combining these equations with (1) 1J gives us a recurrence that defines J in all cases:
(1) 1J
(2 ) 2 ( ) 1J n J n , for 1n (1)
(2 1) 2 ( ) 1J n J n , for 1n
Our recurrence makes it possible to build a table of small values which can helps us to
guess the closed form of Josephus problem.
( )J n is always 1 at the beginning of a group and it increases by 2 within a group. So, if
we write n in the form 2m
n l , where 2m
is the largest power of 2 not exceeding n
where 2m
l n satisfies 1
0 2 2 2m m m
l
, the solution to our recurrence would be
(2 ) 2 1m
J l l , for 0m and 0 2m
l
1
2
3
4
5
6
7
8
9
10
1
3
5
7
2 3n
2 1n
3
5
7
9
2 1n
2 1n
n 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16
J(n) 01 01 03 01 03 05 07 01 03 05 07 09 11 13 15 01
Find J(100) using thess recurrences:
J(100) = 2 J(50) -1
= 4 J(25) -2 -1 = 8J(12) + 4 - 2 -1
=16 J(6) -8 +4 -2 -1
= 32 J(3) - 16 -8 + 4 -2 -1
= 64 J(1) + 32 -16 -8 +4 -2 -1 = 64 + 32 -16-8+4-2-1 = 73 (ANS)
J(n) =
Write the
Recurrence
for Josephus
Problem
Establish the
logic for
these
Recurrences
6. To illustrate the solution let’s compute J(100).
6
(100) (2 36) 2 36 1 73J J
Try to prove, (2 ) 1m
J
Every solution to a problem can be generalized so that it applies to a wider class of
problems. Power of 2 played an important role in our recurrence solution. Thus it’s
natural to look at the radix 2 representation of n and J(n).
1 2 2 1 0 2( )m m mn b b b b bb i.e. 1 2 2
1 2 2 1 02 2 2 2 2m m m
m m mn b b b b b b
where, each bit ib is either 0 or 1 and the leading bit mb is 1. Recalling that 2m
n l ,
we have, successively,
1 2 2 1 0 2(1 )m mn b b b bb
1 2 2 1 0 2(0 )m ml b b b b b
1 2 2 1 0 22 ( 0)m ml b b b bb
1 2 2 1 0 22 1 ( 1)m ml b b b b b
1 2 2 1 0 2( ) ( )m m mJ n b b b bb b
We have proved that, 1 2 2 1 0 2 1 2 2 1 0 2( ) (( ) ) ( )m m m m m mJ n J b b b b bb b b b bb b
For example, 2 2(100) ((1100100) ) (1001001) 64 8 1 73J J
We can find out special cases solution of Josephus problem if required. For example, we
going to find when ( )
2
n
J n is true.
( )
2
n
J n
2
2 1
2
m
l
l
1
(2 2)
3
m
l
For all m, if
1
(2 2)
3
m
l is an integer, then solution exists. We can verify this equation
m ( ) 2 1 / 2J n l n
1 0 002 01
3 2 010 05
5 10 042 21
7 42 170 85
Now, we are going to find out the closed form of more general recurrence of Josephus
problem introducing constants , and .
(1)f
(2 ) 2 ( )f n f n , for 1n (2)
(2 1) 2 ( )f n f n , for 1n
n 2^m ll=1/3*
(2^m-2)
Binary Property of the Josephus Problem:
l = 1/5 (2^m-3)
Q: Find the minimum three values of n for which J(n) = n/3
Q: Find threee values of n for which J(n) = n
Question: Find J(100)by
using the Binary
Property of the
Josephus Problem
l must be integer
see the file : "Josephus Add On.docx" for such problems
Q: Derive the Binary Property of the Josephus problem
This is called
General
Recurrence of
the Josephus
Problem
LHS = J(2^m) = 2 J(2^m-1) - 1 = 4 J(2^m-2) - 2 -1 = 8 J(2^m-3) - 4 - 2 -1 = ... ... ...
= 2^m J(1) - 2^m-1 - 2^m-2 - ... -1 = 2^m - (2^m - 1) = 1
7. We can construct the following general table for small values of n.
n f(n)
1
2 2 +
3 2 +
4 4 +3
5 4 +2 +
6 4 + +2
7 4 +3
8 8 +7
9 8 +6 +
Thus, we can express f(n) as following form
( ) ( ) ( ) ( )f n A n B n C n (3)
where A(n), B(n) and C(n) are coefficients of , and respectively.
Considering the special case 1, 0 , we get ( ) ( )f n A n and recurrence
becomes
(1) 1A
(2 ) 2 ( )A n A n , for 1n
(2 1) 2 ( )A n A n , for 1n
Solving the above recurrence we get ( ) (2 ) 2m m
A n A l
Plugging the constant function ( ) 1f n into equation (2), we get
1
1 2 1 1
1 2 1 1
Putting the values of , and in equation (3), we get
1 ( ) ( ) ( )A n B n C n (4)
Similarly, we can plug in ( )f n n into equation (2).
1
2 2 0
2 1 2 1
n n
n n
Putting the values of , and in equation (3), we get
( ) ( )
( ) ( )
n A n C n
C n n A n
2 2m m
l
l
Q: Write the General Recurrence of Josephus Problem.
Solve the recurrence (Or, find the closed form
expression of the recurrence) by Using
Repertoire Method. (start from prev page (the
recurrences) + this Page Full + next page upto
the point where you get all of A(n), B(n), C(n) )
Better: See Scanned Lecture (Pages 21-24)
Better explained in Scanned Lecture
8. Putting the values of A(n) and C(n) into equation (4), we can find B(n).
( ) ( ) ( ) 1B n A n C n
(1)f
(2 ) 2 ( ) jf n j f n , for j = 0, 1 and 1n
where 0 and 1 , and this recurrence unfolds, binary-wise
01 2 2 1 0 2 1 2 2 1 2(( ) ) 2 (( ) )m m m m m m bf b b b b b b f b b b b b
1 0
1 0
2 1 0
2 1 0
1 2 3 2 2
2
1 2 3 2 2
2
1 2 3 2
3 2
1 2 3 2
2(2 (( ) ) )
2 (( ) ) 2
2 (2 (( ) ) ) 2
2 (( ) ) 2 2
m m m b b
m m m b b
m m m b b b
m m m b b b
f b b b b b
f b b b b b
f b b b b
f b b b b
1 2 2 1 0
1 2 2 1 0
1 2 2 1 0
1 2 2
2
1 2 2
2
2 (( ) ) 2 2 2 2
2 2 2 2 2
( )
m m
m m
m m
m m m
m b b b b b
m m m
b b b b b
b b b b b
f b
To verify our new general formula, we are going to check the solution with previously
known value of Josephus problem. For example, when n = 100 = 2(1100100) , our
original Josephus value 01, 1 and 1 1 yield
n = (1 1 0 0 1 0 20) = 100
0 0 1 0 0 2)
= ( 1 1 1 1 1 1 21)
= +64 +32 16 8 +4 2 1 = 73
We can generalize even more. The recurrence
( ) jf j , for 1 j d
( ) ( ) jf dn j cf n , for 0 j d and 1n
is the same as the previous one except that we start with numbers in radix d and produce
values in radix c i.e. it has the radix-changing solution.
1 2 2 1 01 2 2 1 0(( ) ) ( )m m mm m m d b b b b b b cf b b b b bb
2m
l 1
The above method of solving recurrence problems is called repertoire method. In this
method first we find settings of general parameters for which we know the solution, this
gives us a repertoire of special cases that we can solve. Then we obtain the general case
by combining the special cases.
We can write the generalized recurrence of equation (2) as
n = ((1 1 0 0 1 0 0)2 )
= ( 1
Question: Find J(100)
by using the
Radix Based Property
of the
Josephus Problem
f(n)
*Q: Write The Radix Based Property
Of the Generalized Recurrence.
Use This Property to Find J(100) for
Josephus Recurrence.
(must write this: alpha, beta0, beta1)
Write the Recurrence for the
Generalized Josephus Problem
in original
9. For example, suppose we have the given recurrence
(1) 34
(2) 5
f
f
(3 ) 10 ( ) 76f n f n , for 2n
(3 1) 10 ( ) 2f n f n , for 2n
(3 2) 10 ( ) 8f n f n , for 2n
and suppose we want to compute f(19).
Here, we have d = 3 and c = 10.
3(19) ((201) )f f
2 0 1 10( )
= (5 76 2 1 0
102) 5 10 76 10 2 10 500 760 2 1258
which is our answer.
Though Easy, dont do like below (but you can
always verify your answer using this):
f(19) = f(3*6+1) =10f(6) - 2 = 10f(3*2) - 2
=10* [10f(2) + 76] - 2
=10* [10*5 + 76] - 2
= 100*5 +760 -2 ==> 1258
Question: Solve following recurrence using *Radix Based Properties* of the Generalized Josephus Problem
for n=3 (i.e., 6 persons, first 3 Good, next 3 Bad) => m= LCM(4,5,6) = 60
for n=4 (i.e., 8 persons, first 4 Good, next 4 Bad) => m= LCM(5,6,7,8) = 840
m = LCM of (3n, 3n-1, ... , n+1)
Read this Section from the Scanned Class Lecture
Pages 25 - 29 (more explanation and examples)