9/4/2015 prepared by dr.saad alabbad1 cs100 : discrete structures counting(1) dr.saad alabbad...
TRANSCRIPT
04/19/23 Prepared by Dr.Saad Alabbad 1
CS100 : Discrete Structures
Counting(1) Dr.Saad Alabbad
Department of Computer ScienceE-mail: [email protected]
Tel # 2581888
04/19/23 Prepared by Dr.Saad Alabbad 2
Basics of Counting
Counting objects with certain properties is very important in solving many problems: How many ways are there to travel from Riyadh to
Makkah by car given this map? How many license plates can be made using three
letters and three digits? How many ways are there to select 2 persons from a
group of 5? How many ways are there to arrange 5 books in a shelf?
04/19/23 Prepared by Dr.Saad Alabbad 3
Basics of CountingProduct Rule
PRODUCT RULE: If a procedure can be broken into two tasks such that there are:
• n1 ways to do the first task,• n2 ways to do the second taskthen there are n1 · n2 ways to do the procedure.
Riyadh Taif Makkah
a
b
c
d
e
How many ways to go from Riyadh to Makkah? 6 ways=3*2
ad , ae
bd , be
cd , ce
04/19/23 Prepared by Dr.Saad Alabbad 4
Basics of CountingGeneralized Product Rule
PRODUCT RULE: If a procedure can be broken into i tasks such that there are:
• n1 ways to do the first task,• n2 ways to do the second task…. ni ways to do the ith task
then there are n1 · n2 · · · ni ways to do the procedure.
Dammam RiyadhMakkah
How many ways to go from Dammam to Makkah? n1*n2*…ni
Taif ……n1 n2 ni
Taif
04/19/23 Prepared by Dr.Saad Alabbad 5
Basics of CountingProduct Rule
Example 1:Rooms of a hotel are labeled with one letter (a..z) and two digits(0..9).How many rooms can be labeled using this scheme?
There are 26 ways for the first place. 10 ways for the second and 10 for the third. By the product rule the total is 26*10*10=2600
Example 2: A byte consist of 8 bits. How many different values can be represented using one byte?
Each bit can have either 0 or 1. By the product rule the total is 28=256
a..z 0..9 0..9
04/19/23 Prepared by Dr.Saad Alabbad 6
Basics of CountingProduct Rule
Example 3:In example 1 , How many rooms can be labeled if labels starting with a vowel letter (a,w,i,o,u) or ending with 0 are not allowed
Answer: 21*10*9Example 4: how many ways are there to arrange 5 people in a row There are 5 ways for the first place,4 for the second, etcTotal is 5*4*3*2*1=5!
Example 5: How many functions are there from a set with 3 elements to a set with 4 elements?
A function must assign for each element in the domain one element from the codomain so there are: 4*4*4=43 functions
21 0..9 1..9
.A
.B
.C
.D
1.
2.
3.Domain Codomain
04/19/23 Prepared by Dr.Saad Alabbad 7
Basics of CountingProduct Rule
Example 6: How many subsets are there in a set S of n elements?Arrange S in any order a1, a2, a3,…. an
Take an arbitrary subset, then the first element a1 is either in this subset or not
the second element a2 is either in this subset or not
….the nth element an is either in this subset or not
So there are 2n possible ways to construct a subset from a set of n elements
NOTENOTE: The product rule is often phrased in terms of sets as follows: If A1, A2, . . ., Ak are finite sets then the number of elements in the Cartesian product of these sets is
|A1×A1 × … × Ak|= |A1| . |A2| …..|Ak|.
04/19/23 Prepared by Dr.Saad Alabbad 8
Basics of CountingSum Rule
SUM RULE: If a task can be done either in n1 ways or n2 ways where none of n1 ways is the same as any of the n2 ways then there are n1 +n2 to do the task
In general, if a set of tasks can be broken into k mutually disjoint subsets of tasks A1, A2, . . ., Ak, the total number of tasks is |A1| + |A2| + · · · + |Ak|.
Example 1: A university has three colleges. The colleges have 4,12 and 7 departments. In how many ways a student can choose his major?
A student can choose a major by selecting a department from the first ,the second or the third college so the total is 4+12+7=23 ways
04/19/23 Prepared by Dr.Saad Alabbad 9
Basics of CountingCombining Sum Rule and Product
RuleExample 2: How many bit strings of length 8 either
start with 10 or 011? We have two cases:case 1: 10xxxxxx there are 26 ways by product ruleCase 2: 011xxxxx there are 25 ways by product ruleTotal is 25+26 by sum ruleNote that it is not possible to have a string that
starts with 10 and 011 at the same time
04/19/23 Prepared by Dr.Saad Alabbad 10
Principle of Inclusion-Exclusion (subtraction rule)
Example 3: How many bit strings of length 4 either start with 1 or end with 01? We have two cases:
case 1: 1xxx there are 23 ways by product ruleCase 2: xx01 there are 22 ways by product ruleBut strings of the form 1x01 are counted twice. There
are 21 of these so the total is 23+ 22 – 21=10
Case2 xx01
0001
0101
1001
1101
Case1 1xxx
1000
1001
1010
1011
1100
1101
1110
1111
04/19/23 Prepared by Dr.Saad Alabbad 11
Principle of Inclusion-Exclusion (subtraction rule)
In the previous example we have used what we call:Principle of Inclusion-Exclusion : Number of ways to do two
tasks is the number of ways to do the first plus the number of ways to do the second minus the number of ways to do both.
Stated in terms of sets:Let A and B be sets. The number of way to select an element
from A or B is:
|A B| = |A| + |B| − |A B|
|A|=7 |B|=5
04/19/23 Prepared by Dr.Saad Alabbad 12
Principle of Inclusion-Exclusion (subtraction rule)
Example 1: How many bit strings of length 8 either start with 10 or end with 011? We have two cases:
case 1: 10xxxxxx there are 26 ways by product ruleCase 2: xxxxx011 there are 25 ways by product ruleBut strings of the form 10xxx011 are counted twice
and we have 23 of these so the total is: 26+ 25 - 23
04/19/23 Prepared by Dr.Saad Alabbad 13
Principle of Inclusion-Exclusion (subtraction rule)
Example 2: How many ways to arrange a family of 6 people (parents + 4kids) where:
A) the father must be next to the mother?There are 4*3*2*1 to arrange the kidsThere are 5 ways to place the parentsThere are 2 ways for parents to switch positionsTotal 2*5*(4*3*2*1)=240 by product ruleB) the father is not next to the mother?Ways to arrange the family =6*5*4*3*2*1=720 product rule Ways to not arrange father next to mother=720-240=480
inclusion-exclusion principle
Question : Ways to position father somewhere left to Question : Ways to position father somewhere left to mother? mother?
1
F M
M F
234
04/19/23 Prepared by Dr.Saad Alabbad 14
Counting using Tree Diagrams
Example 1: How many bit strings of length 4 do not have consecutive 1s?
04/19/23 Prepared by Dr.Saad Alabbad 15
Counting using Tree Diagrams
Example 2:Playoff of 2 teams with at most 5 games.The team that wins three games wins the playoff.
04/19/23 Prepared by Dr. Zakir H. Ahmed 16
If n pigeons fly into k pigeonholes and k < n, then some pigeonhole contains at least two pigeons.
Theorem (The Pigeonhole Principle): If k is a positive integer and k+1 or more objects are placed into k boxes, then there is at least one box containing two or more of the objects.
Proof: We will prove it by contradiction.Suppose that none of k boxes contains more than one object. Then the total number of objects would be at most k. This is a contradiction, because there at least k+1 objects.
The pigeonhole principle is also called the Dirichlet drawer principle, because the German mathematician Dirichlet often used this principle in his work.
The Pigeonhole Principle:Introduction
04/19/23 Prepared by Dr. Zakir H. Ahmed 17
Corollary : A function from a set with k+1 or more elements to a set with k elements is not one-to-one.
Example 16: Among any group of 367 people, there must be at least two with the same birthday, because there are only 366 possible birthdays.
Example 17: In any group of 27 English words, there must be at least two that begin with the same letter, because there are only 26 letters in English alphabet.
Example 18: How many students must be in a class to guarantee that at least two students receive the same score on the final exam, if the exam is graded on a scale from 0 to 100 marks?
Solution: There are 101 possible scores on the final. The pigeonhole principle shows that among any 102 students there must be at least two students with the same score.
The Pigeonhole Principle:Examples
04/19/23 Prepared by Dr. Zakir H. Ahmed 18
Theorem (The Generalized Pigeonhole Principle): If N objects are placed into k boxes, then there is at least one box containing at least ⌈N/k⌉ objects.
Proof: We will prove it by contradiction.Suppose that none of boxes contains more than ⌈N/k⌉ -1 objects. Then the total number of objects is at most k(⌈N/k⌉-1) < k((N/k+1) – 1) = N.where the inequality ⌈N/k⌉ < (N/k)+1 has been used. This is a contradiction, because there a total of N objects.
Example 19: Among 100 people, there are at least ⌈100/12⌉ = 9 who were born in the same month.
Example 20: What is the minimum number of students required in CS100 to be sure that at least six receive the same grade, if there nine possible grades A+, A, B+, B, C+, C, D+, D and F?
Solution: ⌈N/9⌉ = 6. The smallest integer is N = 9.5+1 = 46
The Pigeonhole Principle:Generalized
04/19/23 Prepared by Dr. Zakir H. Ahmed 19
Def: A permutation is an ordered arrangement of objects. The number of permutations of r distinct objects chosen from n distinct objects is denoted P(n,r) or nPr.
P(n,r) = n!/(n-r)! = n x (n-1) x (n-2) x ……..x (n-r+1)
If repetitions are allowed, then P(n,r) = nr. Example 21: In a running race of 12 runners, each of the
top 5 finishers receives a different medal. How many ways are there to award the 5 medals?
Solution:
Total is 12x11x10x9x8
Permutations:Definition
12 11 10 9 8
04/19/23 Prepared by Dr. Zakir H. Ahmed 20
Example 22: A salesman has to visit 8 cities. The trip needs to start at a specified city. How many ways the person can visit the 7 cities?
Solution: Total number of ways is P(7,7) = 7! = 5040.
Example 23: How many permutations of the letters ABCDEFGH contain the string ABC?
Solution: Because the letters ABC must occur as a block, we can find the answer by finding the number of permutations of six objects, namely, the block ABC, and the individual letters D, E, F, G, and H. Because this six objects can occur in any order, there are 6! = 720 permutations of the letters ABCDEFGH in which ABC occurs as a block.
Permutations:Examples
04/19/23 Prepared by Dr. Zakir H. Ahmed 21
Example 24: Suppose you have time to listen to 10 Surah recitation from the Holy Quran daily. There are 6 Surah by Abdullah, 8 by Badr, and 3 by Saleh to choose from. How many different playlists can you make?
Solution: P(17,10) = 17x16x15x14x13x12x11 Example 25: Suppose you have time to listen to 10 Surah
recitation from the Holy Quran daily. There are 6 Surah by Abdullah, 8 by Badr, and 3 by Saleh to choose from. How many different playlists can you make? Now suppose you want to listen 4 Surah by Abdullah, 4 by Badr, and 2 by Saleh, in the order. How many playlists can you make?
Solution: P(6,4) x P(8,4) x P(3,2)
Permutations:Examples
04/19/23 Prepared by Dr. Zakir H. Ahmed 22
Combinations:Definition
Def: A combination is an unordered selection of elements from some set. The number of combinations of r distinct objects chosen from n distinct objects is denoted by C(n,r) or nCr or , and is read “n choose r.”
C(n,r) = P(n,r)/r! = n!/((n-r)!r!) = C(n, n-r)
n
r
04/19/23 Prepared by Dr. Zakir H. Ahmed 23
Combinations:Definition
Difference between Permutation and Combination:
Example (a): A basketball squad consists of 12 players, 5 of which make up a team. How many different teams of players can you make from the 12?
Solution: Total number of ways is C(12, 5). Example (b): In a running race of 12 runners,
each of the top 5 finishers receives a different medal. How many ways are there to award the 5 medals?
Solution: Total number of ways is P(12, 5).
04/19/23 Prepared by Dr. Zakir H. Ahmed 24
Combinations:Questions
Example 26: Suppose you have to select 11 students for a football team. There are 6 students in group-1, 8 students in group-2, and 3 students in group-3 to choose from. How many different football team can you make?
Solution: Total number of ways is C(17,11) Example 27: Suppose you have to select 11 students for a
football team. There are 6 students in group-1, 8 students in group-2, and 3 students in group-3 to choose from. Now suppose you want to select 4 from group-1, 5 from group-2, and 2 from group-3 in that team. How many different football team can you make?
Solution: Total number of ways is C(6,4) x C(8,5) x C(3,2)
04/19/23 Prepared by Dr. Zakir H. Ahmed 25
Combinations:Questions
Q 2: Suppose you have to select 11 students for a football team. There are 19 students in group-1, and 23 students in group-2.
(i) In how many ways can 5 from group-1 and 6 from group-2 be selected?(ii) In how many ways can a team with exactly 1 from group-1 be selected?(iii) In how many ways can a team with at most 1 from group-1 be selected?(iv) In how many ways can a team with at least 1 from group-1 be selected?
04/19/23 Prepared by Dr. Zakir H. Ahmed 26
Binomial Coefficients:The Binomial Theorem
(a + b)2 = a2 + 2ab + b2
(a + b)3 = a3 + 3ab2 + 3a2b + b3
(a + b)4 = a4 + 4ab3 + 6a2b2 + 4a3b + b4
Why am I showing this to you?
1.Beautiful patterns2.Recursive definition3.New type of proof
4.Applications in more complex counting
techniques
04/19/23 Prepared by Dr. Zakir H. Ahmed 27
Binomial Coefficients:The Binomial Theorem
Example 28: What is the expansion of (x + y)4? Solution: From the Binomial Theorem it follows that
Binomial Theorem: Let x and y be variables, and let n be any nonnegative integer. Then
(x y)n n
j
j 0
n
x n j y j
Binomial coefficient Select j x’s from (x +y)n
.464
4
4
3
4
2
4
1
4
)(
432234
432234
yxyyxyxx
yxyyxyxx
0
4
yxj
4yx jj4
4
0j
4
04/19/23 Prepared by Dr. Zakir H. Ahmed 28
Binomial Coefficients:The Binomial Theorem
Example 29: What is the coefficient of x12y13 in the expansion of (x + y)25?
Solution: From the Binomial Theorem, the coefficient is
300,200,5!12!13
!25
13
25
Example 30: What is the coefficient of x12y13 in the expansion of (2x - 3y)25?
Solution: This expression equals (2x + (-3y))25. By the Binomial Theorem, we have
25
0
2525 .)3()2(25
j
jj yxj
(-3y)2x
The coefficient x12y13 is obtained by putting j = 13.
13121312 32!12!13
!25)3(2
13
25
04/19/23 Prepared by Dr. Zakir H. Ahmed 29
Binomial Coefficients:The Binomial Theorem
Example 31: Let n be a nonnegative integer. Then prove that
.2
n
0k k
n n
Proof: Using Binomial Theorem with x = 1, y = 1, we get
.11)11(2
n
0k
n
0k k
n
k
n kknnn
Example 32: Let n be a positive integer. Then prove that
.0)1(
n
0k k
nk
Proof: Using Binomial Theorem with x = 1, y = -1, we get
.)1()1(1))1(1(0 kkknn
n
0k
n
0k k
n
k
n
Q 3: Let n be a nonnegative integer. Then prove that .32
n
0k k
n nk
04/19/23 Prepared by Dr. Zakir H. Ahmed 30
End of Chapter IV