Recursion - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

Recursion

Description:

Recursion. Credits: Jeff Edmonds, Ping Xuan. Thinking about Algorithms Abstractly. MULT(X,Y) ... .com/hanoi/index.htm. http://www.cut-the-knot.com/recurrence ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 41
Provided by: depts168
Category:
Tags: recursion

less

Transcript and Presenter's Notes

Title: Recursion


1
Recursion
Thinking about Algorithms Abstractly
  • Credits Jeff Edmonds, Ping Xuan

2
Code 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
3
Tree 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
4
Friends
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.
5
Friends
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.
6
Friends
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

7
Friends
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?
8
Friends
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
9
Friends
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
10
Friends
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
11
Friends
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
12
Friends
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
13
Friends
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.
14
Friends
  • Recursive Algorithm
  • Assume you have an algorithm that works.
  • Use it to write an algorithm that works.

15
Friends
  • 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.
16
Friends 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
17
Friends 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?
18
Friends Strong Induction
  • Recursive Algorithm
  • Assume you have an algorithm that works.
  • Use it to write an algorithm that works.

19
Friends
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.
20
Towers of Hanoi
21
Towers of Hanoi
22
Towers 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.
23
Towers of Hanoi
(Homework 1. Assume n 2k)
24
Tower 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

25
Sorting 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.

26
Recursive 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.

27
Merge Sort
28
Merge Sort
Split Set into Two
29
Merge Sort
Merge two sorted lists into one
30
(No Transcript)
31
Merge 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)
32
Recursive Images
if n1
if n0, draw
n0
else draw
And recursively Draw here with n-1
33
Recursive Images
if n2
if n0, draw
n1
else draw
And recursively Draw here with n-1
34
Recursive Images
if n3
if n0, draw
n2
else draw
And recursively Draw here with n-1
35
Recursive Images
if n30
if n0, draw
else draw
And recursively Draw here with n-1
36
Recursive Images
if n0
37
Recursive Images
if n0
38
Recursive Images
Þ
(4/3)n
L(n) 4/3 L(n-1)
39
Homework 3 Derive the recurrence relation for
the time complexity T( N ) and solve it. Assume
that N 2k .
40
Please 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.
Write a Comment
User Comments (0)
About PowerShow.com