divide and conquer recurrences
play

Divide and Conquer Recurrences Aritra Hazra Department of Computer - PowerPoint PPT Presentation

Divide and Conquer Recurrences Aritra Hazra Department of Computer Science and Engineering, Indian Institute of Technology Kharagpur, Paschim Medinipur, West Bengal, India - 721302. Email: aritrah@cse.iitkgp.ac.in Autumn 2020 Aritra Hazra (CSE,


  1. Example-1: Find Maximum among n Elements Strategy-1.3: Base Cases. If n = 1, Return that element as maximum 1 If n = 2, Compare between these to get maximum Decomposition. Split the set of elements into two parts 2 having 2 elements and ( n − 2) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element,  T 3 (2) + T 3 ( n − 2) + 1 , if n > 2  T 3 ( n ) = 1 , if n = 2 0 , if n = 1  Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 5 / 34

  2. Example-1: Find Maximum among n Elements Strategy-1.3: Base Cases. If n = 1, Return that element as maximum 1 If n = 2, Compare between these to get maximum Decomposition. Split the set of elements into two parts 2 having 2 elements and ( n − 2) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element,  T 3 (2) + T 3 ( n − 2) + 1 , if n > 2  T 3 ( n ) = 1 , if n = 2 0 , if n = 1  Solution: T 3 ( n ) = T 3 (2) + T 3 ( n − 2) + 1 = T 3 ( n − 2) + 2 = T 3 ( n − 4) + 4 = T 3 ( n − 6) + 6 = · · · · · · � T 3 (2) + ( n − 2) if n is even = = n − 1 T 3 (1) + ( n − 1) if n is odd Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 5 / 34

  3. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  4. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  5. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  6. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  7. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) + 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  8. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) + 1 T 4 ( n ) − T 4 ( n − 1) n − 1 − 1 1 = ∴ n n − 1 n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  9. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) + 1 T 4 ( n ) − T 4 ( n − 1) n − 1 − 1 1 = ∴ n n − 1 n T 4 ( n − 1) − T 4 ( n − 2) 1 1 = n − 2 − n − 1 n − 2 n − 1 · · · · · · · · · · · · T 4 (3) − T 4 (2) = 1 2 − 1 3 2 3 T 4 (2) − T 4 (1) = 1 1 − 1 2 1 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  10. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) + 1 T 4 ( n ) − T 4 ( n − 1) n − 1 − 1 1 = Adding all these equations, we get, ∴ n n − 1 n T 4 ( n − 1) − T 4 ( n − 2) 1 1 = n − 2 − n − 1 n − 2 n − 1 T 4 ( n ) − T 4 (1) = 1 − 1 · · · · · · · · · · · · 1 n n T 4 (3) − T 4 (2) = 1 2 − 1 3 2 3 T 4 (2) − T 4 (1) = 1 1 − 1 2 1 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  11. Example-1: Find Maximum among n Elements Strategy-1.4: Base Cases. If n = 1, Return that element as maximum 1 Decomposition. Split the set of elements into two parts having c 2 elements and ( n − c ) elements in respective parts Recursion. Select maximum element from both parts 3 Recomposition. Compare both maximum to find largest 4 Recurrence: Number of comparison required to find maximum element, � T 4 ( c ) + T 4 ( n − c ) + 1 , if n > 1 T 4 ( n ) = 0 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of comparisons, T 4 ( n ) = � � � . [ T 4 ( i ) + T 4 ( n − i ) + 1] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) + ( n − 1) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) + ( n − 2) [ Put, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) + 1 T 4 ( n ) − T 4 ( n − 1) n − 1 − 1 1 = Adding all these equations, we get, ∴ n n − 1 n T 4 ( n − 1) − T 4 ( n − 2) 1 1 = n − 2 − n − 1 n − 2 n − 1 T 4 ( n ) − T 4 (1) = 1 − 1 · · · · · · · · · · · · 1 n n T 4 (3) − T 4 (2) = 1 2 − 1 3 2 3 ⇒ T 4 ( n ) = n − 1 T 4 (2) − T 4 (1) = 1 1 − 1 2 1 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 6 / 34

  12. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.1: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare between these to get max & min Decomposition. Split the set of elements into two equal parts 2 Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

  13. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.1: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare between these to get max & min Decomposition. Split the set of elements into two equal parts 2 Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, � 2 . T 1 ( n 2 ) + 2 , if n > 2 T 1 ( n ) = 1 , if n = 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

  14. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.1: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare between these to get max & min Decomposition. Split the set of elements into two equal parts 2 Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, � 2 . T 1 ( n 2 ) + 2 , if n > 2 T 1 ( n ) = 1 , if n = 2 Solution: Assume the existence of k , such that n = 2 k � n � n + 2 2 + 2 � � 2 2 . T 1 T 1 ( n ) = 2 . T 1 + 2 = 2 2 2 � n � + 2 3 + 2 2 + 2 2 3 . T 1 = = · · · · · · 2 3 n + 2 k − 1 + 2 k − 2 + · · · + 2 2 + 2 1 � � 2 k − 1 . T 1 = 2 k − 1 3 3 2 k − 1 + 2 k − 2 2 . 2 k − 2 = = = 2 . n − 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 7 / 34

  15. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.2: Base Case. If n = 1, Return that element as max & min 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

  16. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.2: Base Case. If n = 1, Return that element as max & min 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, � T 2 (1) + T 2 ( n − 1) + 2 , if n > 1 T 2 ( n ) = 0 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

  17. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.2: Base Case. If n = 1, Return that element as max & min 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements, � T 2 (1) + T 2 ( n − 1) + 2 , if n > 1 T 2 ( n ) = 0 , if n = 1 Solution: T 2 ( n ) = T 2 (1) + T 2 ( n − 1) + 2 = T 2 ( n − 1) + 2 = T 2 ( n − 2) + 4 = T 2 ( n − 3) + 6 = · · · · · · = T 2 (1) + 2( n − 1) = 2 n − 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 8 / 34

  18. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.3: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare in between to get max & min Decomposition. Split the set of elements into two parts 2 having 2 elements and ( n − 2) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

  19. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.3: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare in between to get max & min Decomposition. Split the set of elements into two parts 2 having 2 elements and ( n − 2) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements,  T 3 (2) + T 3 ( n − 2) + 2 , if n > 2  T 3 ( n ) = 1 , if n = 2 0 , if n = 1  Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

  20. Example-2: Find Max. & Min. (both) among n Elements Strategy-2.3: Base Case. If n = 1, Return that element as max & min 1 If n = 2, Compare in between to get max & min Decomposition. Split the set of elements into two parts 2 having 2 elements and ( n − 2) elements in respective parts Recursion. Select max & min elements from both parts 3 Recomposition. Compare both max to find largest 4 Compare both min to find smallest Recurrence: Number of comparison required to find max & min elements,  T 3 (2) + T 3 ( n − 2) + 2 , if n > 2  T 3 ( n ) = 1 , if n = 2 0 , if n = 1  Solution: Let, 2 m = n − 2 (if n is even) or 2 m = n − 1 (if n is odd) T 3 ( n ) = T 3 (2) + T 3 ( n − 2) + 2 = T 3 ( n − 2) + 3 = T 3 ( n − 4) + 6 = T 3 ( n − 6) + 9 = · · · · · · T 3 (2) + 3 m = 1 + 3 2 ( n − 2) = 3 � 2 . n − 2 , if n is even = T 3 (1) + 3 m = 0 + 3 2 ( n − 1) = 3 2 . n − 3 if n is odd 2 , Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 9 / 34

  21. Example-3: Search an Element within n Elements Strategy-3.1: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two equal parts 2 Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

  22. Example-3: Search an Element within n Elements Strategy-3.1: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two equal parts 2 Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � 2 . T 1 ( n 2 ) , if n > 1 T 1 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

  23. Example-3: Search an Element within n Elements Strategy-3.1: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two equal parts 2 Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � 2 . T 1 ( n 2 ) , if n > 1 T 1 ( n ) = 1 , if n = 1 Solution: Assume the existence of k , such that n = 2 k � n � n � � 2 2 . T 1 T 1 ( n ) = 2 . T 1 = = · · · · · · 2 2 2 � n � 2 k . T 1 2 k = = = n 2 k Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 10 / 34

  24. Example-3: Search an Element within n Elements Strategy-3.2: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (fractional) parts (say, 1 3 elements in left and 2 3 elements in right) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

  25. Example-3: Search an Element within n Elements Strategy-3.2: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (fractional) parts (say, 1 3 elements in left and 2 3 elements in right) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 3 ( n 3 ) + T 3 ( 2 n 3 ) , if n > 1 T 3 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

  26. Example-3: Search an Element within n Elements Strategy-3.2: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (fractional) parts (say, 1 3 elements in left and 2 3 elements in right) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 3 ( n 3 ) + T 3 ( 2 n 3 ) , if n > 1 T 3 ( n ) = 1 , if n = 1 Solution: Using strong mathematical induction, we can prove that (assume T 3 ( k ) = ak + b as induction hypothesis for all k < n ), T 3 (1) = 1 (Base + 2( an + b ) Case satisfied for all a = 1 − b ) and T 3 ( n ) = an + b = an + b . 3 3 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

  27. Example-3: Search an Element within n Elements Strategy-3.2: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (fractional) parts (say, 1 3 elements in left and 2 3 elements in right) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 3 ( n 3 ) + T 3 ( 2 n 3 ) , if n > 1 T 3 ( n ) = 1 , if n = 1 Solution: Using strong mathematical induction, we can prove that (assume T 3 ( k ) = ak + b as induction hypothesis for all k < n ), T 3 (1) = 1 (Base + 2( an + b ) Case satisfied for all a = 1 − b ) and T 3 ( n ) = an + b = an + b . 3 3 It may be noted that, � n � n � 2 n � 2 n � 2 n � 4 n � � � � � � T 3 ( n ) = T 3 + T 3 = T 3 + T 3 + T 3 + T 3 3 2 3 2 3 2 3 2 3 3 � n � 2 n � 4 n � � � = + 2 T 3 + T 3 T 3 3 2 3 2 3 2 � n � 3 � 3 � 2 n � 3 � 4 n � 3 � 8 n � � � � � � � � = . T 3 + . T 3 + . T 3 + . T 3 3 3 3 3 3 3 3 3 0 1 2 3 k � 2 i . n � k � � � = · · · · · · = . T i 3 k i =0 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 11 / 34

  28. Example-3: Search an Element within n Elements Strategy-3.3: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

  29. Example-3: Search an Element within n Elements Strategy-3.3: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 2 (1) + T 2 ( n − 1) , if n > 1 T 2 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

  30. Example-3: Search an Element within n Elements Strategy-3.3: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two parts 2 having 1 element and ( n − 1) elements in respective parts Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 2 (1) + T 2 ( n − 1) , if n > 1 T 2 ( n ) = 1 , if n = 1 Solution: [ known as Linear Search ] T 2 ( n ) = T 2 (1) + T 2 ( n − 1) = T 2 ( n − 1) + 1 = T 2 ( n − 2) + 2 = T 2 ( n − 3) + 3 = · · · · · · = T 2 (1) + ( n − 1) = n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 12 / 34

  31. Example-3: Search an Element within n Elements Strategy-3.4: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (constant-depth) parts (say, c elements in left and ( n − c ) elements in right), for an arbitrary constant ( c ) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

  32. Example-3: Search an Element within n Elements Strategy-3.4: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (constant-depth) parts (say, c elements in left and ( n − c ) elements in right), for an arbitrary constant ( c ) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 4 ( c ) + T 4 ( n − c ) , if n > 1 T 4 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

  33. Example-3: Search an Element within n Elements Strategy-3.4: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (constant-depth) parts (say, c elements in left and ( n − c ) elements in right), for an arbitrary constant ( c ) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 4 ( c ) + T 4 ( n − c ) , if n > 1 T 4 ( n ) = 1 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of probes, T 4 ( n ) = � � . � [ T 4 ( i ) + T 4 ( n − i )] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

  34. Example-3: Search an Element within n Elements Strategy-3.4: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (constant-depth) parts (say, c elements in left and ( n − c ) elements in right), for an arbitrary constant ( c ) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 4 ( c ) + T 4 ( n − c ) , if n > 1 T 4 ( n ) = 1 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of probes, T 4 ( n ) = � � . � [ T 4 ( i ) + T 4 ( n − i )] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) [ Putting, n ← n − 1 ] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

  35. Example-3: Search an Element within n Elements Strategy-3.4: Base Case. If n = 1, Compare and Return found / not-found 1 Decomposition. Split the set of elements into two unequal 2 (constant-depth) parts (say, c elements in left and ( n − c ) elements in right), for an arbitrary constant ( c ) Recursion. Search the element from both parts 3 Recomposition. Return found if element found in any part 4 Recurrence: Number of comparison required to search/find an element, � T 4 ( c ) + T 4 ( n − c ) , if n > 1 T 4 ( n ) = 1 , if n = 1 Solution: Assuming the choice of constant c (1 ≤ c ≤ n − 1) is equally likely, the n − 1 1 average number of probes, T 4 ( n ) = � � . � [ T 4 ( i ) + T 4 ( n − i )] n − 1 i =1 implies, ( n − 1) . T 4 ( n ) = 2 . � n − 1 i =1 T 4 ( i ) Similarly, ( n − 2) . T 4 ( n − 1) = 2 . � n − 2 i =1 T 4 ( i ) [ Putting, n ← n − 1 ] Subtracting, we get, ( n − 1) . T 4 ( n ) − ( n − 2) . T 4 ( n − 1) = 2 . T 4 ( n − 1) � n − 1 n n � � � � � ⇒ T 4 ( n ) = . T 4 ( n − 1) = . . T 4 ( n − 2) = · · · = n . T (1) = n n − 1 n − 1 n − 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 13 / 34

  36. Example-4: Binary Search from n (Sorted) Elements Strategy-4.1: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at middle and Return found if matches 2 Otherwise, Split the set of elements into two equal parts Recursion. If query-element is lesser (or greater) than the middle 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

  37. Example-4: Binary Search from n (Sorted) Elements Strategy-4.1: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at middle and Return found if matches 2 Otherwise, Split the set of elements into two equal parts Recursion. If query-element is lesser (or greater) than the middle 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 1 ( n 2 ) + 1 , if n > 1 T 1 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

  38. Example-4: Binary Search from n (Sorted) Elements Strategy-4.1: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at middle and Return found if matches 2 Otherwise, Split the set of elements into two equal parts Recursion. If query-element is lesser (or greater) than the middle 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 1 ( n 2 ) + 1 , if n > 1 T 1 ( n ) = 1 , if n = 1 Solution: Assume the existence of k , such that n = 2 k � n � n � n � � � T 1 ( n ) = T 1 + 1 = T 1 + 2 = T 1 + 3 2 2 2 2 3 � n � = · · · · · · = T 1 + k = 1 + k = 1 + log 2 n 2 k Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 14 / 34

  39. Example-4: Binary Search from n (Sorted) Elements Strategy-4.2: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (fractional) position (say, 1 3 rd) 2 and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1 3 elements in left part and 2 3 elements in right part) Recursion. If query-element is lesser (or greater) than the 1 3 rd 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

  40. Example-4: Binary Search from n (Sorted) Elements Strategy-4.2: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (fractional) position (say, 1 3 rd) 2 and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1 3 elements in left part and 2 3 elements in right part) Recursion. If query-element is lesser (or greater) than the 1 3 rd 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 2 ( 2 n 3 ) + 1 , if n > 1 T 2 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

  41. Example-4: Binary Search from n (Sorted) Elements Strategy-4.2: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (fractional) position (say, 1 3 rd) 2 and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1 3 elements in left part and 2 3 elements in right part) Recursion. If query-element is lesser (or greater) than the 1 3 rd 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 2 ( 2 n 3 ) + 1 , if n > 1 T 2 ( n ) = 1 , if n = 1 � 3 � k Solution: Assume the existence of k , such that n = 2 � n � n � 2 n � � � T 2 ( n ) = T 2 + 1 = T 2 + 2 = T 2 + 3 3 ( 3 ( 3 2 ) 2 2 ) 3 n � � = · · · · · · = T 2 + k = 1 + k = 1 + log 3 2 n ( 3 2 ) k Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

  42. Example-4: Binary Search from n (Sorted) Elements Strategy-4.2: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (fractional) position (say, 1 3 rd) 2 and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., 1 3 elements in left part and 2 3 elements in right part) Recursion. If query-element is lesser (or greater) than the 1 3 rd 3 element, Search the elements from left (or right) part Recomposition. Return found if query-element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 2 ( 2 n 3 ) + 1 , if n > 1 T 2 ( n ) = 1 , if n = 1 � 3 � k Solution: Assume the existence of k , such that n = 2 � n � n � 2 n � � � T 2 ( n ) = T 2 + 1 = T 2 + 2 = T 2 + 3 3 ( 3 ( 3 2 ) 2 2 ) 3 n � � = · · · · · · = T 2 + k = 1 + k = 1 + log 3 2 n ( 3 2 ) k For α n and (1 − α ) n splits ( 1 Generalized Form: 2 < α < 1), T 2 ( n ) = 1 + log 1 α n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 15 / 34

  43. Example-4: Binary Search from n (Sorted) Elements Strategy-4.3: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at two arbitrary (fractional) positions (say, 2 1 3 rd and 2 3 rd) and Return found if matches Otherwise, Split the set of elements into three equal parts (i.e., 1 3 elements in each of left, middle and right parts) Recursion. If query-element is lesser than 1 3 rd (or greater than 3 2 3 rd) element, Search the element from left (or right) part. Otherwise, search the element from middle part. Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

  44. Example-4: Binary Search from n (Sorted) Elements Strategy-4.3: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at two arbitrary (fractional) positions (say, 2 1 3 rd and 2 3 rd) and Return found if matches Otherwise, Split the set of elements into three equal parts (i.e., 1 3 elements in each of left, middle and right parts) Recursion. If query-element is lesser than 1 3 rd (or greater than 3 2 3 rd) element, Search the element from left (or right) part. Otherwise, search the element from middle part. Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 3 ( n 3 ) + 2 , if n > 1 T 3 ( n ) = 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

  45. Example-4: Binary Search from n (Sorted) Elements Strategy-4.3: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at two arbitrary (fractional) positions (say, 2 1 3 rd and 2 3 rd) and Return found if matches Otherwise, Split the set of elements into three equal parts (i.e., 1 3 elements in each of left, middle and right parts) Recursion. If query-element is lesser than 1 3 rd (or greater than 3 2 3 rd) element, Search the element from left (or right) part. Otherwise, search the element from middle part. Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 3 ( n 3 ) + 2 , if n > 1 T 3 ( n ) = 1 , if n = 1 Solution: Assume the existence of k , such that n = 3 k � n � n � n � � � T 3 ( n ) = T 3 + 2 = T 3 + 4 = T 3 + 6 3 3 2 3 3 � n � = · · · · · · = T 3 + 2 . k = 1 + 2 . k = 1 + 2 log 3 n 3 k Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

  46. Example-4: Binary Search from n (Sorted) Elements Strategy-4.3: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at two arbitrary (fractional) positions (say, 2 1 3 rd and 2 3 rd) and Return found if matches Otherwise, Split the set of elements into three equal parts (i.e., 1 3 elements in each of left, middle and right parts) Recursion. If query-element is lesser than 1 3 rd (or greater than 3 2 3 rd) element, Search the element from left (or right) part. Otherwise, search the element from middle part. Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element, � T 3 ( n 3 ) + 2 , if n > 1 T 3 ( n ) = 1 , if n = 1 Solution: Assume the existence of k , such that n = 3 k � n � n � n � � � T 3 ( n ) = T 3 + 2 = T 3 + 4 = T 3 + 6 3 3 2 3 3 � n � = · · · · · · = T 3 + 2 . k = 1 + 2 . k = 1 + 2 log 3 n 3 k Generalized Form: For β equal-sized splits (2 ≤ β ≤ n ), T 2 ( n ) = 1 + ( β − 1) log β n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 16 / 34

  47. Example-4: Binary Search from n (Sorted) Elements Strategy-4.4: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (constant-depth) positions (say, 2 a constant c th element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., ( c − 1) elements in left part and ( n − c ) elements in right part) Recursion. If query-element is lesser (or greater) than the c th 3 element, Search the element from left (or right) part Recomposition. Return found if element found in any part 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

  48. Example-4: Binary Search from n (Sorted) Elements Strategy-4.4: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (constant-depth) positions (say, 2 a constant c th element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., ( c − 1) elements in left part and ( n − c ) elements in right part) Recursion. If query-element is lesser (or greater) than the c th 3 element, Search the element from left (or right) part Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element (let c < n 2 ), � T 4 ( n − c ) + 1 , if n > c T 4 ( n ) = n , if 1 ≤ n ≤ c Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

  49. Example-4: Binary Search from n (Sorted) Elements Strategy-4.4: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (constant-depth) positions (say, 2 a constant c th element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., ( c − 1) elements in left part and ( n − c ) elements in right part) Recursion. If query-element is lesser (or greater) than the c th 3 element, Search the element from left (or right) part Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element (let c < n 2 ), � T 4 ( n − c ) + 1 , if n > c T 4 ( n ) = n , if 1 ≤ n ≤ c = � 1 Solution: T 4 ( n ) = T 4 ( n − c )+1 = T 4 ( n − 2 c )+2 = · · · ≤ T 4 ( c )+ n − c � . n + ( c − 1) c c � 1 T 4 ( n ) = T 4 ( n − c )+ 1 = T 4 ( n − 2 c )+ 2 = · · · ≥ T 4 (1)+ n − 1 . n + c − 1 � = c c c Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

  50. Example-4: Binary Search from n (Sorted) Elements Strategy-4.4: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (constant-depth) positions (say, 2 a constant c th element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., ( c − 1) elements in left part and ( n − c ) elements in right part) Recursion. If query-element is lesser (or greater) than the c th 3 element, Search the element from left (or right) part Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element (let c < n 2 ), � T 4 ( n − c ) + 1 , if n > c T 4 ( n ) = n , if 1 ≤ n ≤ c = � 1 Solution: T 4 ( n ) = T 4 ( n − c )+1 = T 4 ( n − 2 c )+2 = · · · ≤ T 4 ( c )+ n − c � . n + ( c − 1) c c � 1 T 4 ( n ) = T 4 ( n − c )+ 1 = T 4 ( n − 2 c )+ 2 = · · · ≥ T 4 (1)+ n − 1 . n + c − 1 � = c c c [Caution] It can be as bad as linear search (if c = 1 is chosen) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

  51. Example-4: Binary Search from n (Sorted) Elements Strategy-4.4: Base Case. If n = 1, Probe and Return found / not-found 1 Decomposition. Probe at arbitrary (constant-depth) positions (say, 2 a constant c th element) and Return found if matches Otherwise, Split the set of elements into two unequal parts (i.e., ( c − 1) elements in left part and ( n − c ) elements in right part) Recursion. If query-element is lesser (or greater) than the c th 3 element, Search the element from left (or right) part Recomposition. Return found if element found in any part 4 Recurrence: Number of probes (assume each probe can decide whether <, = , > ) required to search/find an element (let c < n 2 ), � T 4 ( n − c ) + 1 , if n > c T 4 ( n ) = n , if 1 ≤ n ≤ c = � 1 Solution: T 4 ( n ) = T 4 ( n − c )+1 = T 4 ( n − 2 c )+2 = · · · ≤ T 4 ( c )+ n − c � . n + ( c − 1) c c � 1 T 4 ( n ) = T 4 ( n − c )+ 1 = T 4 ( n − 2 c )+ 2 = · · · ≥ T 4 (1)+ n − 1 . n + c − 1 � = c c c [Caution] It can be as bad as linear search (if c = 1 is chosen) Insights from Recurrence Relations: Why Binary Search needs to Split at Middle? Since, log 2 n ≤ log 3 2 n [ i . e . log 1 α n ] and log 2 n ≤ 2 . log 3 n [ i . e . ( β − 1) log β n ], Therefore, T 1 ( n ) ≤ T 2 ( n ) and T 1 ( n ) ≤ T 3 ( n ). Also, T 1 ( n ) ≤ T 4 ( n ) (implying lowest number of probes when splitting at middle position) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 17 / 34

  52. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  53. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Recurrence: Number of element comparisons done for sorting, [ Selection Sort ] � T ( n − 1) + ( n − 1) , if n > 1 T ( n ) = 0 , n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  54. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Recurrence: Number of element comparisons done for sorting, [ Selection Sort ] � T ( n − 1) + ( n − 1) , if n > 1 T ( n ) = 0 , n = 1 Solution: T ( n ) = T ( n − 1) + ( n − 1) = T ( n − 2) + ( n − 2) + ( n − 1) 2 . n 2 − 1 1 = · · · = T (1) + 1 + 2 + · · · + ( n − 1) = 2 . n Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  55. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Recurrence: Number of element comparisons done for sorting, [ Selection Sort ] � T ( n − 1) + ( n − 1) , if n > 1 T ( n ) = 0 , n = 1 Solution: T ( n ) = T ( n − 1) + ( n − 1) = T ( n − 2) + ( n − 2) + ( n − 1) 2 . n 2 − 1 1 = · · · = T (1) + 1 + 2 + · · · + ( n − 1) = 2 . n Strategy-5.1B: Base Case. If n = 2 Return max followed by min elements 1 Decomposition. Find � max , min � elements and S ′ ← S − { max , min } 2 Recursion. Sort S ′ with ( n − 2) elements 3 Recomposition. Return � max , sorted elements of S ′ , min � in order 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  56. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Recurrence: Number of element comparisons done for sorting, [ Selection Sort ] � T ( n − 1) + ( n − 1) , if n > 1 T ( n ) = 0 , n = 1 Solution: T ( n ) = T ( n − 1) + ( n − 1) = T ( n − 2) + ( n − 2) + ( n − 1) 2 . n 2 − 1 1 = · · · = T (1) + 1 + 2 + · · · + ( n − 1) = 2 . n Strategy-5.1B: Base Case. If n = 2 Return max followed by min elements 1 Decomposition. Find � max , min � elements and S ′ ← S − { max , min } 2 Recursion. Sort S ′ with ( n − 2) elements 3 Recomposition. Return � max , sorted elements of S ′ , min � in order 4 Recurrence: Number of element comparisons done for sorting (assuming n as even), T ( n − 2) + ( 3 � 2 . n − 1) , if n > 2 T ( n ) = 1 , n = 2 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  57. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.1A: Base Case. If n = 1, Return element 1 Decomposition. Find max element and S ′ ← S − { max } 2 Recursion. Sort S ′ with ( n − 1) elements 3 Recomposition. Return max followed by sorted elements of S ′ 4 Recurrence: Number of element comparisons done for sorting, [ Selection Sort ] � T ( n − 1) + ( n − 1) , if n > 1 T ( n ) = 0 , n = 1 Solution: T ( n ) = T ( n − 1) + ( n − 1) = T ( n − 2) + ( n − 2) + ( n − 1) 2 . n 2 − 1 1 = · · · = T (1) + 1 + 2 + · · · + ( n − 1) = 2 . n Strategy-5.1B: Base Case. If n = 2 Return max followed by min elements 1 Decomposition. Find � max , min � elements and S ′ ← S − { max , min } 2 Recursion. Sort S ′ with ( n − 2) elements 3 Recomposition. Return � max , sorted elements of S ′ , min � in order 4 Recurrence: Number of element comparisons done for sorting (assuming n as even), T ( n − 2) + ( 3 � 2 . n − 1) , if n > 2 T ( n ) = 1 , n = 2 Solution: T ( n ) = T ( n − 2) + ( 3 2 . n − 1) = T ( n − 4) + 3 2 . [( n − 2) + n ] − 2 = · · · 8 . n 2 − 1 = T (2) + 3 2 . [4 + 6 + · · · + ( n − 1)] − n − 2 3 2 . n − 11 = 2 8 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 18 / 34

  58. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.2: Base Case. If n = 1, Return element 1 Decomposition. Split S into two non-empty sets, S 1 and S 2 2 Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Combine sorted elements of S 1 with S 2 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

  59. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.2: Base Case. If n = 1, Return element 1 Decomposition. Split S into two non-empty sets, S 1 and S 2 2 Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Combine sorted elements of S 1 with S 2 4 Combine-Step: If S 1 (or S 2 ) is empty, Return elements of S 2 (or S 1 ) 1 Compare first elements, a 1 ∈ S 1 with b 1 ∈ S 2 2 If a 1 ≥ b 1 , Return a 1 followed by combined sorted elements of 3 S 1 − { a 1 } with S 2 . Otherwise, Return b 1 followed by combined sorted elements of S 1 with S 2 − { b 1 } . Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

  60. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.2: Base Case. If n = 1, Return element 1 Decomposition. Split S into two non-empty sets, S 1 and S 2 2 Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Combine sorted elements of S 1 with S 2 4 Combine-Step: If S 1 (or S 2 ) is empty, Return elements of S 2 (or S 1 ) 1 Compare first elements, a 1 ∈ S 1 with b 1 ∈ S 2 2 If a 1 ≥ b 1 , Return a 1 followed by combined sorted elements of 3 S 1 − { a 1 } with S 2 . Otherwise, Return b 1 followed by combined sorted elements of S 1 with S 2 − { b 1 } . Recurrence: Number of comparisons done for combining, [ Merge ] � MAX [ T C ( j − 1 , n − j ) , T C ( j , n − j − 1)] + 1 , if 1 ≤ j < n T C ( j , n − j ) = 0 , otherwise Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

  61. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.2: Base Case. If n = 1, Return element 1 Decomposition. Split S into two non-empty sets, S 1 and S 2 2 Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Combine sorted elements of S 1 with S 2 4 Combine-Step: If S 1 (or S 2 ) is empty, Return elements of S 2 (or S 1 ) 1 Compare first elements, a 1 ∈ S 1 with b 1 ∈ S 2 2 If a 1 ≥ b 1 , Return a 1 followed by combined sorted elements of 3 S 1 − { a 1 } with S 2 . Otherwise, Return b 1 followed by combined sorted elements of S 1 with S 2 − { b 1 } . Recurrence: Number of comparisons done for combining, [ Merge ] � MAX [ T C ( j − 1 , n − j ) , T C ( j , n − j − 1)] + 1 , if 1 ≤ j < n T C ( j , n − j ) = 0 , otherwise Number of comparisons done for overall sorting, [ Merge-Sort ] � T ( i ) + T ( n − i ) + T C ( i , n − i ) , if n > 1 [ Arbitrary Split ] T ( n ) = 0 , if n = 1 � T � n � n � n � � 2 , n � + T + T C if n > 1 , [ Middle Split ] T ( n ) = 2 2 2 0 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 19 / 34

  62. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.3: Base Case. If n = 1, Return element 1 Decomposition. Choose a pivot element p ∈ S . Partition S into 2 two non-empty sets, S 1 = { a | a ≥ p } and S 2 = { a | a < p } Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Return sorted elements of S 1 followed by S 2 4 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

  63. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.3: Base Case. If n = 1, Return element 1 Decomposition. Choose a pivot element p ∈ S . Partition S into 2 two non-empty sets, S 1 = { a | a ≥ p } and S 2 = { a | a < p } Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Return sorted elements of S 1 followed by S 2 4 Partition-Step: Linear scan elements of S and put into S 1 and S 2 sets. Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

  64. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.3: Base Case. If n = 1, Return element 1 Decomposition. Choose a pivot element p ∈ S . Partition S into 2 two non-empty sets, S 1 = { a | a ≥ p } and S 2 = { a | a < p } Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Return sorted elements of S 1 followed by S 2 4 Partition-Step: Linear scan elements of S and put into S 1 and S 2 sets. Recurrence: Number of comparisons done for partitioning, [ Partition ] � T P (1) + T P ( n − 1) , if n > 1 T P ( n ) = ⇒ T P ( n ) = n 1 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

  65. Example-5: Sort n-element Set S (in Descending Order) Strategy-5.3: Base Case. If n = 1, Return element 1 Decomposition. Choose a pivot element p ∈ S . Partition S into 2 two non-empty sets, S 1 = { a | a ≥ p } and S 2 = { a | a < p } Recursion. Sort S 1 and S 2 set elements 3 Recomposition. Return sorted elements of S 1 followed by S 2 4 Partition-Step: Linear scan elements of S and put into S 1 and S 2 sets. Recurrence: Number of comparisons done for partitioning, [ Partition ] � T P (1) + T P ( n − 1) , if n > 1 T P ( n ) = ⇒ T P ( n ) = n 1 , if n = 1 Number of comparisons done for overall sorting, [ Quick-Sort ] � T ( i ) + T ( n − i ) + T P ( n ) , if n > 1 [ Arbitrary Split ] T ( n ) = 0 , if n = 1 � T � n � n � � + T + T P ( n ) , if n > 1 [ Middle Split ] T ( n ) = 2 2 0 , if n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 20 / 34

  66. General Form of (Equal) Divide and Conquer Recerrence Recurrence Relation: Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a function, � n n = b i > 1 � a . T � + f ( n ) T ( n ) = b c , n = 1 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 21 / 34

  67. General Form of (Equal) Divide and Conquer Recerrence Recurrence Relation: Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a function, � n n = b i > 1 � a . T � + f ( n ) T ( n ) = b c , n = 1 � n � Recursion Tree: Step-wise unfolded form of computations from T ( n ) = a . T + f ( n ) b Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 21 / 34

  68. General Form of (Equal) Divide and Conquer Recerrence Solution: Unfolding the computation steps as shown in the recursion tree, we get, � + f ( n ) � + a . f � n � + f ( n ) a 2 . T � n a . T � n T ( n ) = = = · · · · · · b 2 b b log b n − 1 i − 1 a i . T � n � + a j . f � n � = c . n log b a + a j . f � n [ as n = b i ] � � � = b i b j b j j =0 j =0 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

  69. General Form of (Equal) Divide and Conquer Recerrence Solution: Unfolding the computation steps as shown in the recursion tree, we get, � + f ( n ) � + a . f � n � + f ( n ) a 2 . T � n a . T � n T ( n ) = = = · · · · · · b 2 b b log b n − 1 i − 1 a i . T � n � + a j . f � n � = c . n log b a + a j . f � n [ as n = b i ] � � � = b i b j b j j =0 j =0 Case-1: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then log b n − 1 log b n − 1 � n � n � log b a − ǫ � a j . f � a j . � g ( n ) = ≤ d . b j b j j =0 j =0 log b n − 1 log b n − 1 � a . b ǫ � j d . n log b a − ǫ . � = d . n log b a − ǫ . � ( b ǫ ) j = b log b a j =0 j =0 � n ǫ − 1 � b ǫ. log b n − 1 � � d . n log b a − ǫ . = d . n log b a − ǫ . = b ǫ − 1 b ǫ − 1 D . n log b a ≤ [ for some constant D > 0] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

  70. General Form of (Equal) Divide and Conquer Recerrence Solution: Unfolding the computation steps as shown in the recursion tree, we get, � + f ( n ) � + a . f � n � + f ( n ) a 2 . T � n a . T � n T ( n ) = = = · · · · · · b 2 b b log b n − 1 i − 1 a i . T � n � + a j . f � n � = c . n log b a + a j . f � n [ as n = b i ] � � � = b i b j b j j =0 j =0 Case-1: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then log b n − 1 log b n − 1 � n � n � log b a − ǫ � a j . f � a j . � g ( n ) = ≤ d . b j b j j =0 j =0 log b n − 1 log b n − 1 � a . b ǫ � j d . n log b a − ǫ . � = d . n log b a − ǫ . � ( b ǫ ) j = b log b a j =0 j =0 � n ǫ − 1 � b ǫ. log b n − 1 � � d . n log b a − ǫ . = d . n log b a − ǫ . = b ǫ − 1 b ǫ − 1 D . n log b a ≤ [ for some constant D > 0] T ( n ) ≤ c . n log b a + D . n log b a ≤ C . n log b a So, [for some constant C > 0] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 22 / 34

  71. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � Case-2: We had, T ( n ) = � = b j j =0 If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 > 0, then Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

  72. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � Case-2: We had, T ( n ) = � = b j j =0 If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 > 0, then log b n − 1 log b n − 1 a j . f � n a j . � n � log b a � � � g ( n ) = ≤ d 2 . b j b j j =0 j =0 log b n − 1 log b n − 1 a � j � d 2 . n log b a . � d 2 . n log b a . � = = 1 b log b a j =0 j =0 d 2 . n log b a . log b n D 2 . n log b a . log 2 n [ for some constant D 2 > 0] = ≤ Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

  73. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � Case-2: We had, T ( n ) = � = b j j =0 If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 > 0, then log b n − 1 log b n − 1 a j . f � n a j . � n � log b a � � � g ( n ) = ≤ d 2 . b j b j j =0 j =0 log b n − 1 log b n − 1 a � j � d 2 . n log b a . � d 2 . n log b a . � = = 1 b log b a j =0 j =0 d 2 . n log b a . log b n D 2 . n log b a . log 2 n [ for some constant D 2 > 0] = ≤ D 1 . n log b a . log 2 n Similarly, g ( n ) ≥ [for some constant D 1 > 0] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

  74. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � Case-2: We had, T ( n ) = � = b j j =0 If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 > 0, then log b n − 1 log b n − 1 a j . f � n a j . � n � log b a � � � g ( n ) = ≤ d 2 . b j b j j =0 j =0 log b n − 1 log b n − 1 a � j � d 2 . n log b a . � d 2 . n log b a . � = = 1 b log b a j =0 j =0 d 2 . n log b a . log b n D 2 . n log b a . log 2 n [ for some constant D 2 > 0] = ≤ D 1 . n log b a . log 2 n Similarly, g ( n ) ≥ [for some constant D 1 > 0] Therefore, c . n log b a + D 1 . n log b a . log 2 n ≤ ≤ c . n log b a + D 2 . n log b a . log 2 n T ( n ) C 1 . n log b a . log 2 n ≤ ≤ C 2 . n log b a . log 2 n ⇒ T ( n ) [for some constants C 1 , C 2 > 0] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 23 / 34

  75. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � � Case-3: We had, T ( n ) = = b j j =0 If f ( n ) ≥ d . n log b a + ǫ for some constant d , ǫ > 0, and a . f ( n b ) ≤ k . f ( n ) for some constant k < 1 and for all sufficiently large n ≥ b , then Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

  76. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � � Case-3: We had, T ( n ) = = b j j =0 If f ( n ) ≥ d . n log b a + ǫ for some constant d , ǫ > 0, and a . f ( n b ) ≤ k . f ( n ) for some constant k < 1 and for all sufficiently large n ≥ b , then � n � n � n ≤ k ≤ k � n � k � 2 � � � � a . f ≤ k . f ( n ) ⇒ f a . f ( n ) ⇒ f a . f ≤ . f ( n ) b 2 b b b a f ( n b j ) ≤ ( k a ) j . f ( n ). Hence, Iterating in this manner, we get, Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

  77. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � � Case-3: We had, T ( n ) = = b j j =0 If f ( n ) ≥ d . n log b a + ǫ for some constant d , ǫ > 0, and a . f ( n b ) ≤ k . f ( n ) for some constant k < 1 and for all sufficiently large n ≥ b , then � n � n � n ≤ k ≤ k � n � k � 2 � � � � a . f ≤ k . f ( n ) ⇒ f a . f ( n ) ⇒ f a . f ≤ . f ( n ) b 2 b b b a f ( n b j ) ≤ ( k a ) j . f ( n ). Hence, Iterating in this manner, we get, log b n − 1 log b n − 1 log b n − 1 � n a j . ( k � a j . f � a ) j . f ( n ) � k j . f ( n ) � g ( n ) = ≤ = b j j =0 j =0 j =0 ∞ 1 � � � k j ≤ f ( n ) . = . f ( n ) 1 − k j =0 Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

  78. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � � Case-3: We had, T ( n ) = = b j j =0 If f ( n ) ≥ d . n log b a + ǫ for some constant d , ǫ > 0, and a . f ( n b ) ≤ k . f ( n ) for some constant k < 1 and for all sufficiently large n ≥ b , then � n � n � n ≤ k ≤ k � n � k � 2 � � � � a . f ≤ k . f ( n ) ⇒ f a . f ( n ) ⇒ f a . f ≤ . f ( n ) b 2 b b b a f ( n b j ) ≤ ( k a ) j . f ( n ). Hence, Iterating in this manner, we get, log b n − 1 log b n − 1 log b n − 1 � n a j . ( k � a j . f � a ) j . f ( n ) � k j . f ( n ) � g ( n ) = ≤ = b j j =0 j =0 j =0 ∞ 1 � � � k j ≤ f ( n ) . = . f ( n ) 1 − k j =0 Since k < 1 is a constant, for exact powers of b we can conclude that, D 1 . f ( n ) ≤ g ( n ) ≤ D 2 . f ( n ) [ for some constants D 1 , D 2 > 0] Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

  79. General Form of (Equal) Divide and Conquer Recerrence log b n − 1 � n c . n log b a + c . n log b a + g ( n ) a j . f � � Case-3: We had, T ( n ) = = b j j =0 If f ( n ) ≥ d . n log b a + ǫ for some constant d , ǫ > 0, and a . f ( n b ) ≤ k . f ( n ) for some constant k < 1 and for all sufficiently large n ≥ b , then � n � n � n ≤ k ≤ k � n � k � 2 � � � � a . f ≤ k . f ( n ) ⇒ f a . f ( n ) ⇒ f a . f ≤ . f ( n ) b 2 b b b a f ( n b j ) ≤ ( k a ) j . f ( n ). Hence, Iterating in this manner, we get, log b n − 1 log b n − 1 log b n − 1 � n a j . ( k � a j . f � a ) j . f ( n ) � k j . f ( n ) � g ( n ) = ≤ = b j j =0 j =0 j =0 ∞ 1 � � � k j ≤ f ( n ) . = . f ( n ) 1 − k j =0 Since k < 1 is a constant, for exact powers of b we can conclude that, D 1 . f ( n ) ≤ g ( n ) ≤ D 2 . f ( n ) [ for some constants D 1 , D 2 > 0] Therefore, [for some constants C 1 , C 2 > 0] c . n log b a + D 1 . f ( n ) ≤ ≤ c . n log b a + D 2 . f ( n ) T ( n ) [ with f ( n ) ≥ d . n log b a + ǫ ] ⇒ C 1 . f ( n ) ≤ T ( n ) ≤ C 2 . f ( n ) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 24 / 34

  80. Master Theorem Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a non-negative function defined on exact powers of b . We define T ( n ) on exact powers of b by the following recurrence, � n n = b i > 1 � � a . T + f ( n ) [ where i ∈ Z + ] T ( n ) = b c , n = 1 Then, T ( n ) follows the following inequalities: Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

  81. Master Theorem Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a non-negative function defined on exact powers of b . We define T ( n ) on exact powers of b by the following recurrence, � n n = b i > 1 � � a . T + f ( n ) [ where i ∈ Z + ] T ( n ) = b c , n = 1 Then, T ( n ) follows the following inequalities: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then T ( n ) ≤ C . n log b a , for some 1 constant C > 0. Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

  82. Master Theorem Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a non-negative function defined on exact powers of b . We define T ( n ) on exact powers of b by the following recurrence, � n n = b i > 1 � � a . T + f ( n ) [ where i ∈ Z + ] T ( n ) = b c , n = 1 Then, T ( n ) follows the following inequalities: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then T ( n ) ≤ C . n log b a , for some 1 constant C > 0. If f ( n ) = O ( n log b a − ǫ ) for some constant ǫ > 0 , then T ( n ) = O ( n log b a ) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

  83. Master Theorem Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a non-negative function defined on exact powers of b . We define T ( n ) on exact powers of b by the following recurrence, � n n = b i > 1 � � a . T + f ( n ) [ where i ∈ Z + ] T ( n ) = b c , n = 1 Then, T ( n ) follows the following inequalities: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then T ( n ) ≤ C . n log b a , for some 1 constant C > 0. If f ( n ) = O ( n log b a − ǫ ) for some constant ǫ > 0 , then T ( n ) = O ( n log b a ) If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 , ǫ > 0, then 2 C 1 . n log b a . log 2 n ≤ T ( n ) ≤ C 2 . n log b a . log 2 n , for some constant C 1 , C 2 > 0. Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

  84. Master Theorem Let a ≥ 1, b > 1 and c be constants, and f ( n ) be a non-negative function defined on exact powers of b . We define T ( n ) on exact powers of b by the following recurrence, � n n = b i > 1 � � a . T + f ( n ) [ where i ∈ Z + ] T ( n ) = b c , n = 1 Then, T ( n ) follows the following inequalities: If f ( n ) ≤ d . n log b a − ǫ for some constant d , ǫ > 0, then T ( n ) ≤ C . n log b a , for some 1 constant C > 0. If f ( n ) = O ( n log b a − ǫ ) for some constant ǫ > 0 , then T ( n ) = O ( n log b a ) If d 1 . n log b a ≤ f ( n ) ≤ d 2 . n log b a for some constant d 1 , d 2 , ǫ > 0, then 2 C 1 . n log b a . log 2 n ≤ T ( n ) ≤ C 2 . n log b a . log 2 n , for some constant C 1 , C 2 > 0. If f ( n ) = Θ( n log b a ) , then T ( n ) = Θ( n log b a . log 2 n ) Aritra Hazra (CSE, IITKGP) CS21001 : Discrete Structures Autumn 2020 25 / 34

Recommend


More recommend