Download presentation
Presentation is loading. Please wait.
Published byLucinda James Modified over 9 years ago
1
CPSC 221: Data Structures Lecture #23 Counting Steve Wolfman (stolen from Alan J. Hu who stole some from me which I stole from Kim Voll) 2011W2
2
Learning Goals After this unit, you should be able to: Apply counting principles to determine the number of arrangements or orderings of discrete objects, with or without repetition, and given various constraints. Use appropriate mathematical constructs to express a counting problem (e.g. counting passwords with various restrictions placed on the characters within). Identify problems that can be expressed and solved as a combination of smaller sub problems. When necessary, use decision trees to model more complex counting problems. 2
3
Counting: Motivation This is sometimes really useful, e.g.: –How many possible passwords, encryption keys, etc. –How many possible legal inputs, how many possible data structure configurations, how many paths through a network, etc. –Crucial step in computing probabilities. Sometimes fun – like a puzzle or game.
4
Counting: Warning However, once you get beyond basic problems, it can be very subtle, and there are no simple tricks that always work. Sometimes, you just have to think really, really hard and really really carefully! Epp textbook is pretty good. Must read on your own. (3 rd ed 6.1-6.5, 4 th ed. 9.1-9.6) Old slides follow textbook presentation closely. I am going to try to de-mystify some of the formulas.
5
Outline Case-Splitting, Decision Trees “Addition Rule” Deconstructed “Multiplication Rule” Deconstructed Alan’s “Division Rule” Permutation and Combination Formulas Dealing with Overlapping Cases: Inclusion-Exclusion
6
Counting The basic problem is to count how many items are in some set, which might be specified in a non- obvious way. E.g., how many different min-heaps are possible with the numbers 1,…,7? How many poker hands beat a pair of kings? Etc.
7
Basic Technique: Case Splitting This is the most fundamental way to break a problem down into simpler parts. –The cases must be disjoint (no overlap). (We’ll handle overlap later.) –Combined, the cases must cover all possibilities. Count each case separately, then add them all up. Whenever you are totally lost, always fall back to case-splitting. Point of interest: disjoint but covering sets form a partition.
8
Case Splitting Example What’s the population of Canada?
9
Case Splitting Example What’s the population of Canada? –What’s the population of BC? –What’s the population of AB? –What’s the population of SK? –What’s the population of MB? –etc. Add them all up!
10
Other case splits are possible What’s the population of Canada? –How many men in Canada? –How many women in Canada? Add them all up!
11
“Addition Rule” Demystified From Epp: “Suppose a finite set A equals the union of k distinct mutually disjoint subsets A1, A2, …, Ak. Then N(A) = N(A1)+N(A2)+…+N(Ak).”
12
Addition Principle (Sum Rule) If X 1, X 2, …, X t are pairwise disjoint sets (i.e., X i ∩ X j = ∅ ∀ i ∀ j s.t. i, j ∈ {1, 2, …, t }, i ≠ j), then the number of ways to select an element from any of X 1 or X 2 or … or X t is: | X 1 | + | X 2 | + … + | X t |
13
Case Splitting Error: Missing Cases What’s the population of Canada? –Liberals –Conservatives –NDPs Add them all up! If we miss any cases (e.g., Greens, Rhinoceros Party, Marijuana Party, people without political parties, etc.), we’ll undercount.
14
Case Splitting Error: Overlap What’s the population of Canada? –Francophone Canadians –Anglophone Canadians Add them all up! Double counts anyone who speaks both. (We’ll see how to deal with overlap later.) Also misses anyone who speaks neither, of course!
15
Cases can be split into sub-cases What’s the population of Canada? –What’s the population of BC? What’s the population of Vancouver? What’s the population of Burnaby? What’s the population of Richmond? etc. Add them all up. –What’s the population of AB? –What’s the population of SK? –What’s the population of MB? –etc. Add them all up!
16
Decision Trees When you split cases into subcases, it’s often nice to draw this as a tree. These are informally called “decision trees”, although a proper decision tree has probabilities on branches. We’ll just ignore this. (Epp calls them “possibility trees” which is descriptive, but not standard.)
17
Case Splitting Example How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? –How many have 2 at the root? –How many have 3 at the root? –How many have 4 at the root? –How many have 5 at the root? –How many have 6 at the root? –How many have 7 at the root? Add them all up!
18
Case Splitting Example How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? ??? –How many have 2 at the root? 0 –How many have 3 at the root? 0 –How many have 4 at the root? 0 –How many have 5 at the root? 0 –How many have 6 at the root? 0 –How many have 7 at the root? 0 Add them all up!
19
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? And have 3 at left child? And have 4 at left child? And have 5 at left child? And have 6 at left child? And have 7 at left child? Add them all up! People often draw these as trees.
20
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? ??? And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ??? And have 6 at left child? 0 And have 7 at left child? 0
21
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? –And have 4 at right child? –And have 5 at right child? And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
22
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? –And have 4 at right child? –And have 5 at right child? »6 and 7 must be leaves of right child »Therefore 3 and 4 are leaves of left child »Therefore 4 possibilities in this case. And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
23
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? –And have 4 at right child? »Right subtree leaves are 5, 6: »Right subtree leaves are 5, 7: »Right subtree leaves are 6, 7: –And have 5 at right child? 4 And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
24
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? –And have 4 at right child? »Right subtree leaves are 5, 6: 4 »Right subtree leaves are 5, 7: 4 »Right subtree leaves are 6, 7: 4 –And have 5 at right child? 4 And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
25
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? –And have 4 at right child? 12 –And have 5 at right child? 4 And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
26
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? »Leftmost leaf is 4: »Leftmost leaf is 5: »Leftmost leaf is 6: »Leftmost leaf is 7: –And have 4 at right child? 12 –And have 5 at right child? 4 And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
27
Case Splitting: Splitting into Sub-Cases How many different min-heaps are possible with the numbers 1,…,7? –How many have 1 at the root? And have 2 at left child? –And have 3 at right child? »Leftmost leaf is 4: »Leftmost leaf is 5: »Leftmost leaf is 6: »Leftmost leaf is 7: –And have 4 at right child? 12 –And have 5 at right child? 4 And have 3 at left child? ??? And have 4 at left child? ??? And have 5 at left child? ???
28
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –Leftmost leaf is 4: –Leftmost leaf is 5: –Leftmost leaf is 6: –Leftmost leaf is 7:
29
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –Leftmost leaf is 4: Next leaf is 5 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 5: –Leftmost leaf is 6: –Leftmost leaf is 7:
30
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –Leftmost leaf is 4: Next leaf is 5 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 5: Next leaf is 4 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 6: –Leftmost leaf is 7:
31
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –Leftmost leaf is 4: Next leaf is 5 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 5: Next leaf is 4 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 6: etc. –Leftmost leaf is 7: etc. What do you call it when you add up the same number a bunch of times?
32
Multiplication! E.g.: suppose there are 30 NHL teams, each with 23 players. How many players are there in total?
33
Multiplication! If some case split results in a bunch of identical- sized sub-cases, the total is just the number of cases times the size of each sub-case.
34
Multiplication! If some case split results in a bunch of identical- sized sub-cases, the total is just the number of cases times the size of each sub-case. Note that the cases don’t have to be identical, but just have the same number of things in them.
35
Multiplication Principle (Product Rule) If an operation consists of t steps and: Step 1 can be performed in n 1 ways, Step 2 can be performed in n 2 ways, … Step t can be performed in n t ways, then the entire operation can be performed in n 1 n 2 …n t different ways. Example: How many postal codes begin with the letter V and end with the digit 4? __ __ __
36
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –Leftmost leaf is 4: Next leaf is 5 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 5: Next leaf is 4 Next leaf is 6 Next leaf is 7 –Leftmost leaf is 6: etc. –Leftmost leaf is 7: etc. 4 cases with same number of elements in each one.
37
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –4 times: The number of cases with leftmost leaf is 4: –Next leaf is 5 –Next leaf is 6 –Next leaf is 7 4 cases with same number of elements in each one.
38
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –4 times: The number of cases with leftmost leaf is 4: –Next leaf is 5 –Next leaf is 6 –Next leaf is 7 3 cases with same number of elements in each one.
39
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –4 times: 3 times: –The number of cases with next leaf 5 3 cases with same number of elements in each one.
40
Case Splitting: Splitting into Sub-Cases How many have 1 at the root, 2 at left child, and 3 at right child: –4 times: 3 times: –2 times »1
41
Permutations and Factorial The number of ways to arrange n distinct items in a row is n! –This comes straight from the product rule (which comes from case splitting) These are called permutations of the n items.
42
Permutations P(n,r) What if we only want permutations of r elements out of the n objects? –E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there?
43
Permutations P(n,r) What if we only want permutations of r elements out of the n objects? –E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there? –Product Rule: 20^5 if you can win repeatedly –Product Rule: 20*19*18*17*16 if you can win at most one prize.
44
Permutations P(n,r) What if we only want permutations of r elements out of the n objects? –E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there? –Product Rule: 20^5 if you can win repeatedly –Product Rule: 20*19*18*17*16 if you can win at most one prize. This is P(20,5), the number of permutations of 5 objects out of 20.
45
Permutations P(n,r) Note the underline below the r. This notation is due to Knuth and is probably the most standard notation for this, but nothing is really standard.
46
Permutations P(n,r) You can understand this by canceling out terms. But there’s an intuitive way to understand this, too.
47
P(n,r)=n!/(n-r)! Intuition How many ways to line up 20 people? If I only give prizes to first 5, the order of remaining 15 doesn’t matter. How many ways can I order 15 people?
48
P(n,r)=n!/(n-r)! Intuition How many ways to line up 20 people? If I only give prizes to first 5, the order of remaining 15 doesn’t matter. How many ways can I order 15 people? Therefore, the 20! is counting each winning possibility of the top 5 an extra 15! times. So, the answer is 20!/15!
49
P(n,r)=n!/(n-r)! Intuition How many ways to line up n people? If I only give prizes to first r, the order of remaining (n-r) doesn’t matter. How many ways can I order (n-r) people? Therefore, the n! is counting each winning possibility of the top r and extra (n-r)! times. So, the answer is n!/(n-r)!
50
Alan’s “Division Rule” If the things you are trying to count are really identical-sized groupings of things from a larger set, you can divide the size of the larger set by the size of the groupings. E.g., if I want to know how many cars drove over a sensor strip, I can divide the number of sensor inputs by 2. Why? Steve also uses this but didn’t think to name it after himself. Drat! It’s often handy to think about this type of “overcounting”.
51
Division Rule and Combinations Suppose the 5 prizes are identical, so I don’t care to distinguish who gets which prize, just who the winners are. (This is usually phrased as choosing a committee of 5 out of the class of 20.)
52
Division Rule and Combinations Suppose the 5 prizes are identical, so I don’t care to distinguish who gets which prize, just who the winners are. I already know how many ways to get 5 winners with distinct prizes: P(n,r) But if the prizes are the same, then any re-arranging of those 5 should only be counted once. So, I’m grouping together cases when the same 5 people get different prizes. How many cases are there?
53
Division Rule and Combinations Suppose the 5 prizes are identical, so I don’t care to distinguish who gets which prize, just who the winners are. I already know how many ways to get 5 winners with distinct prizes: P(20,5) But if the prizes are the same, then any re-arranging of those 5 should only be counted once. So, I’m grouping together cases when the same 5 people get different prizes. How many cases are there? 5! Therefore, answer is P(20,5)/5!
54
Division Rule and Combinations Suppose the r prizes are identical, so I don’t care to distinguish who gets which prize, just who the winners are. I already know how many ways to get r winners with distinct prizes: P(n,r) But if the prizes are the same, then any re-arranging of those r should only be counted once. So, I’m grouping together cases when the same r people get different prizes. How many cases are there? r! Therefore, answer is P(n,r)/r!
55
Combinations C(n,r) These are called “combinations” – the number of ways of choosing r items out of n distinct items, where the order of those r doesn’t matter. Pronounced “n choose r”. AKA “binomial coefficients”
56
Binomial Coefficients? If I compute (x+1)^n, what’s the coefficient of x^r (x+1)(x+1)(x+1)…(x+1) It’s exactly C(n,r)
57
Dealing with Overlapping Cases Suppose we try to compute population of the Americas by adding up the population of “North America” (defined by NAFTA as US, Canada, and Mexico) plus “Latin America” defined as countries that are predominantly Spanish- or Portuguese-speaking. What’s wrong? How do we fix it?
58
Dealing with Overlapping Cases Suppose we try to compute population of the Americas by adding up the population of “North America” (defined by NAFTA as US, Canada, and Mexico) plus “Latin America” defined as countries that are predominantly Spanish- or Portuguese-speaking. What’s wrong? How do we fix it? Subtract the population of Mexico!
59
Dealing with Overlapping Cases This goes by the fancy name “inclusion-exclusion principle”, but it’s just basic sets:
60
Old Counting Slides These are for practice and your reference. (BTW, on my computer, many of the set symbols aren’t showing up properly. Most of them are the set “is a member of” symbol.)
61
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 61
62
In CS, we often encounter situations where we want to count the number of possible outcomes of an event. For example, we may wish to determine: the number of possible paths to follow in a directed network (graph), the number of possible 5-8 character passwords, etc.
63
Suppose that the customers of a bank are asked to use 3-digit PINs to protect their accounts when using the bank’s ATM machines. If the other constraints are: no 2 digits can be the same, and the only allowable digits are 1, 2, 3, & 4, how many PINs are possible? (a) Describe the problem in mathematical notation.
64
(b) Model the problem using a tree diagram.
65
Multiplication Principle (Product Rule) If an operation consists of t steps and: Step 1 can be performed in n 1 ways, Step 2 can be performed in n 2 ways, … Step t can be performed in n t ways, then the entire operation can be performed in n 1 n 2 …n t different ways. Example: How many postal codes begin with the letter V and end with the digit 4? __ __ __
66
Example: Use the multiplication principle to prove that the number of subsets of a set containing n elements is 2 n.
67
Example: Suppose a programming language requires you to define variable names (identifiers) using exactly 3 different upper case characters. How many different identifiers contain either an A or a B, but not both?
68
Example: Let X be a set with n elements. How many ordered pairs (A, B) satisfy all of these constraints: A ⊆ X, B ⊆ X, and A ∩ B = ∅ ?
69
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 69
70
Addition Principle (Sum Rule) If X 1, X 2, …, X t are pairwise disjoint sets (i.e., X i ∩ X j = ∅ ∀ i ∀ j s.t. i, j ∈ {1, 2, …, t }, i ≠ j), then the number of ways to select an element from any of X 1 or X 2 or … or X t is: | X 1 | + | X 2 | + … + | X t |
71
Example: Suppose a user is asked to create an alphanumeric password consisting of at least 6 characters, but no more than 8 characters. How many different passwords are possible?
72
Back-of-the-envelope calculation. For the previous example, roughly how long would it take for a program to perform a brute-force attack to discover a user’s password, if we assume (guess?) that a million passwords can be tested each second? Best case? Average case? Worst case? Side thought: Common passwords (when “128-bit” security is no better than 10-bit or 20-bit security).
73
Example: Suppose a user is asked to create an alphanumeric password consisting of at least 6 characters, but no more than 8 characters, but this time, at least 1 of the chars. has to be a number. How many possible combinations are there this time?
74
Example: Suppose a user is asked to create an alphanumeric password consisting of at least 6 characters, but no more than 8 characters, but this time, at least 1 of the chars. has to be a number. How many possible combinations are there if we know users are “minimally complex” in their password choices?
75
Example: Suppose a user is asked to create an alphanumeric password consisting of at least 6 characters, but no more than 8 characters, but this time, at least 1 of the chars. has to be a number. Now, will the passwords be more secure or less secure?
76
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 76
77
Inclusion-Exclusion Principle If an object can be found in 2 or more sets at the same time, then we cannot use the addition principle. Why not? If A and B are sets, then the total number of elements in either A or B is given by: | A ∪ B | = | A | + | B | – | A ∩ B | Example using a Venn Diagram:
78
Example: How many 8-bit strings either start with “1” or end with “00”?
79
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 79
80
Permutations & Combinations How many different outcomes are there if you choose r balls from a barrel containing n different balls?
81
Example: r = 3 Barrel = { A,B,C,D } Order matters ABC ≠ CAB Order doesn’t ABC = CAB Repetition not OK e.g., AAB is not counted ABC, ABD, ACB, ACD, ADB, ADC, BAC, BAD, BCA, BCD, BDA, BDC, CAB, CAD, CBA, CBD, CDA, CDB, DAB, DAC, DBA, DBC, DCA, DCB r-permutations ABC, ABD, ACD, BCD r-combinations Repetition OK e.g., AAB is counted AAA, AAB, AAC, AAD, ABA, ABB, ABC, ABD, … r-permutations w/ rep. AAA, AAB, AAC, AAD, ABB, ABC, ABD, ACC, ACD, ADD, BBB, BBC, BBD, BCC, BCD, BDD, CCC, CCD, CDD, DDD r-combinations w/ rep.
82
(1) r-Permutations (Order matters, and repetition is not allowed.) An r-permutation of n distinct elements x 1, x 2, …, x n is an ordering of an r-element subset of {x 1, x 2, …, x n }. The number of r-permutations is: P(n, r) = n (r) = Derivation:
83
Example 1: In how many ways can 5 electoral candidates finish in an election, assuming no ties? Example 2: In how many ways can 7 girls and 3 boys line up, if the boys must stand next to each other?
84
Example 3: Suppose an operating system has a queue of 3 low priority and 5 high priority processes ready to run. In how many ways could these processes be ordered for execution if 2 low priority processes are not allowed to be executed back to back?
85
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 85
86
(2) r-Combinations (Order doesn’t matter, and repetition is not allowed.) An r-combination of n distinct elements x 1, x 2, …, x n is an r- element subset of {x 1, x 2, …, x n }. The number of r-combinations is: C(n, r) = ( ) = Example 4: A donut shop has 10 kinds of donuts. In how many ways can 6 distinct kinds of donuts be selected? Example 5: Show how to derive a relationship between r- permutations and r-combinations.
87
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 87
88
(3) r-Permutations with Repetition (Generalized r- Permutations) Here, order matters, and repetition is allowed. Suppose we have a set of n distinct elements x 1, x 2, …, x n and we select a sequence of r elements, allowing repeats. How many different sequences are possible?
89
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 89
90
(4) r-Combinations with Repetition (Generalized r- Combinations) Here, order doesn’t matter, and repetition is allowed. Suppose we have a set of n distinct elements x 1, x 2, …, x n. The number of unordered r-element selections from this set, with repetition allowed, is:
91
Example 8: If a donut shop sells 3 kinds of donuts: plain, glazed, and jelly, then how many possible selections of 7 donuts are there?
92
Summary of Formulas Order Matters & Repetition is not Allowed Order does not Matter & Repetition is not Allowed Order Matters & Repetition is Allowed Order does not Matter & Repetition is Allowed nrnr n (r) n-1 n ( ) r r+n-1 ( )
93
Today’s Outline Multiplication Principle Addition Principle Inclusion/Exclusion Principle Combinations and Permutations –r-permutations, no repetition –r-combinations, no repetition –r-permutation, with repetition –r-combinations, with repetition –variants 93
94
Permutations of Indistinguishable Objects Multinomial Theorem: The number of different permutations of n objects where there are: n 1 indistinguishable type 1 objects n 2 indistinguishable type 2 objects … n k indistinguishable type k objects and n 1 + n 2 + … + n k = n is: Proof:
95
Example 9: How many strings can be made by reordering the letters PEPPER?
96
Theorem: The number of ways to place n distinguishable objects into r distinguishable boxes so that n i objects are placed into box i (where i = 1, 2, …, r and n 1 + n 2 + … + n r = n) is: Example 10: How many ways are there to distribute hands of 5 cards to each of 4 players from a deck of 52 cards?
97
Home Exercise #1: How many non-negative integer solutions are there to the equation: x 1 + x 2 + x 3 + x 4 = 10 ? Home Exercise #2: How would your answer to Home Exercise #1 change if we had these constraints on the number of x i ’s: x 1 ≥ 1, x 2 ≥ 2, x 3 ≥ 0, and x 4 ≥ 0 ? (Hint: what if y 1 = x 1 - 1? What constraint is there on y 1 ?) Home Exercise #3: How many times is the following print statement executed, if n = 4? for i 1 = 1 to n for i 2 = 1 to i 1 for i 3 = 1 to i 2 print i 1, i 2, i 3
98
Learning Goals After this unit, you should be able to: Apply counting principles to determine the number of arrangements or orderings of discrete objects, with or without repetition, and given various constraints. Use appropriate mathematical constructs to express a counting problem (e.g. counting passwords with various restrictions placed on the characters within). Identify problems that can be expressed and solved as a combination of smaller sub problems. When necessary, use decision trees to model more complex counting problems. 98
99
Coming Up Final Exam! Yay! 99
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.