Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 221: Algorithms and Data Structures Lecture #10 Counting: Easy as 1, 2, C(3,1) Steve Wolfman 2009W1 1.

Similar presentations


Presentation on theme: "CSE 221: Algorithms and Data Structures Lecture #10 Counting: Easy as 1, 2, C(3,1) Steve Wolfman 2009W1 1."— Presentation transcript:

1 CSE 221: Algorithms and Data Structures Lecture #10 Counting: Easy as 1, 2, C(3,1) Steve Wolfman 2009W1 1

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 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 3

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

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

6 (b) Model the problem using a tree diagram.

7 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? __ __ __

8 Example: Use the multiplication principle to prove that the number of subsets of a set containing n elements is 2 n.

9 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?

10 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 = ∅ ?

11 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 11

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 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?

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

15 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. (a) From a social engineering perspective, do you expect the password to be more secure, or less secure? (b) How many possible combinations are there this time?

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

17 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:

18 Example: How many 8-bit strings either start with “1” or end with “00”?

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

20 Permutations & Combinations How many different outcomes are there if you choose r balls from a barrel containing n different balls?

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

22 (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:

23 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?

24 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?

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

26 (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.

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

28 (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?

29 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 29

30 (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:

31 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?

32 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 ( )

33 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 33

34 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:

35 Example 9: How many strings can be made by reordering the letters PEPPER?

36 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?

37 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 ? 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

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

39 To Do Finish Project #4 Finish HW #3 Read Epp 6.1-6.5 39

40 Coming Up Final Exam! Yay! 40


Download ppt "CSE 221: Algorithms and Data Structures Lecture #10 Counting: Easy as 1, 2, C(3,1) Steve Wolfman 2009W1 1."

Similar presentations


Ads by Google