Presentation is loading. Please wait.

Presentation is loading. Please wait.

8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method.

Similar presentations


Presentation on theme: "8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method."— Presentation transcript:

1 8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method

2 8/2/20152 Problem of the day How many multiplications do you need to compute 3 16 ? 3 16 =3 8 x 3 8 3 8 =3 4 x 3 4 3 4 =3 2 x 3 2 3 2 =3 x 3 3 16 =3 x 3 x 3 …. x 3 Answer: 15 Answer: 4

3 8/2/20153 Outline Review of last lecture – analyzing recursive algorithms –Defining recurrence relation Today: analyzing recursive algorithms –Solving recurrence relation

4 8/2/20154 Solving recurrence 1.Recursion tree or iteration method - Good for guessing an answer 2.Substitution method - Generic method, rigid, but may be hard 3.Master method - Easy to learn, useful in limited cases only - Some tricks may help in other cases

5 8/2/20155 Recurrence for merge sort T(n) =  (1) if n = 1; 2T(n/2) +  (n) if n > 1. We will usually ignore the base case, assuming it is always a constant (but not 0).

6 8/2/20156 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant.

7 8/2/20157 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n)T(n)

8 8/2/20158 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. T(n/2) dn

9 8/2/20159 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn T(n/4) dn/2

10 8/2/201510 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) …

11 8/2/201511 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n

12 8/2/201512 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn

13 8/2/201513 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn

14 8/2/201514 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn …

15 8/2/201515 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn #leaves = n (n)(n) …

16 8/2/201516 Recursion tree for merge sort Solve T(n) = 2T(n/2) + dn, where d > 0 is constant. dn dn/4 dn/2  (1) … h = log n dn #leaves = n (n)(n) Total  (n log n) … Later we will usually ignore d

17 8/2/201517 Recurrence for computing power int pow (b, n) m = n >> 1; p=pow(b,m)*pow(b,m); if (n % 2) return p * b; else return p; int pow (b, n) m = n >> 1; p = pow (b, m); p = p * p; if (n % 2) return p * b; else return p; T(n) = T(n/2)+  (1)T(n) = 2T(n/2)+  (1) Which algorithm is more efficient asymptotically?

18 8/2/201518 Time complexity for Alg1 Solve T(n) = T(n/2) + 1 T(n) = T(n/2) + 1 = T(n/4) + 1 + 1 = T(n/8) + 1 + 1 + 1 = T(1) + 1 + 1 + … + 1 = Θ (log(n)) log(n) Iteration method

19 8/2/201519 Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1.

20 8/2/201520 Time complexity for Alg2 Solve T(n) = 2T(n/2) + 1. T(n)T(n)

21 8/2/201521 Time complexity for Alg2 T(n/2) 1 Solve T(n) = 2T(n/2) + 1.

22 8/2/201522 Time complexity for Alg2 1 T(n/4) 1 1 Solve T(n) = 2T(n/2) + 1.

23 8/2/201523 Time complexity for Alg2 1 11 11 1 1  (1) … Solve T(n) = 2T(n/2) + 1.

24 8/2/201524 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n Solve T(n) = 2T(n/2) + 1.

25 8/2/201525 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n 1 Solve T(n) = 2T(n/2) + 1.

26 8/2/201526 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n 1 2 Solve T(n) = 2T(n/2) + 1.

27 8/2/201527 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n 1 2 4 … Solve T(n) = 2T(n/2) + 1.

28 8/2/201528 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n 1 2 4 #leaves = n (n)(n) … Solve T(n) = 2T(n/2) + 1.

29 8/2/201529 Time complexity for Alg2 1 11 11 1 1  (1) … h = log n 1 2 4 #leaves = n (n)(n) … Solve T(n) = 2T(n/2) + 1. Total  (n)

30 8/2/201530 More iteration method examples T(n) = T(n-1) + 1 = T(n-2) + 1 + 1 = T(n-3) + 1 + 1 + 1 = T(1) + 1 + 1 + … + 1 = Θ (n) n - 1

31 8/2/201531 More iteration method examples T(n) = T(n-1) + n = T(n-2) + (n-1) + n = T(n-3) + (n-2) + (n-1) + n = T(1) + 2 + 3 + … + n = Θ (n 2 )

32 8/2/201532 3-way-merge-sort 3-way-merge-sort (A[1..n]) If (n <= 1) return; 3-way-merge-sort(A[1..n/3]); 3-way-merge-sort(A[n/3+1..2n/3]); 3-way-merge-sort(A[2n/3+1.. n]); Merge A[1..n/3] and A[n/3+1..2n/3]; Merge A[1..2n/3] and A[2n/3+1..n]; Is this algorithm correct? What’s the recurrence function for the running time? What does the recurrence function solve to?

33 8/2/201533 Unbalanced-merge-sort ub-merge-sort (A[1..n]) if (n<=1) return; ub-merge-sort(A[1..n/3]); ub-merge-sort(A[n/3+1.. n]); Merge A[1.. n/3] and A[n/3+1..n]. Is this algorithm correct? What’s the recurrence function for the running time? What does the recurrence function solve to?

34 8/2/201534 More recursion tree examples T(n) = 3T(n/3) + n T(n) = T(n/3) + T(2n/3) + n T(n) = 3T(n/4) + n T(n) = 3T(n/4) + n 2


Download ppt "8/2/20151 Analysis of Algorithms Lecture: Solving recurrence by recursion-tree method."

Similar presentations


Ads by Google