Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Algorithms – I (CS29003/203) Autumn 2022, IIT Kharagpur Divide and Conquer, Recursion Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Divide and Conquer • General techniques • Merge sort • Recursion Tree Method • Master Theorem Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and COnquer 2 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Divide and Conquer • Divide and conquer involves three steps • Divide a problem into pieces (smaller instances of same problem) • E.g., divide into halves • Conquer the subproblems by solving them recursively • Can just call the same algorithm on the subproblems (recursively solving) • Base case: when n=1 (or is small) • Combine solutions to pieces to get answer to original problem • Combining results from recursive calls. • Usually the hardest part in the algorithm design Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 3 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort 8 3 2 9 5 1 7 4 8 3 2 9 8 3 8 5 1 2 9 3 3 8 5 1 7 4 2 9 2 9 2 3 8 9 7 4 1 5 1 5 7 4 4 7 1 4 5 7 1 2 3 4 5 7 8 9 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 4 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort • Dividing is easy. The key operation, though, is merge. • How to do merging of two sorted arrays to produce a merged sorted array? 2 5 9 Aug 10,11,12,17, 2022 3 4 8 CS21003/CS21203 / Algorithms - I | Divide and Conquer 5 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort Pseudocode Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 6 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort – In What Order? 8 3 2 9 5 1 7 4 8 3 2 9 8 3 8 5 1 2 9 3 3 8 5 1 7 4 2 9 2 9 2 3 8 9 7 4 1 5 1 5 7 4 4 7 1 4 5 7 1 2 3 4 5 7 8 9 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 7 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort – Runtime Analysis Total number of elements in two arrays is π1 + π2 Θ 1 [Constant time] Θ π1 Θ π2 Θ 1 Overall runtime of “MERGE” subroutine is Θ(total number of elements after merging) Θ π1 + π2 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 8 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort – Runtime Analysis π Θ 1 Θ 1 π π πΰ΅ 2 π π π πΰ΅ 2 π 2 ∗ πΰ΅2 4 ∗ πΰ΅4 π πΰ΅4 π π πΰ΅4 π π πΰ΅4 π π πΰ΅4 π π = π π = π π = ππ = π π = ππ = π π = ππ = π Θ π 8 ∗ πΰ΅8 πΰ΅ πΰ΅ 4 4 πΰ΅ πΰ΅ 4 4 πΰ΅ πΰ΅ 4 4 πΰ΅ πΰ΅ 4 4 • How do we count the recursive calls? • Recursive calls belong to the next level Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 9 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Merge Sort – Runtime Analysis • What are the number of levels in mergesort? • log π • There are log π levels and at each level the number of operations is Θ(π) • The runtime of mergesort is Θ(π log π) • Next we will learn how to write an equation that represents the running time of a divide and conquer algorithm. Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 10 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Recurrence Relation • Let π π be the running time of the algorithm when the input size is π • Base case takes constant time Base case Divide time = const π • π π = ΰ΅π + Θ π + 2π π 2 π=1 π>1 π Conquer time = π(2 ) each Combine time = Θ(π) π • π π = ΰ΅2π π 2 +Θ π π=1 π>1 • Recurrence relation: Represents the running time of a recursive algorithm Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 11 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree π • π π = ΰ΅2π π 2 +Θ π π=1 π>1 • We can write Θ π as some const.*π • Does the base case need to be always for π = 1? • General form: • π π = α π Aug 10,11,12,17, 2022 2π π = π0 π 2 + ππ π > π0 CS21003/CS21203 / Algorithms - I | Divide and Conquer 12 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree • • • • π π=1 π π π = α 2π + ππ π > 1 2 Left hand side is π(π). This is a node It expands to 2, π(πΤ2) nodes and one ππ node The convention is to replace π(π) with these nodes And continue ππ π(π) π(π) π(πΰ΅2) π(πΰ΅4) Aug 10,11,12,17, 2022 π(πΰ΅2) π(πΰ΅4) ππΰ΅ 2 πΰ΅ ) π(ππ 2 π(πΰ΅4) π(πΰ΅4) CS21003/CS21203 / Algorithms - I | Divide and Conquer ππΰ΅ 2 13 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree π π=1 π π π = α 2π + ππ π > 1 2 • • • • Left hand side is π(π). This is a node It expands to 2, π(πΤ2) nodes and one ππ node The convention is to replace π(π) with these nodes And continue ππ The tree ends when the base case is reached π(π) ππΰ΅ 2 π(πΰ΅4) How many π ? Base Aug 10,11,12,17, 2022 π(πΰ΅4) π(πΰ΅4) ππΰ΅ 2 π(πΰ΅4) π π π CS21003/CS21203 / Algorithms - I | Divide and Conquer π 14 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree ππ ππΰ΅ 2 ππΰ΅ 2 π π=1 π π = α2π π + ππ π > 1 2 ππΰ΅ 4 Base ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 π π π π • Total runtime comes from counting number of operations from the internal nodes and counting them from base nodes • Lets get introduced to trees and some related terminologies as we need • More will come later Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 15 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree • Graphs are sets of nodes and edges • Trees are a form of a graph with some specialties • Trees have direction (parent/child relationships) and don't contain cycles • Some terminologies Root A D Internal C B E F G Leaf • In a Binary Tree, a node can have at most two children Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 16 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Some Terminologies Parent A Right child Left child B D C Siblings G F E Root A Right subtree Left subtree C B D Aug 10,11,12,17, 2022 E F G CS21003/CS21203 / Algorithms - I | Divide and Conquer 17 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Some Terminologies A C B D D • • • • • E D G F E E D E D E Level 0 Max # of Nodes 1 = 20 Level 1 2 = 21 Level 2 4 = 22 Level 3 8 = 23 Number of nodes at level π = 2π Number of leaves πΏ = Number of nodes at level π» ππ 2π» [π» is height] Naturally height π» = log 2 πΏ Note: Number of Levels = Height (H) + 1 π π»+1 Total number of nodes, π = σπ» −1 π=0 2 = 2 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 18 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Some Terminologies A C B D D E D E D For general `k’-array Tree G F E E D E • Number of nodes at level π = π π • Number of leaves πΏ = Number of nodes at level π» = π π» [π» is height] • Naturally height π» = log π πΏ π • Total number of nodes, π = σπ» π=0 π = Aug 10,11,12,17, 2022 π π»+1 −1 π−1 CS21003/CS21203 / Algorithms - I | Divide and Conquer 19 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree Level 0 ππ ππΰ΅ 2 ππΰ΅ 2 π π=1 π π = α2π π + ππ π > 1 2 ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 Level 1 ππΰ΅ 4 Level π» − 1 Base π π π π Level π» • To get the total runtime, first we need to get the height of the tree π» = log 2 π π» log • Number of leaves πΏ = 2 = 2 2 π = π • Base cost = π × πΏ = ππ = Θ(π) Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 20 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree ππ Level 0 ππ 2 ∗ ππΰ΅2 = ππ ππΰ΅ 2 ππΰ΅ 2 4 ∗ ππΰ΅4 = ππ ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 Level 1 ππΰ΅ 4 Level π» − 1 Base • • • • • π π π π Level π» What about the internal costs? We need to sum the costs at each internal node Since the costs at each level is same we can multiply by height Internal cost = ππ ∗ π» = ππ log 2 π = Θ (π log2 π) Total cost =Θ π + π π log2 π = π π log2 π Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 21 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree • Lets take another example π π=1 π π π = α 4π + ππ π > 1 2 • Does it make sense? • The general form of the recurrence can be T n = ππ • • • π + π(π) π Where, π is number of subproblems (branching factor) π is the size of each subproblem (division ratio) π(π) total time for divide and combine operations • What are these values for binary search? T n =π • with base case time - constant Aug 10,11,12,17, 2022 π +π 2 CS21003/CS21203 / Algorithms - I | Divide and Conquer 22 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree π π=1 π π = α4π π + ππ π > 1 2 cπ πππ π( ΰ΅2ΰ΅2) ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 Base πππ π( ΰ΅2ΰ΅2) πππΰ΅ΰ΅ ) π( 22 πΰ΅ΰ΅ ) π(ππ 22 ππΰ΅ 4 π π π π • What will be the number of leaves? More than or less than previous? • What about the height? More than or less than previous? • Height π» = log π π = log 2 π • Number of leaves πΏ = ππ» = 4log2 π = π2 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 23 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree π π=1 π π = α4π π + ππ π > 1 2 cπ ππΰ΅ 2 ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 2 ππΰ΅ 2 ππ Level 0 ππΰ΅ 2 ππΰ΅ 4 ππ = 2ππ 2 Level 1 4 Level 2 16 ππ = 4ππ 4 Level π» − 1 Base π π π π Level π» • So base cost, = π × πΏ = ππ2 = π(π2 ) • Now the internal cost • So, number of operations at level π is 2π ππ π π» log2 π • Internal cost = σπ»−1 − 1 = ππ π − 1 = π=0 2 ππ = ππ 2 − 1 = ππ 2 2 ππ − ππ Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 24 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Recursion Tree π π=1 π π = α4π π + ππ π > 1 2 cπ ππΰ΅ 2 ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 4 ππΰ΅ 2 ππΰ΅ 2 ππ Level 0 ππΰ΅ 2 ππΰ΅ 4 ππ = 2ππ 2 Level 1 4 Level 2 16 ππ = 4ππ 4 Level π» − 1 Base π π π π Level π» • So base cost, = π × πΏ = ππ2 = π(π2 ) • Internal cost = ππ2 − ππ • Total cost = ππ2 + ππ2 − ππ = π(π2 ) Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 25 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Master Theorem • Review of the general procedure π • General form: • π π = α • • • • π ππ π π( ) π π ≤ π0 π π + π(π) π(π) π π( ) π π π( ) π π π( ) π π > π0 where π is base case runtime π is number of subproblems (branching factor) Base π π is the size of each subproblem (division ratio) π(π) total time for divide and combine operations π π • Compute Height π» = log π π • Compute number of leaves πΏ = ππ» = πlogπ π = πlogπ π • Compute base cost (Generally - πΏ × π) π • Compute internal cost π π + ππ + π2 π π • Sum base and internal cost Aug 10,11,12,17, 2022 π π2 +β― CS21003/CS21203 / Algorithms - I | Divide and Conquer 26 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Master Theorem • Master theorem provides a consolidated set of rules • It depends on the relation between the number of leaves (πΏ = πlogπ π ) and the function π(π) • Case I: If πΏ is polynomially larger i.e., π π = π πlogπ π−β , where β is some constant > 0, then π π = Θ(πlogπ π ) • β is the difference in the polynomial degree between πΏ and π(π) • If πΏ = π2 and π π = π, then β=? ο 1 3 • Similarly, if πΏ = π2 and π π = π2 , then β=? ο 0.5 • Another way of thinking this is – the ratio between πΏ and π(π) must be at least a polynomial π2 πΏ • Take πΏ = π2 and π π = , Then = log π and log π is less log π π(π) than polynomial Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 27 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Solving Recurrences – Master Theorem • Case I: If πΏ is polynomially larger i.e., π π = π πlogπ π−β , where β is some constant > 0, then π π = Θ(πlogπ π ) • β is the difference in the polynomial degree between πΏ and π(π) • Case II: If π π and πΏ are asymptotically same, i.e., π π = Θ πlogπ π then π π = Θ πlogπ π log π = Θ(π(π) log π) • Case III: If π(π) is polynomially larger i.e., π π = Ω πlogπ π+β , π then π π = Θ(π(π)) if ππ( ) ≤ ππ(π) for π < 1 Regularity Condition π π + ππ 2 π = 2, π = 2 πΏ = πlogπ π = π π π = ππ Case II: π π = Θ(π log π) π π = 2π Aug 10,11,12,17, 2022 π + ππ 2 π = 4, π = 2 πΏ = πlogπ π = π2 π π = ππ π π = 4π Case I: π π = Θ πlogπ π = Θ(π2 ) CS21003/CS21203 / Algorithms - I | Divide and Conquer 28 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort • Quicksort is a sorting algorithm which puts elements in the right places one by one • What is a “right place”? • Is there any element which is at the right place? 3 7 9 5 15 11 4 7 9 5 15 11 17 4 7 5 9 15 11 12 Aug 10,11,12,17, 2022 4 CS21003/CS21203 / Algorithms - I | Divide and Conquer 29 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort • Lets walkthrough an example to understand how quicksort works 7 6 10 5 9 2 1 15 7 • The crucial step in quicksort is partioning (divide into two parts such that left of pivot is less and right of pivot id more) Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 30 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 31 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis Θ 1 Θ π Overall runtime of “PARTITION” subroutine is Θ(π) Θ 1 Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 32 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis Θ π Hard to tell about these. • Lets take two extreme cases Partitions are perfectly balanced everytime • Number of levels = log 2 π • Recurrence relation π + ππ 2 • Same as mergesort – Θ(π log π) π π = 2π • Aug 10,11,12,17, 2022 However, this is the best case scenario CS21003/CS21203 / Algorithms - I | Divide and Conquer 33 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis Θ π Hard to tell about these. • Lets take two extreme cases Partitions are most unbalanced everytime • Number of levels = Θ(π) • Recurrence relation πΆπππ π‘. π π = π π − 1 + π(0) + ππ • Lets try to solve this recusrsive eqn. Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 34 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis • Base case = ππππ π‘. cπ • Internal cost at level π is π(π − π) C(π − 1) • Total internal cost π 2+ 3+ β―π C(π − 2) • Number of levels = Θ(π) 2c • Recurrence relation Base case πΆπππ π‘. π π = π π − 1 + π 0 + ππ • Same as insertion sort – Θ π2 • Aug 10,11,12,17, 2022 This is the worst case scenario CS21003/CS21203 / Algorithms - I | Divide and Conquer 35 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis • What should the input look like for the best case? • The medians are chosen as pivots always • What should the input look like for the worst case? • The input array is already sorted • Probability of getting an already sorted array is 1 π! • However, the problem can be tackled if instead of always taking the first element as pivot, we take any element as pivot in random • The average-case time complexity of the randomized version of quicksort is Θ(π log π) • The proof of this involves a more complicated recurrence than we have seen so far. Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 36 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis • We will follow “Introduction to Algorithms and Data Structures” by M. J. Dinneen, G. Gimelfarb, M. C. Wilson for the proof • Let π(π) denote the average-case running time for size π list • In the first step, the time taken to compare all the elements with the pivot is ππ • Let 0 ≤ π ≤ π − 1 is the final position of the pivot. So, the two sublists are of size π and π − π − 1 respectively π π π−π−1 • The recurrence relation for this π is π π = π π + π π − 1 − π + ππ • Small detail: Last term should be Θ π , as it depends on the exact implementaiton (e.g., whether comparison starts from pivot or one element after, etc.) Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 37 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis π • • • • • π π−π−1 The recurrence relation for this π is π π = π π + π π − 1 − π + ππ The final pivot position π can be any of the π positions with equal probability So, the value of π(π) to be π 0 + π π − 1 + ππ (i.e., π = 0) has 1 probability π Similarly, the value of π(π) to be π 1 + π π − 2 + ππ (i.e., π = 1) 1 has probability and so on π So the expected value of π(π) is given by, π−1 π−1 1 1 π π = ΰ· π π + π π − 1 − π + ππ = ΰ· π π + π π − 1 − π π π π=0 Aug 10,11,12,17, 2022 + ππ π=0 CS21003/CS21203 / Algorithms - I | Divide and Conquer 38 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis 1 • π π = σπ−1 (π π + π π − 1 − π ) + ππ π π=0 • π 0 + π π − 1 = π π − 1 + π 0 πππ π = 0 πππ π = π − 1 • π 1 + π π − 2 = π π − 2 + π 1 πππ π = 1 πππ π = π − 2 and so on π π π • π π = π−π−1 1 π−1 σ (π π π=0 π−π−1 π 2 π π + π π − 1 − π ) + ππ = σπ−1 π=0 π π + ππ 2 • Let us rewrite this as ππ π = 2 σπ−1 π=0 π π + ππ Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 39 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis 2 • ππ π = 2 σπ−1 π=0 π π + ππ • Replace π by π − 1 and subtract from the above • ππ π = 2 π 0 + π 1 + β― π π − 2 + π π − 1 + ππ2 π − 1 π π − 1 = 2 π 0 + π 1 + β― + π π − 3 + π(π − 2) + π π − 1 • ππ π − π − 1 π π − 1 = 2π π − 1 + π π2 − π − 1 • ππ π = π + 1 π π − 1 + π(2π − 1) • Divide everything by π(π + 1) • π(π) π+1 = π(π−1) + π π 2π−1 π(π+1) = π(π−1) + π π 2 2 3 1 − π+1 π • Last term uses partial fraction decomposition Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 40 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis π(π) π+1 = π(π−1) + π π • π(1) 2 = π(0) 3π + 1 2 − π 1 • π(2) 3 = π(1) 3π + 2 3 − π 2 • π(3) 4 = π(2) 3π + 3 4 − π 3 • π(π) π+1 = π(π−1) 3π π + − π π+1 π • π(π) π+1 = π(0) + 1 • 2π−1 π(π+1) = • Lets put π = 1, 2,3, … π Aug 10,11,12,17, 2022 3π π(π−1) + π π 1 1 1 1 + + +β―+ 2 3 4 π+1 3 1 − π+1 π −π 1 1 1 + + + 1 2 3 CS21003/CS21203 / Algorithms - I | Divide and Conquer β―+ 1 π 41 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in Quicksort – Runtime Analysis • π(π) π+1 = π(0) + 1 • π(π) π+1 = π(0) −π 1 1 2 3π + 1 3 1 1 1 1 + + +β―+ 2 3 4 π+1 3π π+1 + 2π 1 1 + 2 3 1 4 −π + + β―+ 1 π 1 1 1 + + + 1 2 3 =π−π+ β―+ 3π + π+1 1 π 2π(π»π − 1) 1 π • π»π = 1 + + + β― + ; π = π(0) • π π = π − π π + 1 + 3π + 2π π + 1 π»π − 1 • = π + π − 3π π + 2ππ»π + 2πππ»π • Using a result π»π = Θ(log π), we can write • π π = π + π − 3π π + 2πΘ log π + 2πΘ π log π = Θ(π log π) • π»π = Θ log π -> This will shown to be true for a special case, next Aug 10,11,12,17, 2022 CS21003/CS21203 / Algorithms - I | Divide and Conquer 42 Computer Science and Engineering| Indian Institute of Technology Kharagpur cse.iitkgp.ac.in To be continued …