Title: Counting%20Tools
1Counting Tools
- Enumeration
- Multiplication
- Addition
- Negation
2Enumeration
- Make a list of the possibilities
- This is fine if the list is short!
3Sum of dice is 7 or 11
- Sum is 7 there are 6 ways to do it
- 1,6 or 6,1
- 2,5 or 5,2
- 3,4 or 4,3
- Sum is11 there are 2 ways to do it
- 5,6 or 6,5
4Binary numbers
- Eight-bit strings of 0s and 1s with exactly one
1 - 10000000
- 01000000
- 00100000
- 00010000
- 00001000
- 00000100
- 00000010
- 00000001
5Orderings of ABCDEFG
- Strings starting with A
- There are 6! (why?) you dont want to
enumerate these - Strings containing ABC and CFD
- ABCFD must be in the string, plus E and G
- You can enumerate these using permutations
algorithm ABCFDEG, ABCFDGE, EABCFDG, EGABCFD,
GABCFDE, GEABCFD - 6 orderings
- Strings containing ABC, GA, and CFD
- GABCFD must be in the string, plus E
- GABCFDE, EGABCFD
- 2 orderings
6Multiplication Principle
If an activity can be constructed in t successive
steps and step 1 can be done in n1 ways, step 2
can then be done in n2 ways, ... , and step t can
be done in nt ways, then the number of different
possible activities is n1 n2 ... nt
7License Plates
- 3 letters, 2 digits
- Steps
- Pick first letter, Pick second letter, Pick third
letter, Pick first digit, Pick second digit - The number of ways depends on whether repetitions
are allowed - 2626261010 (repetitions allowed)
- 262524109 (no repetitions allowed)
8Menu Selections
- 2 appetizers (opt)
- 3 main courses
- 4 beverages (opt)
- 3 appetizers (counting none)
- 3 main
- 5 beverages (counting none)
- 45
9Committee Assignments
- 6 person committee, 3 positions chair,
secretary, treasurer (A, B, C, D, E, and F) - Procedure
- Step 1. Chair
- Step 2. Secretary
- Step 3. Treasurer
- Counting
- Step 1. 6 possibilities
- Step 2. 5 possibilities
- Step 3. 4 possibilities
- 654 120
10Committee Assignments
- 6 person committee A,B,C,D,E,F
- Subcommittees with D and not F
- Procedure
- Step 1 Assign D a position
- Step 2 Pick members for other positions from
A,B,C,E - Counting
- Step 1 D can have any of three positions
- Step 2 43 for the two positions left after
assigning D a position - 3(43) 24
11Special case of the Multiplication Principle the
Odometer Principle
00000 00001 . . . 00009 00010 00011
. . . 00019 . .
. 00090 00091 . . . 00099
00100 00101 . . . 00109 00110 00111
. . . 00119 . .
. 00190 00191 . . . 00199
00100 00101 . . . 00109 00110 00111
. . . 00119 . .
. 00190 00191 . . . 00199
12Odometer Principle
- If you are looking at the number of ways to order
n digits in same base (e.g., 10 or 2), then the
number of ways is 1 followed by n zeroes in that
base.
13Number of strings of digits
- Five digits base 10
- Multiplication principle 105
- Odometer principle 10000010
- Eight digits base 2
- Multiplication principle 28
- Odometer principle 1000000002 256
14Braille Letters
Each dot can be raised or not 26 (-1 if at
least one dot must be raised)
Alternate view Binary numbers from 1 to 26-1
15Using the constraint itself
- Eight-bit palindromes
- A palindrome reads the same backwards and
forwards - 1101 1011
- 24 16
16Addition Principle
- Suppose that X1,...,Xt are sets and that the ith
set Xi has ni elements. If X1,...,Xt is a
pairwise disjoint family (i.e., if i ? j, Xi n Xj
?), the number of possible elements that can be
selected from X1 or X2 or ... or Xt is n1 n2
... nt.
17Blue and Red Dice
- Two dice show exactly one two
- Blue die is 2 red die is not 2
- 15
- Red die is 2 blue die is not 2
- 15
- Disjoint sets add the results
18Blue and Red Dice
- At least 1 die shows 2
- 16 16 1
- 16 15
1,2 2,2 3,2 4,2 5,2 6,2
2,1 2,2 2,3 2,4 2,5 2,6
19Blue and Red Dice
Total 66 36
Both dice are 2 (1)
Red is 2, blue is not (5)
- Blue is 2,
- red is not (5)
Neither die is 2 (55 25)
20Numbers from 5 to 200
- Numbers from 5 to 200, inclusive
- Digits must be in strictly increasing order
- How many numbers like this are there?
21Numbers from 5 to 200
- How many numbers are there?
- Case 1 Numbers from 5 to 9
- All 5 have digits in increasing order (5)
- Case 2 Numbers from 10 to 99
- Choose two different digits, from 1,2,,9, and
put them in increasing order (C(9,2)) - Case 3 Numbers from 100 to 200
- The numbers will all start with 1
- Choose two different digits, from 2,,9, and
put them in increasing order (C(8,2))
22Permutations
- A permutation of n distinct elements x1, ..., xn
is an ordering of the n elements x1, ..., xn - There are n! permutations of n elements
23Permutations of a string
- How many permutations of ABCDEF contain the
string DEF? - Permutations of the set A, B, C, DEF
- 4!
- Permutations containing DB and AE?
- Permutations of the set AE, C, DB, F
- 4!
24Permutations of a string
- Set of letters a, b, c, d, e
- How many 5-letter strings contain either ae or
ea? - Case 1 String contains ae
- Permutations of ae, b, c, d
- 4!
- Case 2 String contains ea
- Permutations of ea, b, c, d
- 4!
- 24!
25Permutations of a string
- 5-letter strings using a, b, c, d, e
- Containing both ab and be
- abe must appear in the string
- Permutations of abe, c, d
- 3!
26Round table
- How many ways to seat 6 persons around a round
table? - 1 (pick a position for A) 5 4 1
27Strings with repetitions of letters
- Set of letters a, b, c, d, e
- Number of 5-letter strings containing db and ae
- Five cases
- db , ae, a
- db , ae, b
- db, ae, c
- db, ae, d
- db, ae, e
- 3! permutations in each case
- Total 3! 3! 3! 3! 3! 53!
28Strings with repetitions of letters
- 5-letter strings using a, b, c, d, e
- Containing both ab and be
- Case 1 abe is in the string (75)
- Case 2 Case 2 There is a 4-letter string
containing ab and be, but no 3-letter string
(i.e., abbe or beab but not abe) (18) - Case 3 There is a 5-letter string containing
ab and be, but no 4-letter string or 3-letter
string (i.e., ab?be or be?ab but not abe or
abbe or beab) (8) - 101 strings
29Case 1
- abe is in the string
- Case 1a abe??
- Case 1b ?abe?
- Case 1c ??abe
- Or
- Step 1 Pick a starting position for abe
- Step 2 Choose the other two characters
- 355 75
30Case 2
- Case 2 There is a 4-letter string containing
ab and be, but no 3-letter string (i.e.,
abbe or beab but not abe) - Case 2a abbe? or ?abbe
- 5 strings 5 strings
- Case 2b beab? or ?beab
- 4 strings 4 strings
- 18
31Case 3
- There is a 5-letter string containing ab and
be, but no 4-letter string or 3-letter string
(i.e., ab?be or be?ab but not abe or abbe
or beab) - Case 3a ab?be
- 3 strings
- Case 3b be?ab
- 5 strings
- 8
32Strings with repetitions of letters
- Set of letters a, b, c, d, e
- How many 5-letter strings contain either ae or
ea? - Case 1 Containing ae (500)
- Step 1 Pick a position for ae (4)
- Step 2 Pick the remaining letters 53
- Case 2 Containing ea (500)
- Step 1 Pick a position for ea (4)
- Step 2 Pick the remaining letters 53
33Strings with repetitions of letters
- Case 3 Containing both ae and ea (90)
- aea?? 25 possibilities
- ?aea? 25 possibilities
- ??aea 25 possibilities 1 (for overlap with
case 3a) - aeea? 5 possibilities
- eaae? 5 possibilities 1 (for overlap with
case 3c) - ae?ea 5 possibilities 2 (for overlap with
case 3a and 3c) - ea?ae 5 possibilities 1 (for overlap with
case 3b)
34Strings with repetitions of letters
Total 55 3125
Both (90)
Contains ea but not ae (410)
- Contains
- ae but
- not ea
- (410)
Contains neither 3125 910 2215
35r-Permutations
- An r-permutation of n (distinct) elements x1,
..., xn is an ordering of an r-element subset of
x1,...,xn. - The number of r-permutations of a set of n
distinct elements is denoted P(n,r). - P(n,r) n (n-1) ... (n-r1) n!/(n-r)!
36- 3-permutations of 4 letters
- 432
- 5-permutations of 11 objects
- 1110987
37r-Combinations
- An r-combination of X is an unordered selection
of r-element subsets of X - The number of r-combinations of a set of n
distinct elements is denoted C(n,r) or ( )
38Committees and Sub-committees
- Club 6 women and 6 men
- Committee of 5 persons
- C(12,5)
- Committee of 4, and exactly 1 is a woman
- C(6,1) to pick the woman
- C(6,3) to pick the men
- C(6,1)C(6,3)
39Committees and Sub-committees
- Four person with at least one man and one woman
- All-male committee C(6,4)
- All-female committee C(6,4)
- C(12,4) 2C(6,4)
40Poker Hands
- All Aces
- Step 1 Pick the aces (C(4,4))
- Step 2 Pick the other cards (C(48,1))
- Four of a kind
- Step 1 Pick which kind (13 ways)
- Step 2 Pick which suits (C(4,4))
- Step 3 Pick the other cards (C(48,1))
- All spades
- C(13,5)
41Poker Hands
- Cards of two suits
- Pick the two suits C(4,2)
- Pick the cards C(26,5)
- Subtract the hands containing only 1 suit
2C(13,5)
42Negation
- The number of possibilities with constraint X is
equal to the possibilities with no constraints
minus the possibilities with constraint not X. - This is a special case of the addition principle.
43Binary numbers
- Eight bit string, with at least one 1
- The only string that doesnt have at least one 1
is 00000000 - 28 - 1
44Braille Letters
Each dot can be raised or not 26 -1 since at
least one dot must be raised
Alternate view Binary numbers from 1 to 26-1
45Poker Hands
- Containing cards of more than one suit
- Not (containing cards of one suit)
- Cards of one suit
- Step 1. Pick the suit C(4,1)
- Step 2. Pick the cards C(13,5)
- All possible hands C(52,5)
- Cards of more than one suit C(52,5)
C(4,1)C(13,5)
46Permutations
- Orderings of ABCDEF not containing DEF
- Orderings of ABCDEF containing DEF 4!
- All orderings of ABCDEF 6!
- Orderings not containing DEF 6! 4!
47Subcommittees
- Group has 6 men, 6 women
- Subcommittees containing at least one man and one
woman - Not (subcommittees of a single sex)
- Step 1. Pick the sex C(2,1)
- Step 2. Pick the members C(6,4)
- All subcommittees C(12,4)
- At least 1 man and 1 woman C(12,4)
C(2,1)C(6,4)
48Summary
- Enumeration for small numbers of things
- Multiplication principle define steps
- Addition principle define distinct cases
- Negation approach how many x dont have the
property? - Permutations sequences (order is important)
- Combinations sets (ignore order)
49Algorithm for combinations
- 4-combinations of 1,2,3,4,5,6,7
- Problem 1 How do we avoid repeating the same
set, since order doesnt matter - Generate the elements in increasing order
- Problem 2 What is the first combination?
- 1,2,3,4
- Problem 3 How do we generate the next element
from the current one? - Problem 4 What is the last combination?
- 4,5,6,7
50Algorithm for Combinations
- Try it by hand
- 1234
- 1235
- 1236
- 1237
- 1245
- 1246
- 1247
- 1256
- 1257
- 1267
- 1345
- 1346
51Algorithm for Combinations
- Step 1 Find last (rightmost) item (sm) thats
not equal to its maximum value and add one to it.
- Maximum value
- for sr the maximum is n
- for sr-i the maximum is n-i
- Step 2 For the remaining items (to the right of
sm), let si si-1 1
52Algorithm for Permutations
- Note that order matters, so we dont have to
worry about repeating - Problem 1 What is the first permutation?
- 1n
- Problem 2 How do we generate the next
permutation from the current one? - Problem 3 What is the last permutation?
- n1
53Algorithm for Permutations
- Try it by hand
- 12345
- 12354
- 12435
- 12453
- 12534
- 12543
- 13245
- 13254
- 13425
- 13452
- 13524
- 13542
54Algorithm for Permutations
- Step 1 Locate the rightmost sm such that sm lt
sm1 (entries sm1 sn are in decreasing order) - Step 2 Locate the rightmost (last) si after sm
such that sm lt si - Step 3 Swap sm and si (entries sm1 sn are
still in decreasing order) - Step 4 Reverse the order of sm1 sn
55Recursive algorithm
- Create a list 12n
- For each element i in the list
- Generate the set of permutations of the remaining
elements - Return the set of permutations formed by
appending the element i to the permutation - Base case Only 1 element in the list return
the set of permutations containing the list
56Two more tools for counting
- Generalized permutations to count orderings of a
string containing multiple identical letters - n! / (n1!nt!)
- Generalized combinations to count the number of
ways to put n identical things into k distinct
groups - C(nk-1, k-1)
57MISSISSIPPI
- How many strings can be formed from the above
letters?
Solution Fill 11 blanks _ _ _ _ _ _ _ _ _ _ _
Step 1. Pick the positions for the Ss Step 2.
Pick the positions for the Is Step 3. Pick the
positions for the Ps Step 4. Put the M in the
remaining position
58Counting the ways
Step 1. Pick the positions for the Ss C(11,4)
Step 2. Pick the positions for the Is C(7,4)
Step 3. Pick the positions for the Ps C(3,2)
Step 4. Put the M in the remaining position C(1,1)
C(11,4)C(7,4)C(3,2)C(1,1)
59Counting sequences with identical objects
- Suppose that a sequence S of n items has n1
identical objects of type 1, n2 identical objects
of type 2, , and nt identical objects of type t.
Then the number of orderings of S is - n!________
- n1! n2! nt!
60Using equivalence relations
- M, I1, S1, S2, I2, S3, S4, I3, P1, P2, I4
- Define R
- Let p and q be permutations of the above string.
- pRq if the letter in each position of the two
permutations is the same (ignore the number)
How many members in each equivalence class?
Pick any permutation how many ways to reorder
it so that the same letters appear in the same
positions?
61Eight books
- How many ways to divide among Bill (with 4),
Shizuo (with 2), and Marian (with 2)
B
B
B
B
S
S
M
M
How many orderings of the string BBBBSSMM
62Choosing six books
- Three books CS, Physics, History
- Library has at least 6 of each
- How many ways can we choose 6?
63Choosing 6 books
CS Physics History
6
5 1
5 1
4 1 1
64Choosing 6 books
CS Physics History
xxxxxx
xxxxx x
xxxxx x
xxxx x x
Number of ways of ordering 6 xs and 2 s !!
65Building sets by choosing identical elements
- If X is a set containing t elements, the number
of unordered, k-element selections from X,
repetitions allowed, is - C(kt-1, t-1) C(kt-1,k).
66Red, Green, Blue Balls
- How many ways can we select 8 balls
- How many ways can we select 8 balls with at least
one of each color?
6712 identical math books
- Distributed among Anna, Beth, Candy, and Don
68Solutions to equation
- x1 x2 x3 x4 29 if x1, x2, x3, x4 are
non-negative integers? - If x1 gt 0, x2 gt 1, x3 gt 2, x4 gt 0?
69How many times is the print statement executed?
- For i1 1 to n do
- for i2 1 to i1 do
- for i3 1 to i2 do
- for ik 1 to ik-1 do
- print i1,, ik