Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cinda Heeren / Geoffrey Tien

Similar presentations


Presentation on theme: "Cinda Heeren / Geoffrey Tien"β€” Presentation transcript:

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


Download ppt "Cinda Heeren / Geoffrey Tien"

Similar presentations


Ads by Google