40
Recursion Credits: Jeff Edmonds, Ping Xuan

Recursion Credits: Jeff Edmonds, Ping Xuan. MULT(X,Y): If |X| = |Y| = 1 then RETURN XY Break X into a;b and Y into c;d e = MULT(a,c) and f =MULT(b,d)

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Recursion

Credits: Jeff Edmonds, Ping Xuan

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2

+ f

Code Representation of an Algorithm

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Tree of Frames

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

FriendsOne Friend for each

stack frame.

Each worries only about his job.

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

FriendsWorry about one

step at a time.Imagine that you are one specific

friend.

X = 33Y = 12ac = bd = (a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends•Consider your input instance

X = 33Y = 12ac = ?bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends•Consider your input instance•Allocate work

•Construct one or more subinstances

X = 3Y = 1XY=?

X = 3Y = 2XY=?

X = 6Y = 3XY=?

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends•Consider your input instance•Allocate work

•Construct one or more subinstances•Assume by magic your friends give you the answer for these.

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends•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 = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends•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 = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends

This technique is often referred to as

Divide and Conquer

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

FriendsConsider generic instances.

ac bd (a+b)(c+d)

MULT(X,Y):

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – 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.

X = 2133Y = 2312ac = 483bd = 396(a+b)(c+d) = 1890XY = 4931496

X = 21Y = 23ac = 4bd = 3(a+b)(c+d) = 15XY = 483

X = 33Y = 12ac = 3bd = 6(a+b)(c+d) = 18XY = 396

X = 54Y = 35ac = 15bd = 20(a+b)(c+d) = 72XY = 1890

X = 2Y = 2XY=4

X = 1Y = 3XY=3

X = 3Y = 5XY=15

X = 3Y = 1XY=3

X = 3Y = 2XY=6

X = 6Y = 3XY=18

X = 5Y = 3XY=15

X = 4Y = 5XY=20

X = 9Y = 8XY=72

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

FriendsThis solves the

problem for every possible instance.

FriendsRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

FriendsRecursive 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.

Circular Argument!

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

To get into my houseI must get the key from a smaller house

X = 33Y = 12ac = ?bd = ?(a+b)(c+d) = XY =

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends & Strong Induction

•Allocate work •Construct one or more subinstances

X = 3Y = 1XY=?

X = 3Y = 2XY=?

X = 6Y = 3XY=?

Each subinstance must be a smaller instance

to the same problem.

Friends & Strong InductionRecursive Algorithm:

•Assume you have an algorithm that works.•Use it to write an algorithm that works.

Use brute force to get into the smallest house.

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Break X into a;b and Y into c;d

e = MULT(a,c) and f =MULT(b,d)

RETURN

e 10n + (MULT(a+b, c+d) – e - f) 10n/2 + f

Friends

MULT(X,Y):

If |X| = |Y| = 1 then RETURN XY

Use brute force to solve the base case instances.

Towers of Hanoi

Towers of Hanoi

Towers of Hanoi

Get a job at the Towers of Hanoi company at level n=1,2,3,4,…..

Or get transferred in as the president& you decide what your vice president does.

Towers of Hanoi

Time: T(1)=1, T(n) = 2T(n-1) + 1 = (2n)(Homework 1. Assume n = 2k)

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

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.

88 149825

62

52

79

3023

31 14,23,25,30,31,52,62,79,88,98

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.

Merge Sort

88 149825

62

52

79

3023

31

Divide and Conquer

Merge Sort

88 149825

62

52

79

3023

31Split Set into Two

(no real work)

25,31,52,88,98

Get one friend to sort the first half.

14,23,30,62,79

Get one friend to sort the second half.

Merge Sort

Merge two sorted lists into one

25,31,52,88,98

14,23,30,62,79

14,23,25,30,31,52,62,79,88,98

Merge Sort

Time: T(n) = = (n log(n))

2T(n/2) + n(Homework 2. Assume n = 2k)

merge_sort ( array a[ ] ){ if (length of a > 1)

then { merge_sort ( left half of a ); merge_sort ( right half of a); merge the above two sorted half arrays;}

}

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=1

n=0

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=2

n=1

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=3

n=2

Recursive Images

if n=0, draw

else draw

And recursivelyDraw here with n-1

if n=30

Recursive Images

if n=1if n=2if n=3if n=4if n=5

if n=0

Recursive Images

if n=1

if n=0

if n=2if n=3if n=4if n=5

Recursive Images

L(n) = 4/3 L(n-1) = (4/3)n

Homework 3: Derive the recurrence relation for the time complexity T( N ) and solve it. Assume that N = 2k .

Please feel free to ask questions!

Please give me feedback

so that I can better serve you.

Thanks for the feedback that you

have given me already.