Docsity

Counting Techniques, Lecture Notes - Computer Science, Study notes of Algorithms and Programming

Partial preview of the text

Download Counting Techniques, Lecture Notes - Computer Science and more Study notes Algorithms and Programming in PDF only on Docsity!

Counting Techniques

Zeph Grunschlag

Agenda

Section 4.1: Counting Basics

 Sum Rule

 Product Rule

 Inclusion-Exclusion

Section 4.

 Basic pigeonhole principle

 Generalized pigeonhole principle

Counting Basics

Set Cardinalities

Interested in answering questions such as:

How many bit strings of length n are there?

How many ways are there to buy 13 different bagels from a shop that sells 17 types? How many bit strings of length 11 contain a streak of one type of bit of exact length 7? How many ways can a dating service match 13 men to 17 women?

COMMON THEME: convert to set cardinality problems so each question above is a about counting the number of elements in some set:

Q: What is the corresponding set in each case?

L17 5

Counting Basics

Set Cardinalities

A: The set to measure the cardinality of is…

How many bit strings of length n are there?

 {bit strings of length n} How many ways are there to buy 13 different bagels from a shop that sells 17 types?

 {S  {1, 2, … 17} | |S | = 13 } (…arguable)

How many bit strings of length 11 contain a streak of one type of bit of exact length 7?  {length 11 bit strings with 0-streak of length 7}  {length 11 bit strings with 1-streak of length 7} How many ways to match 13 M to 17 W?

 { f :{1,2,…,13}  {1,2,…,17} | f is 1-to-1}

Product Rule

A: 2 n.

Proof : Let S = {bit strings of length n}.

S is in 1-to-1 correspondence with

Consequently the product rule implies:

times

B   B   B   B where B 

n

n n

S

B B B B B

B B B B

Cardinality of Power Set

THM: |P ({1,2,3,…, n})| = 2^ n

Proof. The set of bit strings of length

n is in 1-to-1 correspondence with the

P({1,2,3,…, n}) since subsets are

represented by length n bit strings.•

Sum Rule

Next the number of length 11 bit strings

with a streak of length exactly 7.

Q: Which of the following should be

counted:

No!, longest streak has length 2.

2. 01101111010 11 No! Too long.

3. 1 0000000 011 No! Streak too long.

4. 1 0000000 101 Yes!

5. 0 1111111 010 Yes!

Sum Rule

We are trying to compute the cardinality

of:

{length 11 bit strings with 0-streak of length 7}

{length 11 bit strings with 1-streak of length 7}

Call the first set A and the second set B.

Q: Are A and B disjoint?

Sum Rule

Break up

A = {length 11 bit strings with

0-streak of length exactly 7}

into more cases and use sum rule:

A 1 = {00000001**} ( is either 0 or 1)

A 2 = {100000001**}

A 3 = {100000001}

A 4 = {**100000001}

A 5 = {***10000000}.

Apply sum rule:

|A| = |A 1 | +|A 2 | +|A 3 | +|A 4 | +|A 5 |

L17 14

Sum Rule

So let’s count each set.

A 1 = {00000001***}. There are 3 *’s, each with

2 choices, so product rule gives |A 1 | = 2^3 = 8

A 2 = {100000001**}. There are 2 *’s. Therefore,

|A 2 | = 2^2 = 4

A 3 = {100000001}, A 4 = {**100000001}

Similarly: |A 2 | = |A 3 | = |A 4 | = 4

A 5 = {***10000000}.

|A 1 | = |A 5 | = 8

|A| = |A 1 | +|A 2 | +|A 3 | +|A 4 | +|A 5 | =

Therefore answer is 56.

Counting Functions

A: The number of 1-to-1 functions from

a size k set to a size n set is

n! / (n - k)!

As long as k is no larger than n. If k > n

there are no 1-to-1 functions.

Q: How about general functions from

size k sets to size n sets?

Counting Functions

A: The number of functions from a size k

set to a size n set is

n k

L17 19

Inclusion-Exclusion

Visualize.

Diagram

gives

proof

Inclusion-

Exclusion principle:

A-A  B

U

A  B B-A  B

| | | | | |

(| | | |) (| | | |) | |

| | | | | | | |

A B A B

A A B A B B A B A B A B

A B A A B A B B A B

   

           

        

Blackboard Exercises for

Section 4.

Problem 4.1.19 (a,b,c,f). How many positive integers with exactly three decimal digits are…  …divisible by 7?  …odd?  …have the same 3 digits?  …are not divisible by 3 or 4? Problem 4.1.49. How many ways can the letters a,b,c,d be arranged such that a is not immediately followed by b? Link to worked out problems