1 / 31

Recursive Definitions

Recursive Definitions. Rosen, 3.4. 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

floriane
Télécharger la présentation

Recursive Definitions

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. Recursive Definitions Rosen, 3.4

  2. Recursive (or inductive) Definitions • Sometimes easier to define an object in terms of itself. • This process is called recursion. • Sequences • {s0,s1,s2, …} defined by s0 = a and sn = 2sn-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) = 2n, f(n) = 2f(n-1) and f(0) = 1

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

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

  5. Prove that the nth term in the Fibonacci sequence is when n2 Induction Proof: Basic Step: Let n = 2, then f2 = 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 fk+1 fk+1 = fk +fk-1 by definition by the inductive hypothesis

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

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

  8. Prove that T(1) = c0, T(n) = 2T(n-1)+c1has closed form solution T(n) = 2n-1c0 + (2n-1-1)c1 Basis Step: T(1) = 21-1c0 + (21-1-1)c1 = c0 Induction Step: Assume that T(n) = 2n-1c0 + (2n-1-1)c1. We must show that T(n+1) = 2(n+1)-1c0 + (2(n+1)-1-1)c1 = 2nc0 + (2n-1)c1. T(n+1) = 2T(n) + c1 = 2[2n-1c0 + (2n-1-1)c1]+ c1 = 2nc0 + (2n-2)c1 + c1 = 2nc0 + 2nc1 - c1 = 2nc0 + (2n-1)c1.

  9. 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. mij = 1 if (ai,bj) R = 0 if (ai,bj) R

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

  11. Basic Counting Rosen 4.1

  12. Sum Rule • If a first task can be done in n1 ways and a second task can be done in n2 ways, and if these tasks cannot be done at the same time (i.e., the tasks are either/or), then there are n1+ n2 ways to do either task. • If A and B are disjoint sets then |AÈ B|=|A|+|B| • In general if A1, A2 . . .An are disjoint sets, then |A1A2  . . .  An| = |A1| + |A2| + . . . + |An|

  13. Product Rule • Suppose that a procedure can be broken down into two tasks. If there are n1 ways to do the first task and n2 ways to do the second task after the first task has been done, then there are n1n2 ways to do the procedure. • If A and B are disjoint sets then | A  B | = | A | | B| • In general if A1, A2 . . .An are disjoint sets, then |A1  A2  . . .  An| = |A1| |A2| . . . |An|

  14. 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

  15. 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 = 410 • 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 = 510

  16. 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!

  17. 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 = 27+26-25 = 25(4+2-1) = 5*25 = 5*32 = 160

  18. The Pigeonhole Principle Rosen 4.2

  19. 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.

  20. 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

  21. 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

  22. Permutations and Combinations Rosen 4.3

  23. 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…

  24. 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)

  25. 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).

  26. 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)!

  27. 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)

  28. 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 = 52 Each of the other 4 positions can contain any of 21 consonants. 214 15*52*214

  29. 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. 266 - 216

  30. 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.

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

More Related