Cinda Heeren / Geoffrey Tien Recurrence relations March 02, 2018 Cinda Heeren / Geoffrey Tien
Analyzing recursive functions In a previous adventure in CPSC 221: Geoff's hand-wavy intuition for running time of recursive functions e.g. Geoff says: Merge sort running time = number of recursion levels times work at each level Not a solid proof, Geoff gets: Recall: recursive functions are defined in terms of themselves The running time of some particular recursive call can also be defined in terms of the running time of its own recursive call(s) e.g 𝑇 𝑛 =something+𝑇 a smaller 𝑛 + … Actually, recursion tree analysis is a valid technique, but let's learn about a more precise method March 02, 2018 Cinda Heeren / Geoffrey Tien
Recurrence relations Analysis 𝑇 𝑛 ∈𝑂 𝑛 Example: Recursive max in an array double arrMax(double arr[], int size, int start) { if (start == sz – 1) return arr[start]; else return max( arr[start], arrMax(arr, size, start + 1) ); } 𝑇 1 ≤𝑏 𝑇 𝑛 ≤𝑐+𝑇 𝑛−1 Analysis 𝑇 𝑛 ≤𝑐+𝑐+𝑇 𝑛−2 (by substitution) 𝑇 𝑛 ≤𝑐+𝑐+𝑐+𝑇 𝑛−3 (by substitution, again) 𝑇 𝑛 ≤𝑘∙𝑐+𝑇 𝑛−𝑘 (extrapolating, 0<𝑘≤𝑛) 𝑇 𝑛 ≤ 𝑛−1 ∙𝑐+𝑇 1 = 𝑛−1 ∙𝑐+𝑏 for 𝑘=𝑛−1 𝑇 𝑛 ∈𝑂 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Merge sort analysis Now with more math! Merge sort algorithm Split list in half, sort first half, sort second half, merge together 𝑇 1 ≤𝑏 𝑇 𝑛 ≤2∙𝑇 𝑛/2 +𝑐∙𝑛 Analysis ≤2 2∙𝑇 𝑛/4 +𝑐 𝑛/2 +𝑐𝑛 =4∙𝑇 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 ≤4 2∙𝑇 𝑛/8 +𝑐 𝑛/4 +𝑐∙𝑛+𝑐∙𝑛 =8∙𝑇 𝑛/8 +𝑐∙𝑛+𝑐∙𝑛+𝑐∙𝑛 ≤ 2 𝑘 ∙𝑇 𝑛/ 2 𝑘 +𝑘∙𝑐∙𝑛 extrapolating, 1<𝑘≤𝑛 ≤𝑛∙𝑇 1 +𝑐∙𝑛 log 𝑛 for 2 𝑘 =𝑛, or 𝑘= log 2 𝑛 𝑇 𝑛 ∈𝑂 𝑛 log 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien A problem with trees... int fun(Node* curr) { if (curr != NULL) { int ret1 = fun(curr->left); int ret2 = fun(curr->right); return 1 + ret1 + ret2; } else return 0; Assume this function is only used on perfect binary trees! This function performs: 1 recursive call to the left subtree 1 recursive call to the right subtree 1 addition (and return) 𝑇 𝑛 =2∙𝑇 𝑛 2 +𝑐 𝑇 1 =𝑏 March 02, 2018 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien A fun problem 𝑇 𝑛 =2∙𝑇 𝑛 2 +𝑐 𝑇 𝑛 =2∙ 2∙𝑇 𝑛 4 +𝑐 +𝑐 𝑇 𝑛 =4∙𝑇 𝑛 4 +2𝑐+𝑐 𝑇 𝑛 =4∙ 2∙𝑇 𝑛 8 +𝑐 +2𝑐+𝑐 𝑇 𝑛 =8∙𝑇 𝑛 8 +4𝑐+2𝑐+𝑐 𝑇 𝑛 = 2 𝑘 ∙𝑇 𝑛 2 𝑘 +𝑐 𝑖=0 𝑘−1 2 𝑖 𝑇 𝑛 = 2 log 2 𝑛 ∙𝑇 1 +𝑐∙ 𝑖=0 log 2 𝑛 −1 2 𝑖 𝑇 𝑛 =𝑛∙𝑏+𝑐∙ 2 log 2 𝑛 −1 =𝑛∙𝑏+𝑐∙ 𝑛−1 ∈𝑂 𝑛 𝑇 𝑛 2 =2∙𝑇 𝑛 4 +𝑐 𝑇 𝑛 4 =2∙𝑇 𝑛 8 +𝑐 𝑛 2 𝑘 =1 𝑘= log 2 𝑛 March 02, 2018 Cinda Heeren / Geoffrey Tien
In the numeric domain 𝑇 𝑛 =2∙𝑇 𝑛−1 +1, 𝑇 0 =0 Solving for exact closed forms 𝑇 𝑛 =2∙𝑇 𝑛−1 +1, 𝑇 0 =0 March 02, 2018 Cinda Heeren / Geoffrey Tien
Cinda Heeren / Geoffrey Tien Exercise We know that binary search on an ordered array has a running time 𝑂 log 2 𝑛 . Do a recurrence analysis to show this Start by writing the base case and general recurrence relation Suppose we implemented Merge sort, to split the array into 3 (roughly) equally-sized subarrays, and the Merge function repeatedly copies in the smallest of the three values in each subarray index Perform a recurrence analysis to see how the (asymptotic) running time will be affected. March 02, 2018 Cinda Heeren / Geoffrey Tien
Readings for this lesson Epp Chapter 5.6 – 5.7 (Recurrence relations) Next class: Carrano & Henry, Chapter 18.4 (Hash tables) March 02, 2018 Cinda Heeren / Geoffrey Tien