1 / 40

Recursion

Thinking about Algorithms Abstractly. Recursion. Credits: Jeff Edmonds, Ping Xuan. Code Representation of an Algorithm. 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

demi
Download Presentation

Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Code Representation of an Algorithm 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

  3. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 4931496 X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 54 Y = 35 ac = 15 bd = 20 (a+b)(c+d) = 72 XY = 1890 X = 2 Y = 2 XY=4 X = 1 Y = 3 XY=3 X = 3 Y = 5 XY=15 X = 3 Y = 1 XY=3 X = 3 Y = 2 XY=6 X = 6 Y = 3 XY=18 X = 5 Y = 3 XY=15 X = 4 Y = 5 XY=20 X = 9 Y = 8 XY=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

  4. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 4931496 X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 54 Y = 35 ac = 15 bd = 20 (a+b)(c+d) = 72 XY = 1890 X = 2 Y = 2 XY=4 X = 1 Y = 3 XY=3 X = 3 Y = 5 XY=15 X = 3 Y = 1 XY=3 X = 3 Y = 2 XY=6 X = 6 Y = 3 XY=18 X = 5 Y = 3 XY=15 X = 4 Y = 5 XY=20 X = 9 Y = 8 XY=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 Friends One Friend for each stack frame. Each worries only about his job.

  5. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 4931496 X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 54 Y = 35 ac = 15 bd = 20 (a+b)(c+d) = 72 XY = 1890 X = 2 Y = 2 XY=4 X = 1 Y = 3 XY=3 X = 3 Y = 5 XY=15 X = 3 Y = 1 XY=3 X = 3 Y = 2 XY=6 X = 6 Y = 3 XY=18 X = 5 Y = 3 XY=15 X = 4 Y = 5 XY=20 X = 9 Y = 8 XY=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 Friends Worry about one step at a time. Imagine that you are one specific friend.

  6. X = 33 Y = 12 ac = 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

  7. X = 33 Y = 12 ac = ? 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 = 3 Y = 2 XY=? X = 3 Y = 1 XY=? X = 6 Y = 3 XY=?

  8. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 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 • Assume by magic your friends give you the answer for these. X = 3 Y = 2 XY=6 X = 3 Y = 1 XY=3 X = 6 Y = 3 XY=18

  9. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 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 = 3 Y = 2 XY=6 X = 3 Y = 1 XY=3 X = 6 Y = 3 XY=18

  10. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 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 = 2 XY=6 X = 3 Y = 1 XY=3 X = 6 Y = 3 XY=18

  11. X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 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 = 3 Y = 2 XY=6 X = 3 Y = 1 XY=3 X = 6 Y = 3 XY=18

  12. 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 generic instances. 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. bd (a+b)(c+d) ac

  13. X = 2133 Y = 2312 ac = 483 bd = 396 (a+b)(c+d) = 1890 XY = 4931496 X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = 18 XY = 396 X = 54 Y = 35 ac = 15 bd = 20 (a+b)(c+d) = 72 XY = 1890 X = 2 Y = 2 XY=4 X = 1 Y = 3 XY=3 X = 3 Y = 5 XY=15 X = 3 Y = 1 XY=3 X = 3 Y = 2 XY=6 X = 6 Y = 3 XY=18 X = 5 Y = 3 XY=15 X = 4 Y = 5 XY=20 X = 9 Y = 8 XY=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 Friends 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. Circular Argument! 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. X = 33 Y = 12 ac = ? 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 • 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. Use brute force to get into the smallest house. Friends & Strong Induction • Recursive Algorithm: • Assume you have an algorithm that works. • Use it to write an algorithm that works.

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

  20. Towers of Hanoi

  21. Towers of Hanoi

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

  23. (2n) Time: T(1)=1, T(n) = 2T(n-1) + 1 = 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. 52 88 14 14,23,25,30,31,52,62,79,88,98 31 98 25 30 23 62 79 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. Divide and Conquer 52 88 14 31 98 25 30 23 62 79 Merge Sort

  28. (no real work) Get one friend to sort the first half. Get one friend to sort the second half. 52 88 14 25,31,52,88,98 14,23,30,62,79 31 98 25 30 23 62 79 Merge Sort Split Set into Two

  29. 25,31,52,88,98 14,23,30,62,79 14,23,25,30,31,52,62,79,88,98 Merge Sort Merge two sorted lists into one

  30. Merge Sort 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; } } Time: T(n) = = Q(n log(n)) 2T(n/2) + n (Homework 2. Assume n = 2k)

  31. Recursive Images if n=1 if n=0, draw n=0 else draw And recursively Draw here with n-1

  32. Recursive Images if n=2 if n=0, draw n=1 else draw And recursively Draw here with n-1

  33. Recursive Images if n=3 if n=0, draw n=2 else draw And recursively Draw here with n-1

  34. Recursive Images if n=30 if n=0, draw else draw And recursively Draw here with n-1

  35. if n=5 if n=1 if n=2 if n=3 if n=4 Recursive Images if n=0

  36. if n=1 if n=2 if n=3 if n=4 if n=5 Recursive Images if n=0

  37. Recursive Images Þ¥ = (4/3)n L(n) = 4/3 L(n-1)

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

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

More Related