Title: Recursion
1Recursion
Thinking about Algorithms Abstractly
- Credits Jeff Edmonds, Ping Xuan
2Code Representation of an Algorithm
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
3Tree of Frames
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
4Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
One Friend for each stack frame. Each worries
only about his job.
5Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
Worry about one step at a time. Imagine that you
are one specific friend.
6Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider your input instance
7Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider your input instance
- Allocate work
- Construct one or more subinstances
X 3 Y 2 XY?
X 3 Y 1 XY?
X 6 Y 3 XY?
8Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider your input instance
- Allocate work
- Construct one or more subinstances
- Assume by magic your friends give you the answer
for these.
X 3 Y 2 XY6
X 3 Y 1 XY3
X 6 Y 3 XY18
9Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider your input instance
- Allocate work
- Construct one or more subinstances
- Assume by magic your friends give you the answer
for these. - Use this help to solve your own instance.
X 3 Y 2 XY6
X 3 Y 1 XY3
X 6 Y 3 XY18
10Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider your input instance
- Allocate work
- Construct one or more subinstances
- Assume by magic your friends give you the answer
for these. - Use this help to solve your own instance.
- Do not worry about anything else.
- Who your boss is.
- How your friends solve their instance.
X 3 Y 2 XY6
X 3 Y 1 XY3
X 6 Y 3 XY18
11Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- This technique is often
- referred to as
- Divide and Conquer
X 3 Y 2 XY6
X 3 Y 1 XY3
X 6 Y 3 XY18
12Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Consider generic instances.
MULT(X,Y) Break X into ab and Y into cd e
MULT(a,c) and f MULT(b,d) RETURN e 10n
(MULT(ab, cd) e - f) 10n/2 f
MULT(X,Y) If X Y 1 then RETURN XY
- Remember!
- Do not worry about
- Who your boss is.
- How your friends solve their instance.
bd
(ab)(cd)
ac
13Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
This solves the problem for every possible
instance.
14Friends
- Recursive Algorithm
- Assume you have an algorithm that works.
- Use it to write an algorithm that works.
15Friends
- Recursive Algorithm
- Assume you have an algorithm that works.
- Use it to write an algorithm that works.
If I could get in, I could get the key. Then I
could unlock the door so that I can get in.
16Friends Strong Induction
- Recursive Algorithm
- Assume you have an algorithm that works.
- Use it to write an algorithm that works.
To get into my house I must get the key from a
smaller house
17Friends Strong Induction
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
- Allocate work
- Construct one or more subinstances
Each subinstance must be a smaller instance to
the same problem.
X 3 Y 2 XY?
X 3 Y 1 XY?
X 6 Y 3 XY?
18Friends Strong Induction
- Recursive Algorithm
- Assume you have an algorithm that works.
- Use it to write an algorithm that works.
19Friends
MULT(X,Y) If X Y 1 then RETURN XY
Break X into ab and Y into cd e MULT(a,c)
and f MULT(b,d) RETURN e 10n (MULT(ab,
cd) e - f) 10n/2 f
MULT(X,Y) If X Y 1 then RETURN XY
Use brute force to solve the base case instances.
20Towers of Hanoi
21Towers of Hanoi
22Towers of Hanoi
Get a job at the Towers of Hanoi company at
level n1,2,3,4,..
Or get transferred in as the president you
decide what your vice president does.
23Towers of Hanoi
(Homework 1. Assume n 2k)
24Tower of Hanoi
- http//www.mazeworks.com/hanoi/index.htm
- http//www.cut-the-knot.com/recurrence/hanoi.shtml
- http//www.cs.yorku.ca/andy/courses/3101/lecture-
notes/LN0.html
25Sorting Problem Specification
- Precondition The input is a list of n values
with the same value possibly repeated. - Post condition The output is a list consisting
of the same n values in non-decreasing order.
26Recursive Sorts
- Given list of objects to be sorted
- Split the list into two sublists.
- Recursively have a friend sort the two sublists.
- Combine the two sorted sublists into one entirely
sorted list.
27Merge Sort
28Merge Sort
Split Set into Two
29Merge Sort
Merge two sorted lists into one
30(No Transcript)
31Merge Sort
merge_sort ( array a ) if (length of a gt
1) then merge_sort ( left half of a )
merge_sort ( right half of a) merge the
above two sorted half arrays
Time T(n)
Q(n log(n))
2T(n/2) n
(Homework 2. Assume n 2k)
32Recursive Images
if n1
if n0, draw
n0
else draw
And recursively Draw here with n-1
33Recursive Images
if n2
if n0, draw
n1
else draw
And recursively Draw here with n-1
34Recursive Images
if n3
if n0, draw
n2
else draw
And recursively Draw here with n-1
35Recursive Images
if n30
if n0, draw
else draw
And recursively Draw here with n-1
36Recursive Images
if n0
37Recursive Images
if n0
38Recursive Images
Þ
(4/3)n
L(n) 4/3 L(n-1)
39Homework 3 Derive the recurrence relation for
the time complexity T( N ) and solve it. Assume
that N 2k .
40Please feel free to ask questions!
Please give me feedbackso that I can better
serve you.
Thanks for the feedback that you have given me
already.