Download presentation
Presentation is loading. Please wait.
1
Cinda Heeren / Geoffrey Tien
Recurrence relations March 02, 2018 Cinda Heeren / Geoffrey Tien
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.