Recursion Droste Effect A Deep Lust Website Introduction
Recursion (Droste Effect) © A Deep Lust Website
Introduction to Recurrences § Many efficient algorithms are based on a recursive definition § The ability to “transform” a problem into one or more smaller instances of the same problem. § Then solving these instances to find a solution to the large problem. § A recurrence is a relation describing a recursive function in terms of its values on smaller inputs. § It is widely used in the analysis of recursive algorithms § The recursive decomposition of a problem is directly reflected in its analysis via a recurrence relation: § Size and number of subproblems § Time required for the decomposition
Understanding a Relation Cost of the original problem Cost of each of the subproblems (each constant indentifies how many instances of a particular subproblem) Decomposition Cost T(n) = (a 1 x T(n 1)) + (a 2 x T(n 2))+ … + (ak x T(nk)) + f(n)
Recurrence 1
Recurrence 1
Recurrence 2
Recurrence 2
Understanding Recurrences § But where do these recurrences come from? § How can we derive one from a real recursive problem? § Let's collectively think about a problem. § Try first to come up with any algorithm (brute-force) § Then try to improve it and possibly define a recursive solution for it. § After we have a recursive solution we'll derive the recurrence for the algorithm § What is the problem? § Pancake Flipping § This problem has been posed by Harry Dweighter on American Mathematical Monthly, 1975
Flipping Pancakes (yeah, I know these are squares!) How can we pile this pancakes in the correct order? OR How many disc-moves are necessary (in the worst case) to organize the pancakes so that The largest is on the bottom, the second-largest is on the top of that, and so on?
Two-Flips method § The problem above can be solved by doing the following § Bring the largest pancake to the top of the pile with one flip § With another flip place the largest pancake at the bottom of the pile § Bring the second largest pancake to the top of the pile with one flip § With another flip place the second largest pancake at the second position from the bottom of the pile. . . and so on.
Two-Flips Recurrence § The problem above is intrinsically recursive and can be stated as § n represents the number of pancakes § If the pile has size (n) == 1 then stop § Otherwise, perform a two-flip operation on the nth pancake § Solve the flip problem for (n-1) pancakes § Recurrence relation for this problem is: T(n) = T(n-1) + n
Recurrence 3
Recurrence 3
Recurrence 4
Solution to Recurrence 4
Recurrence 5 (class exercise)
- Slides: 16