Discrete Mathematics Advanced Counting Technique (Recurrence) M. Affan Aleem 1 Recursive Definition To define a function with the set of nonnegative integers as its domain, 1. Specify the value of the function at zero 2. Give a rule for finding its value as an integer from its values at smaller integers Such a definition is called recursive or inductive definition 2 Recursive Definition Example: f(0) = 3 f(n+1) = 2*f(n) + 3 Find f(1), f(2), f(3) 3 Recursive Definition cont… Example: Give an inductive definition of the factorial function F(n) = n! F(0) = F(1) = 1 F(n+1) = (n+1)F(n) When F(0) is the only value of the function that occurs, no more reductions 4 Definition A recurrence relation for the sequence {an} is an equation that expresses an in terms of one or more of the previous terms of the sequence, namely, a0, a1, …,an-1, for all integers n with n n0 is a nonnegative integer 5 Definition A sequence is called a solution of a recurrence relation if its terms satisfy the recurrence relation. 6 Example#1 Let {an} be a sequence that satisfies the recurrence relation an = an-1 – an-2 for n = 2,3,4… and suppose that a0 = 3 and a1 = 5 what are the a2 and a3. 7 Initial Condition The initial conditions for a sequence specify the terms that precede the first term where the recurrence relation takes effect. 8 Modeling with Recurrence Relations The recurrence relations to model a wide variety of problems, such as finding compound interest, counting rabbits on a island, determining the number of moves in the Tower of Hanoi puzzle, and counting bit strings with certain properties 9 10 Examples Compound interest suppose that a person deposits $10,000 in a saving account at a bank yielding 11% per year with interest compounded annually. How much will be in the account after 30 years. 11 Solution • To solve this problem, let Pn denote the amount in the account after n years. • Since the amount in the account after n years equal the amount in the account after n-1 year plus interest for the nth year, we see that the sequence {Pn} satisfies the recurrence relation. 12 Solution contd… pn pn 1 0.11 pn 1 (1.11) pn 1 • The initial condition is p0 = 10,000 • We can use an iterative approach to find a formula for Pn note that 13 Tower of Hanoi (Problem Statement) 14 Tower of Hanoi-Problem Statement • There three pegs, some disks are in first peg in descending order from bottom to up • Transfer the all disk from first peg to third peg with following conditions: • Only one disk can be moved at a time, from one peg to another, such that it never is placed on a disk of smaller diameter. 15 Tower of Hanoi (Step by Step Solution) 16 Tower of Hanoi- one disk 1-moves 17 Tower of Hanoi- two disk Note: that we perform the solution to the one-disk Tower of Hanoi twice (once in the top row, and once in the bottom row). Between rows, we move the bottom disk. Thus, we require 2· 1+1 = 3 Moves 18 Tower of Hanoi- three disk Note that we perform the solution to the two-disk Tower of Hanoi twice (once in the top row, and once in the bottom row). Between rows we move the bottom disk. Thus, we require 2(2 · 1+ 1) +1= 7 Moves 19 Tower of Hanoi: What we know so far Let M(n) denote the minimum number of legal moves required to complete a tower of Hanoi puzzle that has n disks n 1 2 M(n) 1 3 3 7 20 Tower of Hanoi: What we know so far Following the pattern, for n = 4 we need to solve the three-disk puzzle twice, plus one more operation to move the largest disk. Thus, M(4) =2 · M(3) +1 = 2· 7+1= 15. 21 Tower of Hanoi: What we know so far Similarly, for n = 5 disks, we expect that we will need to perform M(5) = 2 • M(4) +1 = 2• 15+1= 31 22 Tower of Hanoi: n-disk Let M(n) denote the minimum number of legal moves required to complete a tower of Hanoi puzzle that has n disks. We thus obtain the recursion relation, M(n) = 2M(n - 1) + 1 23 Tower of Hanoi: Solution With the solution for a single disk M(1) = 1 the recursion relation M(n) = 2M(n - 1) + 1 n = 2, 3,4 … (number of disk) 24 Tower of Hanoi: Solution Make a series of different values of n 1, 3, 7,15, 31…nth term Each term can be generated by Tn = 2n-1 25 Tower of Hanoi: practical Consequence The practical difficulty with exponential algorithms is that they can quickly grow out of hand. (With each additional disk, the minimum number of operations essentially doubles.) N.B., 1 century ~ 4.5 × 109 seconds. (The age of the universe is 4 × 1017 seconds.) 26 n 2n - 1 n 2n - 1 n 2n - 1 8 255 19 524287 30 1073741823 9 511 20 1048575 31 2147483647 10 1027 21 2097151 32 4294967295 11 2047 22 4194303 33 8589934591 12 4095 23 8388607 34 17179869183 13 8191 24 16777215 35 34359738367 14 16383 25 33554431 36 68719476735 15 32767 26 67108863 37 137438953471 16 65635 27 134217727 38 274877906943 17 131071 28 268435455 39 549755813887 18 262143 29 536870911 64 18446744073709551615 27