1 / 98

Big O

Big O. David Kauchak cs161 Summer 2009. Administrative. Homework 1 is available and due Thur. at 5pm Homework grading/policies May turn in one homework late up to 2 days (48 hours) For final grade, we’ll drop your lowest score Note versioning Final time: 8/14 3:30-6:30pm

lakishak
Download Presentation

Big O

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. Big O David Kauchak cs161 Summer 2009

  2. Administrative • Homework 1 is available and due Thur. at 5pm • Homework grading/policies • May turn in one homework late up to 2 days (48 hours) • For final grade, we’ll drop your lowest score • Note versioning • Final time: 8/14 3:30-6:30pm • Wed. office hour location change: Gates 195

  3. Divide and Conquer • Divide: Break the problem into smaller subproblems • Conquer: Solve the subproblems. Generally, this involves waiting for the problem to be small enough that it is trivial to solve (i.e. 1 or 2 items) • Combine: Given the results of the solved subproblems, combine them to generate a solution for the complete problem

  4. Divide and Conquer: some thoughts • Often, the sub-problem is the same as the original problem • Dividing the problem in half frequently does the job • May have to get creative about how the data is split • Splitting tends to generate run times with log n in them

  5. Divide and Conquer: Sorting • How should we split the data? • What are the subproblems we need to solve? • How do we combine the results from these subproblems?

  6. MergeSort

  7. MergeSort: Merge • Assuming L and R are sorted already, merge the two to create a single sorted array

  8. Merge L: 1 3 5 8 R: 2 4 6 7

  9. Merge L: 1 3 5 8 R: 2 4 6 7 B:

  10. Merge L: 1 3 5 8 R: 2 4 6 7 B:

  11. Merge L: 1 3 5 8 R: 2 4 6 7 B:

  12. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1

  13. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1

  14. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2

  15. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2

  16. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3

  17. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3

  18. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4

  19. Merge L: 1 3 5 8 R: 2 4 6 7 B: 12 3 4

  20. Merge L: 1 3 5 8 R: 2 4 6 7 B: 12 3 4 5

  21. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4 5

  22. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4 5 6

  23. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6

  24. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7

  25. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7

  26. Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7 8

  27. Merge • Does the algorithm terminate?

  28. Merge • Is it correct? • Loop invariant: At the end of each iteration of the for loop of lines 4-10 the subarray B[1..k] contains the smallest k elements from L and R in sorted order.

  29. Merge • Is it correct? • Loop invariant: At the end of each iteration of the for loop of lines 4-10 the subarray B[1..k] contains the smallest k elements from L and R in sorted order.

  30. Merge • Running time?

  31. Merge • Running time? Θ(n) - linear

  32. Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4

  33. Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 8 5 1 3

  34. Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 8 5 1 3 8 5

  35. Merge-Sort: Conquer 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 8 5 1 3 Sorting a list of one element is easy 8 5

  36. Merge-Sort: Combine 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 5 8 1 3 8 5

  37. Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 5 8 1 3 1 3

  38. Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 5 8 1 3 1 3

  39. Merge-Sort: Merge 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 5 8 1 3 1 3

  40. Merge-Sort: Merge 8 5 1 3 6 2 7 4 1 3 5 8 6 2 7 4 5 8 1 3

  41. Merge-Sort: … 8 5 1 3 6 2 7 4 1 3 5 8 6 2 7 4 …

  42. Merge-Sort: … 8 5 1 3 6 2 7 4 1 3 5 8 2 4 6 7

  43. Merge-Sort: Merge 1 2 3 4 5 6 7 8 1 3 5 8 2 4 6 7 Done!

  44. MergeSort • Running time? D(n): cost of splitting (dividing) the dataC(n): cost of merging/combining the data

  45. MergeSort • Running time? D(n): cost of splitting (dividing) the data - linear Θ(n)C(n): cost of merging/combining the data – linearΘ(n)

  46. MergeSort • Running time? Which is?

  47. MergeSort cn T(n/2) T(n/2)

  48. MergeSort cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4)

  49. MergeSort cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8) T(n/8)

  50. MergeSort cn cn/2 cn/2 cn/4 cn/4 cn/4 cn/4 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 cn/8 … c c c c c … c c c c c c

More Related