Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.

Similar presentations


Presentation on theme: "Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called."— Presentation transcript:

1

2 Recursive Definitions Rosen, 3.4

3 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called recursion. –Sequences {s 0,s 1,s 2, …} defined by s 0 = a and s n = 2s n-1 + b for constants a and b and n  Z+ –Sets 3  S and x+y  S if x  S and y  S –Functions Example: f(n) = 2 n, f(n) = 2f(n-1) and f(0) = 1

4 Recursively Defined Functions To define a function with the set of nonnegative integers as its domain 1.Specify the value of the function at zero (or sometimes, it first k terms). 2.Give a rule for finding its value at an integer from its values at smaller integers.

5 Examples of Recursively Defined Functions Factorial Function n!  n! = n(n-1)(n-2)….(1)  f(0) = 1, f(n) = n(f(n-1)) a n  f(0) = 1, f(n+1) = f(n)*a Fibonacci Numbers  f 0 =0, f 1 =1, f n+1 = f n + f n-1  {0,1,1,2,3,5,8,13,...}

6 Prove that the nth term in the Fibonacci sequence iswhen n  2 Induction Proof: Basic Step: Let n = 2, then f 2 = 1 = =1+0 Inductive Step: Consider k  2 and assume that the expression is true for 2  n  k. We must show that the expression is true for n = k+1, i.e., that f k+1 f k+1 = f k +f k-1 by definition by the inductive hypothesis

7 Fibonacci Proof (cont.) Since f 2 is true and [f n is true for 2  n  k  f k+1 ] is true, then f n is true for all positive integers n  2.

8 Find a closed form solution to T(1) = c 0, T(n) = 2T(n-1)+c 1 T(1) = c 0 T(2) = 2T(1) + c 1 = 2c 0 + c 1 T(3) = 2T(2)+c 1 = 2(2c 0 +c 1 )+c 1 = 4c 0 +3c 1 T(4) = 2T(3)+c 1 = 2(4c 0 +3c 1 )+c 1 = 8c 0 +7c 1 T(5) = 2T(4)+c 1 = 2(8c 0 +7c 1 )+c 1 = 16c 0 +15c 1 Guess that T(n) = 2 n-1 c 0 + (2 n-1 -1)c 1

9 Prove that T(1) = c 0, T(n) = 2T(n-1)+c 1 has closed form solution T(n) = 2 n-1 c 0 + (2 n-1 -1)c 1 Basis Step: T(1) = 2 1-1 c 0 + (2 1-1 -1)c 1 = c 0 Induction Step: Assume that T(n) = 2 n-1 c 0 + (2 n-1 -1)c 1. We must show that T(n+1) = 2 (n+1)-1 c 0 + (2 (n+1)-1 -1)c 1 = 2 n c 0 + (2 n -1)c 1. T(n+1) = 2T(n) + c 1 = 2[2 n-1 c 0 + (2 n-1 -1)c 1 ]+ c 1 = 2 n c 0 + (2 n -2)c 1 + c 1 = 2 n c 0 + 2 n c 1 - c 1 = 2 n c 0 + (2 n -1)c 1.

10 Using Matrices For finite sets we can use zero-one matrices. Elements of each set A and B must be listed in some particular (but arbitrary) order. When A=B we use the same ordering for A and B. m ij = 1 if (a i,b j )  R = 0 if (a i,b j )  R

11 Example Zero-One Matrix b1b2b3 a1 a2 a3 R = {(a1,b1), (a1,b2), (a2,b2), (a3,b2), (a3,b3)}

12 Basic Counting Rosen 4.1

13 Sum Rule If a first task can be done in n 1 ways and a second task can be done in n 2 ways, and if these tasks cannot be done at the same time (i.e., the tasks are either/or), then there are n 1 + n 2 ways to do either task. If A and B are disjoint sets then |A  B|  |A|  |B| In general if A 1, A 2...A n are disjoint sets, then |A 1  A 2 ...  A n | = |A 1 | + |A 2 | +... + |A n |

14 Product Rule Suppose that a procedure can be broken down into two tasks. If there are n 1 ways to do the first task and n 2 ways to do the second task after the first task has been done, then there are n 1 n 2 ways to do the procedure. If A and B are disjoint sets then | A  B | = | A | | B| In general if A 1, A 2...A n are disjoint sets, then |A 1  A 2 ...  A n | = |A 1 | |A 2 |... |A n |

15 Examples There are 18 math majors and 325 computer science majors at a college –How many ways are there to pick two representatives, so that one is a math major and the other is a computer science major? 18*325 = 5850 –How many ways are there to pick one representative who is either a math major or a computer science major? 18+325 = 343

16 Examples A multiple choice test contains 10 questions. There are four possible answers for each question. –How many ways can a student answer the questions on the test if every question is answered? 4*4*4*4*4*4*4*4*4*4 = 4 10 –How many ways can a student answer the questions on the test if the student can leave answers blank? 5*5*5*5*5*5*5*5*5*5 = 5 10

17 Principle of Inclusion-Exclusion When two tasks can be done at the same time we add the number of ways to do each of the two tasks, then subtract the number of ways to do both tasks. If A and B are not disjoint |A  B|  |A|  |B|  |A  B| –Don't count objects in the intersection of two sets more than once!

18 How many bit strings of length eight either start with 1 or end with the two bits 00? Add (number of bit strings that look like 1xxxxxxx) to the (number of bit strings that look like xxxxxx00) minus the (number of bit string that look like 1xxxxx00) 1*2*2 *2 *2 *2 *2 *2 + 2* 2* 2* 2* 2* 2*1*1 – 1*2*2*2*2*2*1*1 = 2 7 +2 6 -2 5 = 2 5 (4+2-1) = 5*2 5 = 5*32 = 160

19 The Pigeonhole Principle Rosen 4.2

20 Pigeonhole Principle If k+1 or more objects are placed into k boxes, then there is at least one box containing two or more objects.

21 Generalized Pigeonhole Principle If N objects are placed into k boxes, then there is at least one box containing at least  N/k  objects Examples –Among any 100 people there must be at least  100/12  = 9 who were born in the same month. –What is the minimum number of students needed in a class to be sure that at least 6 to get the same grade? (5 choices for grades:A,B,C,D,F) Smallest integer N such that  N/5  = 6, 5*5+1 = 26

22 Example What’s the minimum number of students, each of whom comes from one of the 50 states must be enrolled in a university to guarantee that there are at least 100 who come from the same state? 50*99 + 1 = 4951  4951/50  = 100

23 Permutations and Combinations Rosen 4.3

24 Permutations A permutation of a set of distinct objects is an ordered arrangement these objects. An ordered arrangement of r elements of a set is called an r-permutation. The number of r-permutations of a set with n elements is denoted by P(n,r). A = {1,2,3,4} 2-permutations of A include 1,2; 2,1; 1,3; 2,3; etc…

25 Counting Permutations Using the product rule we can find P(n,r) = n*(n-1)*(n-2)* …*(n-r+1) = n!/(n-r)! How many 2-permutations are there for the set {1,2,3,4}? P(4,2)

26 Combinations An r-combination of elements of a set is an unordered selection of r element from the set. (i.e., an r-combination is simply a subset of the set with r elements). Let A={1,2,3,4} 3-combinations of A are {1,2,3}, {1,2,4}, {1,3,4}, {2,3,4}(same as {3,2,4}) The number of r-combinations of a set with n distinct elements is denoted by C(n,r).

27 Example Let A = {1,2,3} 2-permutations of A are: 1,2 2,1 1,3 3,1 2,3 3,2 6 total.Order is important 2-combinations of A are: {1,2}, {1,3}, {2,3} 3 total. Order is not important If we counted the number of permutations of each 2-combination we could figure out P(3,2)!

28 How to compute C(n,r) To find P(n,r), we could first find C(n,r), then order each subset of r elements to count the number of different orderings. P(n,r) = C(n,r)P(r,r). So C(n,r) = P(n,r) / P(r,r)

29 The English alphabet contains 21 consonants and 5 vowels. How many strings of six lower case letters of the English alphabet contain: exactly 2 vowels? Choose position for the vowels. C(6,2) = 6!/2!4! = 15 Choose the two vowels. 5 choices for each of 2 positions = 5 2 Each of the other 4 positions can contain any of 21 consonants. 21 4 15*5 2 *21 4

30 The English alphabet contains 21 consonants and 5 vowels. How many strings of six lower case letters of the English alphabet contain: at least 1 vowel Count the number of strings with no vowels and subtract this from the total number of strings. 26 6 - 21 6

31 Binomial Coefficient Another notation for C(n,r) is. This number is also called a binomial coefficient. These numbers occur as coefficients in the expansions of powers of binomial expressions such as (a+b) n.

32 Binomial Theorem Let x and y be variables and let n be a positive integer. Then


Download ppt "Recursive Definitions Rosen, 3.4 Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called."

Similar presentations


Ads by Google