XR2b - College of Computing

advertisement
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
• {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
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.
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,...}
Prove that the nth term in the Fibonacci
n 2
sequence is 1   f i when n2
i 0
Induction Proof:
2 2
Basic Step: Let n = 2, then f2 = 1 =
1   f i =1+0
i 0
Inductive Step: Consider k2 and assume that the expression is
true for 2  n  k. We must show
k1 that the expression is true for
 (1   f i )
n = k+1, i.e., that fk+1
i 0
fk+1
= fk +fk-1 by definition
k 2
 (1   f i )  f k 1
i 0
by the inductive hypothesis
Fibonacci Proof (cont.)
k 2
 (1   f i )  f k 1
i 0
k1
 (1   f i )
i 0
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.
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
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.
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
Example Zero-One Matrix
b1
a1
a2
a3
b2
b3
1 1 0
0 1 0


0 1 1
R = {(a1,b1), (a1,b2), (a2,b2), (a3,b2), (a3,b3)}
Basic Counting
Rosen 4.1
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|
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|
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
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
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!
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
The Pigeonhole Principle
Rosen 4.2
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.
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
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
Permutations and Combinations
Rosen 4.3
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…
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)
4 * 3 * 2 *1 4!
4*3 
  12
2 *1
2!
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).
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)!
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)
n!
n!
(n  r )! n!(r  r )!



r!
(n  r )!r! r!(n  r )!
(r  r )!
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
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
Binomial Coefficient
n

 
Another notation for C(n,r) is r  . 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.
Binomial Theorem
Let x and y be variables and let n be a positive
integer. Then
n
(x  y) n   C(n, j)x n  j y j
j 0
n n 
n n1
n n 2 2
n  n 1 
n n









x 
x y
x y  ...
xy 
y
0
1
2
n  1
n
Download