980 likes | 1k Views
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
E N D
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 • Wed. office hour location change: Gates 195
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
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
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?
MergeSort: Merge • Assuming L and R are sorted already, merge the two to create a single sorted array
Merge L: 1 3 5 8 R: 2 4 6 7
Merge L: 1 3 5 8 R: 2 4 6 7 B:
Merge L: 1 3 5 8 R: 2 4 6 7 B:
Merge L: 1 3 5 8 R: 2 4 6 7 B:
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4
Merge L: 1 3 5 8 R: 2 4 6 7 B: 12 3 4
Merge L: 1 3 5 8 R: 2 4 6 7 B: 12 3 4 5
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4 5
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 3 4 5 6
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7
Merge L: 1 3 5 8 R: 2 4 6 7 B: 1 2 34 5 6 7 8
Merge • Does the algorithm terminate?
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.
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.
Merge • Running time?
Merge • Running time? Θ(n) - linear
Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4
Merge-Sort: Divide 8 5 1 3 6 2 7 4 8 5 1 3 6 2 7 4 8 5 1 3
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
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
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
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
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
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
Merge-Sort: Merge 8 5 1 3 6 2 7 4 1 3 5 8 6 2 7 4 5 8 1 3
Merge-Sort: … 8 5 1 3 6 2 7 4 1 3 5 8 6 2 7 4 …
Merge-Sort: … 8 5 1 3 6 2 7 4 1 3 5 8 2 4 6 7
Merge-Sort: Merge 1 2 3 4 5 6 7 8 1 3 5 8 2 4 6 7 Done!
MergeSort • Running time? D(n): cost of splitting (dividing) the dataC(n): cost of merging/combining the data
MergeSort • Running time? D(n): cost of splitting (dividing) the data - linear Θ(n)C(n): cost of merging/combining the data – linearΘ(n)
MergeSort • Running time? Which is?
MergeSort cn T(n/2) T(n/2)
MergeSort cn cn/2 cn/2 T(n/4) T(n/4) T(n/4) T(n/4)
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)
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