V. Adamchi Recurrences Victor Adamchi Fall of 00 Plan Multiple roots. More on multiple roots. Inhomogeneous equations 3. Divide-and-conquer recurrences In the previous lecture we have showed that if the characteristic equation has a multiple root then both a n n and a n n n are solutions. Today we will prove this directly. Consider the second order recurrence equation The characteristic equation has two identical roots a n a n a n 0 4, 4 if and only if. It follows. To prove that 4 a n n n is the solution we substitute this into the original recurrence Divide this by n n n n n n n n n n 0
V. Adamchi -7: Concepts of Mathematics and then collect terms with respect to n n 0 The first term is zero because is the roots of the characteristic equation. The second term is zero because and 4. Theorem. Let be a root of multiplicity p of the characteristic equation. Then are all solutions to the recurrence. Example. Find a general solution n, n n, n n,..., n p n a n 3 a n 3 a n a n3 The characteristic equation has a root of multiplicity 3. Therefore, is a solution of this recurrence equation. Exercise. Solve the recurrence Inhomogeneous Equations a n c c n c 3 n a n a n 7 a n 3 a n3 0 a 0, a, a 3 As an example of such recurrences we consider Fibonacci trees. This data structure is defined recursively as follows: the empty tree is a Fibonacci tree of order 0 a single node tree is a Fibonacci tree of order. a Fibonacci tree of order n has a left Fibonacci subtree of order n, and a right Fibonacci subtree of order n.
V. Adamchi 3 We want to count the number of nodes in a Fibonacci tree of order n. Let T n denote the number of nodes in a tree of order n. Then A recurrence of the form T n T n T n T 0 0, T a n a n... a n fn where all are constants and fn is a function other than the zero is called an inhomogeneous linear recurrence equation with constant coefficients. There is no a nown general method for solving such equations. We consider a one important particular case when the function fn is fn n pn wherepn is a polynomial and 0. The main idea is to transform a given inhomogeneous equation into a homogeneous one. Let us trace the idea on the Fibonacci tree recurrence. In order to cancel the right hand side, we consider the original equation along with the one obtained by replacing n n T n T n T n T n T n T n3 Next we subtract the second equation from the first T n T n T n3 0 T 0 0, T, T
V. Adamchi -7: Concepts of Mathematics This is the forth order homogeneous equation, which we can solve by the characteristic equation Solvex^3x^ 0, x x, x, x The general solution is given by The system for coefficients c has a solution After some algebra, we get which can be recognized as or n T n c c c n 3 T 0 c c c 3 0 T c c c 3 T c c c 3 c, c 3, c 3 3 0 0 T n n n n T n F n F n T n F n wherer F n is the Fibonacci number. You will see this sequence again in - when study AVL trees. n
V. Adamchi Divide-and-conquer Recurrences The divide-and-conquer algorithm consist of three steps: dividing a problem into smaller subproblems solving (recursively) each subproblem then combining solutions Suppose T n is the number of steps in the worst case needed to solve the problem of size n. Let us split a problem into a subproblems, each of which is of the input size n b where b. Observe, that the number of subproblems a is not necessarily equal to b. The total number of steps T n is obtained by all steps needed to solve smaller subproblems T nb plus the number needed to combine solutions into a final one. The following equation is called divide-and-conquer recurrence relation Here are some examples T n a T nb fn T n T n n T n 3 T n4 n T n T n3 n log n There are three main techniques to solve such recurrence equations: the iteration method the tree method the master-theorem method MergeSort Mergesort involves the following steps: Divide the array into two subarrays Sort each subarray Merge them into one (in a smart way!) Example.. divide it into two parts. sort each one 7 0 34 6 3 7 0 34 6 3
V. Adamchi -7: Concepts of Mathematics 0 7 6 3 34 3. merge into one (comparing, one by one, the paired elements from the two parts) (0 ) ( ) ( ) ( 6) ( 3) (3 7) (3 34) 0 6 7 3 34 Let T n denote the running time of the algorithm, i.e. the number of comparisons needed to sort n elements. We have the following recurrence equation for T n : T n T n n T 0 To get the feeling for the nature of the solution we consider a case when n is a power of, namely n. Then We divide both sides by and iterate it Using a bacward substitution, this leads to The finite sum is the geometric series Therefore, T T T T T T... steps... T T 0 T... T... x j x x j0
V. Adamchi 7 j j j j0 Thus or T T Since n, we finally obtain T n nlog n n