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

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

Embed Size (px)

Citation preview

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

CSE 221: Algorithms and Data Structures

Lecture #10Counting: Easy as 1, 2, C(3,1)

Steve Wolfman

2009W1

1

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

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

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

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

– variants3

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

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.

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

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.

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

(b) Model the problem using a tree diagram.

 

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

Multiplication Principle (Product Rule)

If an operation consists of t steps and:

Step 1 can be performed in n1 ways,

Step 2 can be performed in n2 ways,

Step t can be performed in nt ways,

then the entire operation can be performed in n1n2…nt different ways.

Example: How many postal codes begin with the letter V and end with the digit 4?

__ __ __ __ __ __

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

Example:

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

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

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?

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

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

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

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

– variants11

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

Addition Principle (Sum Rule)

If X1, X2, …, Xt are pairwise disjoint sets (i.e., Xi ∩ Xj = ∅ ∀i ∀j s.t. i, j∈{1, 2, …, t }, i ≠ j), then the number of ways to select an element from any of X1 or

X2 or … or Xt is:

| X1 | + | X2 | + … + | Xt |

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

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?

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

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

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

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?

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

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

– variants16

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

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:

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

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

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

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

– variants19

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

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

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

Example: r = 3Barrel = {A,B,C,D}

Order mattersABC ≠ CAB

Order doesn’tABC = CAB

Repetition not OKe.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 OKe.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.

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

(1) r-Permutations (Order matters, and repetition is not allowed.)

An r-permutation of n distinct elements x1, x2, …, xn is an ordering of an r-element subset of {x1, x2, …, xn}. The number of r-permutations is:

P(n, r) = n(r) =

Derivation:

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

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?

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

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?

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

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

– variants25

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

(2) r-Combinations (Order doesn’t matter, and repetition is not allowed.)An r-combination of n distinct elements x1, x2, …, xn is an r-element subset of {x1, x2, …, xn}. 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.

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

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

– variants27

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

(3) r-Permutations with Repetition (Generalized r-Permutations)Here, order matters, and repetition is allowed.

Suppose we have a set of n distinct elements x1, x2, …, xn and we select a sequence of r elements, allowing repeats. How many different sequences are possible?

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

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

– variants29

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

(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 x1, x2, …, xn. The number of unordered r-element selections from this set, with repetition allowed, is:

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

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?

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

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

nr

n(r)

n-1

n( )r

r+n-1( )

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

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

– variants33

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

Permutations of Indistinguishable Objects

Multinomial Theorem: The number of different permutations of n objects where there are:n1 indistinguishable type 1 objects

n2 indistinguishable type 2 objects

nk indistinguishable type k objects

and n1 + n2 + … + nk = n is:

Proof:

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

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

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

Theorem: The number of ways to place n distinguishable objects into r distinguishable boxes so that ni objects are placed into box i (where i = 1, 2, …, r and n1 + n2 + … + nr = 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?

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

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

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

To Do

• Finish Project #4• Finish HW #3• Read Epp 6.1-6.5

39

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

Coming Up

• Final Exam! Yay!

40