1 / 138

Announcements

Announcements. Next lecture will be given by Dr. Leigh Johnston. 35. 30. 25. 20. T(n). 15. 10. 5. 0. 0. 5. 10. 15. 20. n. Master Theorem Case 3: When the Regularity Condition Fails. Alternative Iterative Algorithms for Binary Search. function mid=bsearch1(L,p,q,key)

Download Presentation

Announcements

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. Announcements • Next lecture will be given by Dr. Leigh Johnston.

  2. 35 30 25 20 T(n) 15 10 5 0 0 5 10 15 20 n Master Theorem Case 3: When the Regularity Condition Fails

  3. Alternative Iterative Algorithms for Binary Search function mid=bsearch1(L,p,q,key) while q>p mid=floor((p+q)/2); if key<=L(mid) q=mid; else p=mid+1; end end if key==L(p) mid=p; else mid=0; end function mid=bsearch2(L,p,q,key) while q>p mid=floor((p+q)/2); if key==L(mid) return elseif key<L(mid) q=mid-1; else p=mid+1; end end %NB: q may be invalid here if key==L(p) mid=p; else mid=0; end

  4. Central Algorithmic Techniques Recursion

  5. Different Representationsof Recursive Algorithms Views Pros

  6. 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 Pros and Cons?

  7. Runs on computers Precise and succinct Perception that being able to code is the only thing needed to get a job. (false) I am not a computer I need a higher level of intuition. Prone to bugs Language dependent Code Representation of an Algorithm Pros: Cons:

  8. Different Representationsof Recursive Algorithms Views Pros

  9. X = 2133 Y = 2312 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 Stack of Stack Frames Stack Frame: A particular execution of one routine on one particular input instance.

  10. X = 2133 Y = 2312 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 Stack of Stack Frames

  11. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 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 Stack of Stack Frames

  12. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 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 Stack of Stack Frames

  13. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = ? bd = (a+b)(c+d) = XY = X = 2 Y = 2 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 Stack of Stack Frames

  14. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = ? bd = (a+b)(c+d) = XY = X = 2 Y = 2 XY = 4 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 Stack of Stack Frames

  15. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 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 Stack of Stack Frames

  16. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 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 Stack of Stack Frames

  17. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = ? (a+b)(c+d) = XY = X = 1 Y = 3 XY = 3 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 Stack of Stack Frames

  18. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (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 Stack of Stack Frames

  19. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (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 Stack of Stack Frames

  20. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = ? XY = X = 3 Y = 5 XY = 15 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 Stack of Stack Frames

  21. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 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 Stack of Stack Frames

  22. X = 2133 Y = 2312 ac = ? bd = (a+b)(c+d) = XY = X = 21 Y = 23 ac = 4 bd = 3 (a+b)(c+d) = 15 XY = 483 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 Stack of Stack Frames

  23. X = 2133 Y = 2312 ac = 483 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 Stack of Stack Frames

  24. X = 2133 Y = 2312 ac = 483 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 Stack of Stack Frames

  25. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = ? bd = (a+b)(c+d) = XY = 15 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 Stack of Stack Frames

  26. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = ? bd = (a+b)(c+d) = XY = 15 X = 3 Y = 1 XY = 3 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 Stack of Stack Frames

  27. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = ? (a+b)(c+d) = XY = 15 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 Stack of Stack Frames

  28. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = ? (a+b)(c+d) = XY = 15 X = 3 Y = 2 XY = 6 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 Stack of Stack Frames

  29. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = ? XY = 15 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 Stack of Stack Frames

  30. X = 2133 Y = 2312 ac = 483 bd = ? (a+b)(c+d) = XY = X = 33 Y = 12 ac = 3 bd = 6 (a+b)(c+d) = ? 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 Stack of Stack Frames An so on ….

  31. Trace what actually occurs in the computer Concrete. It is what students attempt to describe when told not to give code. Described in words it is impossible to follow who is doing what. Does not explain why it works. Demonstrates for only one of many inputs. Stack of Stack Frames Representation of an Algorithm Pros: Cons:

  32. Different Representationsof Recursive Algorithms Views Pros

  33. 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 Stack Frames

  34. View the entire computation. Good for computing the running time. Must describe entire tree. For each stack frame input instance computation solution returned. who calls who Structure of tree may be complex. Stack of Stack Frames Representation of an Algorithm Pros: Cons:

  35. Different Representationsof Recursive Algorithms Views Pros

  36. 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 & Strong Induction One Friend for each stack frame. Each worries only about his job.

  37. 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 & Strong Induction Worry about one step at a time. Imagine that you are one specific friend.

  38. 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 • Consider your input instance

  39. 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 • 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=?

  40. 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 & Strong Induction • 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

  41. 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 & Strong Induction • 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

  42. 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 & Strong Induction • 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

  43. 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 & Strong Induction • 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

  44. 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 • 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

  45. 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 & Strong Induction This solves the problem for every possible instance.

  46. Friends & Strong Induction • Recursive Algorithm: • Assume you have an algorithm that works. • Use it to write an algorithm that works.

  47. Circular Argument! Friends & Strong Induction • 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.

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

  49. 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=?

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

More Related