1 / 62

Chapter 7

Chapter 7. 7.1 Recurrence Relations 7.2 Solving Linear Recurrence Relations 7.3 Divide-and-Conquer Algorithms and Recurrence Relations 7.4 Generating Functions 7.5 Inclusion- Exclusion 7.6 Applications of Inclusion-Exclusion. 7.1 Recurrence Relations . Recurrence Relations

jolene
Download Presentation

Chapter 7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 • 7.1Recurrence Relations • 7.2 Solving Linear Recurrence Relations • 7.3Divide-and-Conquer Algorithms and Recurrence Relations • 7.4 Generating Functions • 7.5 Inclusion- Exclusion • 7.6Applications of Inclusion-Exclusion

  2. 7.1 Recurrence Relations • Recurrence Relations • Modeling with Recurrence Relations

  3. Recurrence Relations • The number of bacteria in a colony doubles every hour. If a colony begins with five bacteria , how many will be present in n hours? • Some of the counting problems that cannot be solved using the techniques discussed in Chapter 5 can be solved by finding relationships, called recurrence relations. • We will study variety of counting problems that can be modeled using recurrence relations. • We will develop methods in this section and in Section 7.2 for finding explicit formulae for the terms of sequences that satisfy certain types of recurrence relations.

  4. Recurrence Relations • Example 1: Let {an} be a sequence that satisfies the recurrence relation an = an-1 – an-2for n=2, 3, 4, . . . And suppose that a0= 3and a1= 5. What are a2 and a3 ? • Example 2: Determine whether the sequence {an}, where an=3n for every nonnegative integer n, is a solution of the recurrence relation an=2an-1 –an-2 for n=2, 3, 4, . . ., Answer the same question where an= 2n and where an= 5?

  5. Modeling with Recurrence Relations • Example 3: Compound Interest Suppose that person deposits $10,000 in a savings account at a bank yielding 11% per year with interest compounded annually. • How much sill be in the account after 30 year? • Solution: Pn = Pn-1 +0.11Pn-1 = (1.11)Pn-1 • P1 = (1.11)P0 • P2 = (1.11) p1=(1.11)2 p0 • P3 = (1.11) P2=(1.11)3 P0 : • Pn = (1.11) Pn-1=(1.11)n P0 • Inserting n =30 into the formula Pn =(1.11)n 10,000 shows that after 30 year the account contains • P30 = (1.11)3010,000=$228,922.97

  6. Modeling with Recurrence Relations • Example 4: Rabbits and the Fibonacci Numbers consider this problem, which was originally posed by Leonardo Pisano, also Known as Fibonacci, in the thirteenth century in his book Liber abaci. A young pair of rabbits ( one of each sex) is placed on n island. • A pair of rabbits does not breed until they are 2 months old. After they are 2 months old, each pair of rabbits produces another pair each month, as shown in Figure 1. • Find a recurrence relation for the number of pairs of rabbits on the island after n months, assuming that no rabbits ever die.

  7. Modeling with Recurrence Relations • Consequently, the sequence {fn} satisfies the recurrence relation fn =fn-1 + fn-2 ; f1=1, f2=1 FIGURE 1 Rabbits on an Island.

  8. Modeling with Recurrence Relations • Example 5: The Tower of Hanoi A popular puzzle of the late nineteenth century invented by the French mathematician Édouard Lucas, called the Tower of Hanoi, consists of three pegs mounted on a board together with disks of different sizes. Initially there disks are placed on the first peg in order of size, with the largest on the bottom ( as shown in Figure 2). • The rules of the puzzle allow disks to be moved one at a time from one peg to another as long as a disk is never placed on the top of a smaller disk. • The goal of the puzzle is to have all the disks on the second peg in order of size, with the largest on the bottom. • Let {Hn} denote the number of moves needed to solve the Tower of Hanoi problem with n disks. Set up a recurrence relation for the sequence {Hn}

  9. Modeling with Recurrence Relations FIGURE 2 The Initial Position in the Tower of Hanoi. FIGURE 3 An Intermediate Position in the Tower of Hanoi.

  10. Modeling with Recurrence Relations • Example 6: Find a recurrence relation and give initial conditions for the number of bit strings of length n that do not have two consecutive 0s. How many such bit strings are there of length five? • Example 7: Codeword Enumeration A computer system considers a string of decimal digits a valid codeword if it contains an even number of 0 digits. For instance, 1230407869 is valid, whereas 120987045608 is not valid. Let an be the number of valid n-digit codewords. Find a recurrence relation for an.

  11. Modeling with Recurrence Relations • Example 8: Find a recurrence relation for Cn, the number of ways to parenthesize the product of n+1 numbers, x0.x1.x2.. . ..xn, to specify the order of multiplication. For example ,C3=5 because there are five ways to parenthesize x0.x1.x2.x3 to determine the order of multiplication: • ((x0.x1) .x2) .x3 • (x0.(x1 .x2)).x3 • (x0.x1) .(x2 .x3) • x0.((x1 .x2) .x3 ) • x0.(x1 .(x2.x3))

  12. 7.2 Solving Linear Recurrence Relations • Some of these recurrence relations can be solved using iteration or some other ad hoc technique. • However, one important class of recurrence relations can be explicitly solved in a systematic way. These are recurrence relations that express the terms of a sequence as linear combinations of previous terms. • Definition 1: A linear homogeneous recurrence relation of degree k with constant coefficients is a recurrence relation of the form an= c1an-1 + c2an-2+ . . .+ ckan-k Where c1, c2, . . .,ck are real numbers, ck ≠ 0.

  13. Solving Linear Recurrence Relations • The recurrence relation in the definition is linear because the right-hand side is a sum of previous terms of the sequence each multiplied by a function of n. • The recurrence relation is homogeneousbecause no terms occur that are not multiples of the ajs. • The coefficients of the terms of the sequence are all constants, rather than functions that depend on n. • Thedegreeis k because an is expressed in terms of the previous k terms of the sequence. • A consequence of the second principle of mathematical induction is that a sequence satisfying the recurrence relation in the definition is uniquely determined by this recurrence relation and the k initial conditions a0 =C0, a1=C1,. . . , ak-1=Ck-1

  14. Solving Linear Recurrence Relations • Example 1: the recurrence relation Pn=(1.11)Pn-1is a linear homogeneous recurrence relation of degree one. • The recurrence relation fn=fn-1 +fn-2is a linear homogeneous recurrence relation of degree two. • The recurrence relation an=an-5is a linear homogeneous recurrence relation of degree five. • Example 2: The recurrence relation an= an-1+an-22 is not linear. The recurrence relation Hn = 2Hn-1 +1 is not homogeneous. The recurrence relation Bn =nBn-1does not have constant coefficients.

  15. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Theorem 1: let c1 and c2 be real numbers. Suppose that r2-c1r-c2=0 has two distinct roots r1and r2. • Then the sequence {an} is a solution of the recurrence relation an = c1an-1 + c2an-2 if and only is an = α1r1n + α2r2nfor n=0, 1, 2,. . .,where α1 and α2 are constants

  16. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Example 3: what is the solution of the recurrence relation of the recurrence relation an = an-1 + 2an-2 With a0= 2 and a1= 7 ? • Example 4: Find an explicit formula for the Fibonacci numbers.

  17. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Theorem 2: Let c1 and c2 be real numbers with c2 ≠0. Suppose that r2 - c1r - c2 = 0 has only one root r0. • A sequence {an} is a solution of the recurrence relation an = c1an-1 + c2an-2 if and only if an= α1r0n+ α2nr0n for n=0, 1, 2,. . .,where α1 and α2 are constants. • Example 5: What is the solution of the recurrence relation an = 6 an-1-9 an-2,with a0= 1 and a1= 6?

  18. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Theorem 3: Let c1 , c2 ,. . ., ckbe real numbers. Suppose that the characteristic equation rk- c1rk-1 - . . . - ck = 0 has k distinct roots r1, r2,. . .,rk. Then a sequence {an} is a solution of the recurrence relation an = c1an-1 + c2an-2 +. . . + ckan-k if and only is an =α1r1n +α2r2n +. . .+ αkrkn for n=0, 1, 2,. . .,where α1,α2 , . . ., αk are constants.

  19. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Example 6: Find the solution to the recurrence relation an = 6an-1 - 11an-2 + 6an-3 With the initial conditions a0=2 and a1=5, and a2=15.

  20. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Theorem 4: Let c1 , c2 ,. . ., ck be real numbers. Suppose that the characteristic equation rk-c1rk-1-. . .- ck= 0 has t distinct roots r1, r2,. . .,rt with multiplicities m1, m2,. . .,mt , respectively, so that mi ≧ 1 for i= 1, 2,. . ., t and m1+ m2+. . .+mt =k. Then a sequence {an} is a solution of the recurrence relation an=c1an-1+c2an-2 +. . . +ckan-k if and only if an= ( α1 , 0+ α1, 1n +. . .+ α1, m1-1nm1-1) r1n + ( α2 , 0+ α2 , 1n +. . .+ α2, m2-1nm2-1) r2n + … +( αt , 0 + αt , 1n +. . .+ αt, mt-1nmt-1) rtn for n=0, 1, 2,. . .,where α1, α2 , . . ., αk are constants

  21. Solving Linear Homogeneous Recurrence Relations with Constant Coefficients • Example 7: Suppose that the roots of the characteristic equation of a linear homogeneous recurrence relation are 2, 2, 2, 5, 5, and 9 (that is, there are three roots, the root 2 with multiplicity three, the root 5 with multiplicity two, and the root 9 with multiplicity one). • What is the form of the general solution? • Example 8: Find the solution to the recurrence relation an = -3an-1 - 3an-2 - an-3 With the initial conditions a0=1 and a1=-2, and a2=-1.

  22. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Linear nonhomogeneous recurrence relation with constant coefficients, an = c1an-1 + c2an-2 +. . . + ckan-k + F(n) Where c1 , c2 ,. . ., ck be real numbers and F(n)is a function not identically zero depending only on n. • The recurrence relation an = c1an-1 + c2an-2 +. . . + ckan-k is called the associated homogeneous recurrence relation.

  23. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Example 9: Each of the recurrence relations an = an-1 + 2n , an = an-1 + an-2 +n2+ n +1 , an = 3an-1 +n3n an = an-1 + an-2 + an-3 + n! , is a linear nonhomogeneous recurrence relation with constant coefficients. The associated linear homogeneous recurrence relations are an = an-1 , an = an-1 + an-2 , an = 3an-1 an = an-1 + an-2 + an-3 ,respectively.

  24. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Theorem 5: if {an(p)}is a particular solution of the nonhomogeneous linear recurrence relation with constant coefficients an = c1an-1 + c2an-2 +. . . + ckan-k + F(n) Then every solution is of the form {an(p)+ an(h)} , where {an(h)}is a solution of the associated homogeneous recurrence relation an = c1an-1 + c2an-2 +. . . + ckan-k

  25. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Example 10: Find all solutions of the recurrence relation an = 3an-1 + 2n. • What is the solution with a1= 3? • Example 11: Find all solution of the recurrence relation an =5an-1 - 6an-2 + 7n

  26. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Theorem 6: Suppose {an} satisfies the linear nonhomogeneous recurrence relation an = c1an-1 + c2an-2 +. . . + ckan-k + F(n) Where c1 , c2 ,. . ., ck be real numbers and F(n) = (b1nt+ bt-1nt-1+. . .+ b1n+ b0 )*sn Where b1 , b2 ,. . ., btand s are real numbers. When s is not a root of the characteristic equation of the associated linear homogeneous recurrence relation, there is a particular solution of the form (p1nt+ pt-1nt-1+. . .+ p1n+ p0 )*sn When s is a root of this characteristic equation and its multiplicity is m, there is a particular solution of the form nm(p1nt+ pt-1nt-1+. . .+ p1n+ p0 )*sn

  27. Linear Nonhomogeneous Recurrence Relations with Constant Coefficients • Example 12: What form does a particular solution fo the linear nonhomogeneous recurrence relation an = 6an-1 - 9an-2 + F(n) have F(n) = 3n , F(n) = n22n ,and F(n) = (n2 +1)3n ?

  28. 7.3 Divide-and-Conquer Algorithms and Recurrence Relations • If f(n) represents the number of operations required to solve the problem of size n, it follow that f satisfies the recurrence relation f(n) = af(n/b) + g(n) this is called a divide-and-conquer algorithms and recurrence relations • Example 1:Binary Search We introduced a binary search algorithm in section 3.1 .

  29. The binary search • Algorithm 3: the binary search algorithm Procedure binary search (x: integer, a1, a2, …,an: increasing integers) i :=1 { i is left endpoint of search interval} j :=n { j is right endpoint of search interval} While i < j begin m := (i + j) / 2 if x > am then i := m+1 else j := m end If x = ai then location := i else location :=0 {location is the subscript of the term equal to x, or 0 if x is not found}

  30. Divide-and-Conquer Algorithms and Recurrence Relations • Example 3: Merge Sort • Algorithm 9A Recursive Merge Sort. Procedure mergesort(L = a1, . . . , an) If n > 1 then m := n/ 2 L1 := a1, a2, . . . ,am L2 := am+1, am+2, . . ., an L := merge (mergesort(L1), mergesort(L2)) {L is now sorted into elements in nondecreasing order}

  31. Divide-and-Conquer Algorithms and Recurrence Relations • Example 4: Fast Multiplication of Integers • There are more efficient algorithms than the conventional algorithm (described in section 3.6) for multiplying integers. • Suppose that a and b are integers with binary expansions of length 2n. • Let a = (a2n-1 a2n-2 . . . a1 a0)2 ,b = (a2n-1 a2n-2 . . . a1 a0)2 • Let a = 2nA1 + A0 , b = 2nB1 + B0 A1= (a2n-1 . . . an+1 an)2 , A0= (an-1 . . . a1 a0)2, B1 = (b2n-1 . . . bn+1 bn)2 , B0= (bn-1 . . . b1 b0)2 , • the algorithm for fast multiplication of integers is based on the fact that ab can be rewritten as • ab= (22n+2n) A1 B1 + 2n(A1 - A0) (B0 - B1 ) +(2n +1)A0B0 .

  32. Divide-and-Conquer Algorithms and Recurrence Relations • This shows that if f(n) is the total number of bit operations needed to multiply two n-bit integers, thenf(2n) = 3f(n) + Cn • This reasoning behind this equation is as follows. • The three multiplications of n-bit integers are carried out using 3f(n)-bit operations.

  33. Divide-and-Conquer Algorithms and Recurrence Relations • Theorem 1: let f be an increasing function that satisfies the recurrence relation f(n)= af(n/b) + c • Whenever n is divisible by b, where a≧1 ,b is an integer greater than 1, and c is a positive real number . Then f(n) is O(nlogb a) if a >1 , O(log n) if a =1 • when n=bk,where k is a positive integer, f(n) = C1nlogba +C2= C1ak+C2 where C1= f(1)+c/(a-1) and C2 = -c/(a-1).

  34. Divide-and-Conquer Algorithms and Recurrence Relations • Example 6: Let f(n) = 5f(n/2) + 3 and f(1) =7. Find f(2k), where k is a positive integer. Also , estimate f(n) if f is an increase function. • Example 7: Estimate the number of comparisons used by a binary search. f(n) = f(n/2) + 2. • Example 8: Estimate the number of comparisons used to locate the maximum and minimum elements in a sequence using the algorithm given in example 2. f(n) = 2f(n/2) + 2.

  35. Divide-and-Conquer Algorithms and Recurrence Relations • Theorem 2: Master Theorem Let f be an increasing function that satisfies the recurrence relation f(n)= af(n/b) + cnd Whenever n=bk, where k is a positive integer, a≧1, b is an integer greater than 1, and c and d are real numbers with c positive and d nonnegative. Then f(n) is O(nd) if a < bd, O(nd log n) if a= bd , O(n logba) if a > bd.

  36. Divide-and-Conquer Algorithms and Recurrence Relations • Example 9: Complexity of Merge Sort In Example 3 we explained that the number of comparisons usedby the merge sort to sort a list of n elements is less than M(n), where M(n)=2 M(n/2) +n . By the Master Theorem (Theorem 2) we find that M(n) is O(n logn) , which agrees with the estimate found in section 4.4. • Example 10: Estimate the number of bit operations needed to multiply two n-bit integers using the fast multiplication algorithm described in Example 4.

  37. Divide-and-Conquer Algorithms and Recurrence Relations • Example 11 : Estimate the number of multiplications and additions required to multiply two n x n matrices using the matrix multiplication algorithm referred to in example 5. The function for the number of multiplications and additions is f(n)=7f(n/2)+(15/4)n2.

  38. Divide-and-Conquer Algorithms and Recurrence Relations • Example 12: The Closest-Pair Problem Consider the problem of determining the closest pair of points in a set of n points (x1,y2),. . ., (xn,yn) in the plane, where the distance between two points (xi,yi) and (xj,yj) is the usual Euclidean distance [ (xi -xj)2+ (yi - yj)2 ]0.5. • This problem arises in many applications such as determining the closest pair of airplanes in the air space at a particular altitude being managed by an air traffic controller. • How can this closest pair of points be found in an efficient way? (FIGURE 1 )

  39. Divide-and-Conquer Algorithms and Recurrence Relations FIGURE 1 The Recursive Step of the Algorithm for Solving the Closest-Pair Problem.

  40. Divide-and-Conquer Algorithms and Recurrence Relations FIGURE 2 Showing That There Are at Most Seven Other Points to Consider for Each Point in the Strip.

  41. 7.4 Generating Functions • Definition 1: The generation function for the sequence a0, a1, . . .,ak,. . . of real numbers is the infinite series • G(x) = a0+ a1x + . . .+ akxk +. . . =k=0akxk • Example 1: The generating functions for the sequences {ak} with ak=3 , ak=k+1, and ak= 2k are k=0 3xk ,k=0 (k+1)xk ,and k=0 2kxk,respectively. • Example 2: what is the generating function for the sequence 1, 1, 1, 1, 1, 1?

  42. Useful Facts About Power Series • Example 3: Let m be a positive integer. Let ak= C(m,k), for k=0, 1, 2,. . . m,. What is the generating function for the sequence a0, a1, . . .,am? • Example 4: The function f(x)=1/(1-x) is the generating function of the sequence 1, 1, 1, 1,. . ., because 1/(1-x)= 1 + x + x2+. . . . for |x|<1. • Example 5: The function f(x)=1/(1-ax) is the generating function of the sequence 1, a, a2, a3, . . . because 1/(1-ax)=1 + ax + a2x2 + . . . when |ax|<1, or equivalently, for |x|<1/|a|for |a|≠ 0

  43. Useful Facts About Power Series • Theorem 1: Let f(x)= k=0akxk and g(x)= k=0bkxk. • Then f(x)+ g(x)=k=0(ak+ bk)xk and • f (x)g(x) = k=0j=0k(aj× bk-j )xk • Example 6: Let f(x) =1/(1-x) 2. Use example 4 to find the coefficients a0, a1, . . . , in the expansion f(x)= k=0akxk.

  44. Useful Facts About Power Series • Definition 2: Let u be a real number and k a nonnegative integer then the extended binomial coefficient is defined by • Example 7: Find the values of the extended binomial coefficients and .

  45. Useful Facts About Power Series • Example 8: When the top parameter is a negative integer, the extended binomial coefficient can be expressed in terms of an ordinary binomial coefficient . note that by definition of extended binomial coefficient factoring out -1 from each term in the numerator by the commutative law for multiplication multiplying both the numerator and denominator by (n-1)! by the definition of binomial coefficients using alternative notation for binomial coefficients

  46. Useful Facts About Power Series • Theorem 2: The Extended Binomial Theorem Let x be a real number with |x|< 1 and let u be a real number. Then • Example 9: Find the generating functions for (1+x)-n and (1-x)-n , where n is a positive integer, using the extended Binomial Theorem.

  47. Counting Problems and Generating Functions

  48. Counting Problems and Generating Functions

  49. Counting Problems and Generating Functions • Example 10: Find the number of solutions of e1 +e2 +e3 =17 Where e1 ,e2 ,e3 are nonnegative integers with 2 e1  5, 3  e2  6 , 4  e3  7 • Example 11: In how many different ways can eight identicalcookies be distributed among three distinct children if each child receives at least two cookies and no more than four cookies?

  50. Counting Problems and Generating Functions • Example 12: Use generating functions to determine the number of ways to insert tokens worth $1, $2, and $5 into a vending machine to pay for an item that costs r dollars in both the cases then the order in which the tokens are inserted does not matter and when the order does matter. • Example 13: Use generating functions to find the number of k-combinations of a set with n elements. Assume that the Binomial Theorem has already been established.

More Related