CSE 2010: Algorithms and Data Structures Running time analysis of mergesort
current next current next current next p = 1 r = 8 q = 4 p = 1 r = 4 MERGE-SORT( A, 1, 8 ) current next MERGE-SORT( A, 1, 4 ) p = 1 r = 4 q = 2 current next MERGE-SORT( A, 1, 2 ) p = 1 r = 2 q = 1 current next MERGE-SORT( A, 1, 1) p = 2 r = 2
The divide-and-conquer approach Constant time if problem size is small enough What are the values for a and b in merge sort? a: number of subproblems T(n/b): time to solve subproblem of size n/b. Time to divide the problem into subproblems Time to merge the partial solutions into the solution of the original problems.
The divide-and-conquer: worst-case analysis For merge sort, c is equal to 1. The sum D(n) + C(n) is Theta(n) + Theta(1), which in turn is Theta(n).
The divide-and-conquer: worst-case analysis For merge sort, c is equal to 1. The sum D(n) + C(n) is Theta(n) + Theta(1), which in turn is Theta(n).
Time required to solve the problems of size 1, also the time per array element of the divide and combine steps. logn + 1 levels cn Let’s re-rewrite the recurrence equation. Height is logn. So, total time is of order n log n. Each level contributes with a cost of cn. So, total cost is cn long + cn Log n + 1, Base case is log 1 = 0. So, the correct number of levels is log n + 1. cn/2 cn/4 2 x (cn/2) = cn 4 x (cn/4) = cn cn c
a >= 1 (must be contant): Number of smaller subproblems f(n) is the combination time must be positive a >= 1 (must be contant): Number of smaller subproblems Each subproblem is 1/b the size of the original problem, with b > 1.
We need to memorize 3 cases
Example 1 1st Step: f(n) = (1/2)n^2+n which is polynomial. So, we are okay, and d = 2. a = 1 b = 2 2nd Step: Compare the values of a and b^d.
Example 2
Example 3
Example 4
Example 5
Example 6 Binary search
Example 7
Example 8
Example 9 The combination time f(n) must be positive.
http://www.cse.unt.edu/~tarau/teaching/cf1/Master%20theorem.pdf