1 copyright m.r.k. krishna rao 2003 ch 4. counting techniques counting techniques are important in...

81
1 Copyright M.R.K. Krishna Rao 2003 Ch 4. Counting Ch 4. Counting Techniques Techniques Counting techniques are important in Counting techniques are important in programming design. programming design. EG: How large an array EG: How large an array or or hash table hash table or or heap is needed? heap is needed? EG: What is the average case complexity EG: What is the average case complexity of quick-sort? of quick-sort? Answers depend on being able to count. Answers depend on being able to count. Counting is useful in computing the Counting is useful in computing the probability of discrete events. probability of discrete events. EG: What is the probability of winning EG: What is the probability of winning a lottery? a lottery?

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

1Copyright M.R.K. Krishna Rao 2003

Ch 4. Counting Ch 4. Counting TechniquesTechniques

Counting techniques are important in Counting techniques are important in programming design.programming design.

EG: How large an array EG: How large an array oror hash table hash table oror heap heap is needed?is needed?

EG: What is the average case complexity of EG: What is the average case complexity of quick-sort?quick-sort?

Answers depend on being able to count. Answers depend on being able to count.

Counting is useful in computing the probability Counting is useful in computing the probability of discrete events.of discrete events.

EG: What is the probability of winning a EG: What is the probability of winning a lottery?lottery?

2Copyright M.R.K. Krishna Rao 2003

Counting BasicsCounting BasicsSet CardinalitiesSet Cardinalities

Interested in answering questions such as:Interested in answering questions such as: How many bit strings of length How many bit strings of length nn are there? are there? How many ways are there to buy 13 different How many ways are there to buy 13 different

bagels from a shop that sells 17 types?bagels from a shop that sells 17 types?How many bit strings of length 11 contain a How many bit strings of length 11 contain a

streak of one type of bit of exact length 7?streak of one type of bit of exact length 7?How many ways can a marriage service match How many ways can a marriage service match

13 men to 17 women?13 men to 17 women?COMMON THEME: convert to set cardinality COMMON THEME: convert to set cardinality

problems so each question above is a about problems so each question above is a about counting the number of elements in some counting the number of elements in some set:set:

Q: What is the corresponding set in each case?Q: What is the corresponding set in each case?

3Copyright M.R.K. Krishna Rao 2003

Counting BasicsCounting BasicsSet CardinalitiesSet Cardinalities

A: The set to measure the cardinality of is…A: The set to measure the cardinality of is…

How many bit strings of length How many bit strings of length nn are there? are there?– {bit strings of length {bit strings of length nn}}

How many ways are there to buy 13 different bagels How many ways are there to buy 13 different bagels from a shop that sells 17 types?from a shop that sells 17 types?

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

How many bit strings of length 11 contain a streak of How many bit strings of length 11 contain a streak of one type of bit of exact length 7?one type of bit of exact length 7?– {length 11 bit strings with 0-streak of length 7} {length 11 bit strings with 0-streak of length 7}

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

How many ways to match 13 M to 17 W ?How many ways to match 13 M to 17 W ?

– { { ff :{1,2,…,13} :{1,2,…,13} {1,2,…,17} {1,2,…,17} | | ff is 1-to-1} is 1-to-1}

4Copyright M.R.K. Krishna Rao 2003

Basic Counting PrinciplesBasic Counting Principles

The sum rule:The sum rule:If a task can be done in nIf a task can be done in n11 ways and a second ways and a second task in ntask in n22 ways, and if these ways, and if these two tasks cannot two tasks cannot be done at the same timebe done at the same time, then there are n, then there are n11 + n+ n22 ways to do EITHER task. ways to do EITHER task.

Example:Example: The department will award a free computer to The department will award a free computer to either a ICS student or a ICS professor. either a ICS student or a ICS professor. How many different choices are there, if there How many different choices are there, if there are 530 students and 45 professors?are 530 students and 45 professors?

There are 530 + 45 = 575 choices.There are 530 + 45 = 575 choices.

5Copyright M.R.K. Krishna Rao 2003

Basic Counting PrinciplesBasic Counting Principles

Generalized sum rule:Generalized sum rule:

If we have tasks TIf we have tasks T11, T, T22, …, T, …, Tmm that can be done in that can be done in nn11, n, n22, …, n, …, nmm ways, respectively, and no two of ways, respectively, and no two of these tasks can be done at the same time, then these tasks can be done at the same time, then there are nthere are n11 + n + n22 + … + n + … + nmm ways to do ONE of ways to do ONE of these tasks.these tasks.

Example:Example: A student can choose a computer project from A student can choose a computer project from one of the three lists. The three lists contain 23, one of the three lists. The three lists contain 23, 15, and 19 possible projects, respectively. How 15, and 19 possible projects, respectively. How many possible projects are there to choose from?many possible projects are there to choose from?

There are 23 + 15 + 19 = 57 projects.There are 23 + 15 + 19 = 57 projects.

6Copyright M.R.K. Krishna Rao 2003

Basic Counting PrinciplesBasic Counting Principles

The product rule:The product rule:Suppose that a procedure can be broken down into Suppose that a procedure can be broken down into two successive tasks. If there are ntwo successive tasks. If there are n11 ways to do the ways to do the first task and nfirst task and n22 ways to ways to do the second task after do the second task after the first task has been donethe first task has been done, then there are n, then there are n11nn22 ways to do the procedure (BOTH the tasks).ways to do the procedure (BOTH the tasks).

Example: Example: There are 32 computers in a lab. Each There are 32 computers in a lab. Each computer has 24 ports. How many different ports to computer has 24 ports. How many different ports to a computer in the lab are there?a computer in the lab are there?

Solution: Solution: There are 32 possibilities to pick the There are 32 possibilities to pick the computer and 24 ports to choose on that computer. computer and 24 ports to choose on that computer. So there are 32So there are 3224 = 768 different ports.24 = 768 different ports.Exercise: Exercise: A student brings 5 shirts and 4 pairs of A student brings 5 shirts and 4 pairs of pants to campus. How many outfits can he wear?pants to campus. How many outfits can he wear?

7Copyright M.R.K. Krishna Rao 2003

Basic Counting PrinciplesBasic Counting Principles

Generalized product rule:Generalized product rule:If we have a procedure consisting of sequential If we have a procedure consisting of sequential tasks Ttasks T11, T, T22, …, T, …, Tmm that can be done in n that can be done in n11, n, n22, …, n, …, nmm ways, respectively, then there are nways, respectively, then there are n11 n n22 … … n nmm ways to carry out the procedure.ways to carry out the procedure.

Example: Example: How many different license plates are there How many different license plates are there if each plate consists of 3 English letters and 3 digits?if each plate consists of 3 English letters and 3 digits?Solution: Solution: There are 26 possibilities to pick each letter and There are 26 possibilities to pick each letter and 10 possibilities to pick each digit. So there are 10 possibilities to pick each digit. So there are 2626262626.10.10.10 = 17,576,000 different license plates.26.10.10.10 = 17,576,000 different license plates.

Exercise: Exercise: A Chinese restaurant serves 3 soups, 5 A Chinese restaurant serves 3 soups, 5 meat entrees and 6 vegetables. A regular priced meal meat entrees and 6 vegetables. A regular priced meal consists of one soup, one meat, and one vegetable. consists of one soup, one meat, and one vegetable. How many different regular meals could one order?How many different regular meals could one order?

8Copyright M.R.K. Krishna Rao 2003

Basic Counting PrinciplesBasic Counting Principles

The sum and product rules can also be phrased in The sum and product rules can also be phrased in terms of terms of set theoryset theory..

Sum rule:Sum rule: Let A Let A11, A, A22, …, A, …, Amm be disjoint sets. Then be disjoint sets. Then the number of ways to choose an element from the number of ways to choose an element from anyone of these sets is |Aanyone of these sets is |A11 A A22 … … A Amm | = | =|A|A11| + |A| + |A22| + … + |A| + … + |Amm|.|.

Product rule:Product rule: Let A Let A11, A, A22, …, A, …, Amm be finite sets. be finite sets. Then the number of ways to choose one element Then the number of ways to choose one element from each set in the order Afrom each set in the order A11, A, A22, …, A, …, Am m is is |A|A11 A A22 … … A Amm | = |A | = |A11| | |A |A22| | … … |A |Amm|.|.

9Copyright M.R.K. Krishna Rao 2003

Cardinality of Power SetCardinality of Power Set

Q: How many bit strings of length Q: How many bit strings of length nn are are there?there?

A: 2A: 2nn

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

ProofProof . The set of bit strings of length . The set of bit strings of length

n n is in 1-to-1 correspondence with the is in 1-to-1 correspondence with the PP({1,2,3,…,({1,2,3,…,nn}) since subsets are }) since subsets are represented by length represented by length nn bit strings. � bit strings. �

10Copyright M.R.K. Krishna Rao 2003

Sum and Product Rules togetherSum and Product Rules together

In general, The Sum Rule and The Product Rule should be used in combination.

Example: Example: In how many ways you can travel from A to C??

Solution: Solution: 2 possibilities: 2 possibilities: (a)(a) Go thru B orGo thru B or(b)(b) Not go thru BNot go thru B

A

C

BR1

R2

R3

R4

R5

R6

R7

Use product rule Use product rule to compute # of ways to do (a). to compute # of ways to do (a). 3.2=63.2=6

There are 2 There are 2 ways to do (b) –- ways to do (b) –- R6 and R7.Use sum rule Use sum rule to compute # of ways to do (a) or (b). to compute # of ways to do (a) or (b). 6+2=86+2=8

11Copyright M.R.K. Krishna Rao 2003

Sum and Product Rules togetherSum and Product Rules together

Another example: Another example: the number of length 11 the number of length 11 bit strings with a streak of length exactly 7.bit strings with a streak of length exactly 7.

Q: Which of the following should be counted:Q: Which of the following should be counted:

1001100101010011001010

01101111010110110111101011

1000000001110000000011

1000000010110000000101

0111111101001111111010

12Copyright M.R.K. Krishna Rao 2003

Sum and Product Rules togetherSum and Product Rules together

Another example:Another example: the number of length 11 the number of length 11 bit strings with a streak of length exactly 7.bit strings with a streak of length exactly 7.

Q: Which of the following should be counted:Q: Which of the following should be counted:

1001100101010011001010 No!, longest streak has length 2.No!, longest streak has length 2.

011011110100110111101011 11 No! Too long.No! Too long.

1100000000000000011 No! Streak too long.011 No! Streak too long.

1100000000000000101101 Yes!Yes!

0011111111111111010010 Yes!Yes!

13Copyright M.R.K. Krishna Rao 2003

Sum RuleSum Rule

We are trying to compute the cardinality of:We are trying to compute the cardinality of:{length 11 bit strings with 0-streak of length 7}{length 11 bit strings with 0-streak of length 7}

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

Call the first set Call the first set AA and the second set and the second set BB. .

Q: Are Q: Are AA and and B B disjoint?disjoint?

14Copyright M.R.K. Krishna Rao 2003

Sum RuleSum Rule

A: Yes. If had both a 0-streak and a 1-streak A: Yes. If had both a 0-streak and a 1-streak of length 7 each, string would have length of length 7 each, string would have length at least 14! at least 14!

When counting the cardinality of a disjoint When counting the cardinality of a disjoint union we use:union we use:

SUM RULE: If SUM RULE: If AA and and BB are disjoint, then are disjoint, then

||AA BB| = || = |AA|+||+|BB||

By symmetry, in our case By symmetry, in our case AA and and B B have the have the same cardinality. Therefore the answer same cardinality. Therefore the answer would be 2|would be 2|AA|.|.

15Copyright M.R.K. Krishna Rao 2003

Sum RuleSum Rule

Break upBreak upA A = {length 11 bit strings with = {length 11 bit strings with

0-streak of length exactly 7}0-streak of length exactly 7}into more cases and use sum rule:into more cases and use sum rule:

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

AA2 2 = {100000001**}= {100000001**}

AA3 3 = {*100000001*}= {*100000001*}

AA4 4 = {**100000001}= {**100000001}

AA5 5 = {***10000000}. = {***10000000}. Apply sum rule:Apply sum rule:

||AA| = || = |AA11|| +|+|AA22|| +|+|AA33|| +|+|AA44|| +|+|AA55||

16Copyright M.R.K. Krishna Rao 2003

Sum and Product Rules togetherSum and Product Rules together

So let’s count each set.So let’s count each set.

AA1 1 = {00000001***}. There are 3 *’s, each with 2 = {00000001***}. There are 3 *’s, each with 2 choices, so product rule gives |choices, so product rule gives |AA11| = 2| = 23 3 = 8= 8

AA2 2 = {100000001**}. There are 2 *’s. Therefore, = {100000001**}. There are 2 *’s. Therefore, ||AA22| = 2| = 22 2 = 4= 4

AA3 3 = {*100000001*}, = {*100000001*}, AA4 4 = {**100000001}= {**100000001}

Similarly: |Similarly: |AA22|| = |= |AA33|| = |= |AA44| = 4| = 4

AA5 5 = {***10000000}. = {***10000000}.

||AA11|| = |= |AA55| = 8| = 8

||AA| = || = |AA11|| +|+|AA22|| +|+|AA33|| +|+|AA44|| +|+|AA55| = 8+4+4+4+8 = | = 8+4+4+4+8 = 28.28.

Therefore answer is Therefore answer is 5656..

17Copyright M.R.K. Krishna Rao 2003

Sum and Product Rules togetherSum and Product Rules together

Another example:Another example: Each user on a computer system has Each user on a computer system has a password, which is six to eight characters long, a password, which is six to eight characters long, where each character is an uppercase letter or a digit. where each character is an uppercase letter or a digit. Each password must contain at least one digit. How Each password must contain at least one digit. How many possible passwords are there?many possible passwords are there?

Solution:Solution: Let P be the total number of possible passwords, and let P66, P77, and P88 denote the number of possible passwords of length 6; 7, and 8, respectively.

• By the sum rule, P = P66 + P77 + P88.• By the product rule,

– the number of strings of six characters is 3666, and– the number of strings with no digits is 2666. – Hence, P66 = 366 6 – 2666.

• Similarly, P77 = 367 7 – 2677, and P88 = 368 8 – 2688.• Consequently, P = P66 + P77 + P88 = 2,684,483,063,360

18Copyright M.R.K. Krishna Rao 2003

Counting FunctionsCounting Functions

How many ways to match 13M to 17W ?How many ways to match 13M to 17W ?

{ { ff :{1,2,…,13} :{1,2,…,13} P {1,2,…,17} P {1,2,…,17} | | ff is 1-to-1} is 1-to-1}

Use product rule thoughtfully.Use product rule thoughtfully.17 possible output values for 17 possible output values for f f (1)(1)16 values remain for 16 values remain for f f (2)(2)…………………………………………………………

17- 17- i i + 1 values remain for + 1 values remain for f f ((i i ) ) …………………………………………………………

17-13+1=5 values remain for 17-13+1=5 values remain for f f (13)(13)ANS: 17·16·15 ·14 ·… ·7·6·5 = 17! / 4!ANS: 17·16·15 ·14 ·… ·7·6·5 = 17! / 4!Q: In general how many 1-to-1 functions Q: In general how many 1-to-1 functions

from size from size kk to size to size n n set?set?

19Copyright M.R.K. Krishna Rao 2003

Counting FunctionsCounting Functions

A: The number of 1-to-1 functions from a A: The number of 1-to-1 functions from a size size kk set to a size set to a size n n set isset is

n n ! / (! / (n n - - kk)) !!

as long as as long as k k is no larger than is no larger than nn. If . If k k > > n n there are no 1-to-1 functions.there are no 1-to-1 functions.

Q: How about general functions from size Q: How about general functions from size k k sets to size sets to size nn sets? sets?

20Copyright M.R.K. Krishna Rao 2003

Counting FunctionsCounting Functions

A: The number of functions from a size A: The number of functions from a size kk set set to a size to a size n n set isset is

n n kk

21Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

The principle of Inclusion-Exclusion The principle of Inclusion-Exclusion generalized the sum rule the the case of generalized the sum rule the the case of non-empty intersection:non-empty intersection:

INCLUSION-EXCLUSION: If INCLUSION-EXCLUSION: If AA and and BB are sets, are sets, then then

||AA B B | = || = |AA|+||+|B B |- ||- |A A B B ||

This says that when counting all the This says that when counting all the elements in elements in AA or or BB, if we just add the the , if we just add the the sets, we have double-counted the sets, we have double-counted the intersection, and must therefore subtract it intersection, and must therefore subtract it out.out.

22Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

Visualize.Visualize.

This diagramThis diagram

gives proofgives proof

Inclusion-Inclusion-

Exclusion principle:Exclusion principle:

A-AB

U

AB

B-AB

||||||

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

|||||| ||

BABA

BABABABBABAA

BABBABAABA

23Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

Example: Example: 250 students take Java programming, 250 students take Java programming, 180 students take discrete math and 60 students 180 students take discrete math and 60 students take both. How many students are taking either take both. How many students are taking either Java programming or discrete math?Java programming or discrete math?

Solution:Solution: Let AA11 be a set of students taking Java students taking Java programming and programming and AA22 be a set of students taking students taking discrete math. Adiscrete math. A11 A A2 2 is the set of students taking students taking either Java programming or discrete math.either Java programming or discrete math.

|A|A11 A A22| = |A| = |A11| + |A| + |A22| - |A| - |A11 A A22| = 250 + 180 - 60 = 370.| = 250 + 180 - 60 = 370.

Therefore, 370 students take either Java Therefore, 370 students take either Java programming or discrete math.programming or discrete math.

24Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

How many bit strings of length 8 either start with a How many bit strings of length 8 either start with a 1 or end with 00?1 or end with 00?

Task 1:Task 1: Construct a string of length 8 that starts Construct a string of length 8 that starts with a 1. with a 1. Let AA11 be a set of these bit strings.

There is one way to pick the first bit (1), There is one way to pick the first bit (1), two ways to pick the second bit (0 or 1),two ways to pick the second bit (0 or 1),two ways to pick the third bit (0 or 1),two ways to pick the third bit (0 or 1),......two ways to pick the eighth bit (0 or 1).two ways to pick the eighth bit (0 or 1).

Product rule:Product rule: Task 1 can be done in 1 Task 1 can be done in 12277 = 128 = 128 ways.ways.||AA11| = 2| = 27 7 = 128= 128

25Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

Task 2:Task 2: Construct a string of length 8 that ends Construct a string of length 8 that ends with 00. with 00. Let AA22 be a set of these bit strings.

There are two ways to pick the first bit (0 or 1), There are two ways to pick the first bit (0 or 1), two ways to pick the second bit (0 or 1),two ways to pick the second bit (0 or 1),......two ways to pick the sixth bit (0 or 1),two ways to pick the sixth bit (0 or 1),one way to pick the seventh bit (0), andone way to pick the seventh bit (0), andone way to pick the eighth bit (0).one way to pick the eighth bit (0).

Product rule:Product rule: Task 2 can be done in 2 Task 2 can be done in 266 = 64 = 64 ways.ways.||AA22| = 2| = 26 6 = 64.= 64.

26Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

Since there are 128 ways to do Task 1 and 64 ways Since there are 128 ways to do Task 1 and 64 ways to do Task 2, does this mean that there are 192 bit to do Task 2, does this mean that there are 192 bit strings either starting with 1 or ending with 00 ?strings either starting with 1 or ending with 00 ?

No, because here Task 1 and Task 2 can be done No, because here Task 1 and Task 2 can be done at the same timeat the same time..

When we carry out Task 1 and create strings When we carry out Task 1 and create strings starting with 1, some of these strings end with 00.starting with 1, some of these strings end with 00.

Therefore, we sometimes do Tasks 1 and 2 at the Therefore, we sometimes do Tasks 1 and 2 at the same time, so same time, so the sum rule does not applythe sum rule does not apply but but the the principle of inclusion-exclusion principle of inclusion-exclusion can be can be applied.applied.

27Copyright M.R.K. Krishna Rao 2003

Inclusion-ExclusionInclusion-Exclusion

Principle of inclusion-exclusion:Principle of inclusion-exclusion: |A|A11 A A22| = |A| = |A11| + |A| + |A22| - |A| - |A11 A A22||

Compute |ACompute |A11 A A22|, the number of strings start |, the number of strings start with 1 with 1 andand end with 00? end with 00?

There is one way to pick the first bit (1), There is one way to pick the first bit (1), two ways for the second, …, sixth bit (0 or 1),two ways for the second, …, sixth bit (0 or 1),one way for the seventh, eighth bit (0).one way for the seventh, eighth bit (0).

Product rule:Product rule: |A |A11 A A22| = 2| = 255 = 32. = 32.

Therefore, |ATherefore, |A11 A A22| = 128 + 64 – 32 = 160.| = 128 + 64 – 32 = 160.

28Copyright M.R.K. Krishna Rao 2003

Tree DiagramsTree Diagrams

How many bit strings of length four do not have How many bit strings of length four do not have two consecutive 1s?two consecutive 1s?

Task 1Task 1 Task 2Task 2 Task 3Task 3 Task 4Task 4(1(1stst bit) bit) (2(2ndnd bit) bit) (3(3rdrd bit) bit) (4(4thth bit) bit)

00

0000

00

1111

0011 00 00

11

11 0000 00

1111

00

There are 8 strings.There are 8 strings.

29Copyright M.R.K. Krishna Rao 2003

Blackboard Example for Section 4.1Blackboard Example for Section 4.1

In a version of the computer language BASIC, In a version of the computer language BASIC, the name of a variable is a string of one or the name of a variable is a string of one or two alphanumeric characters, where two alphanumeric characters, where uppercase and lowercase are not uppercase and lowercase are not distinguished. (An alphanumeric character distinguished. (An alphanumeric character is either on of the 26 English letters of on of is either on of the 26 English letters of on of the 10 digits.) Moreover, a variable name the 10 digits.) Moreover, a variable name must begin with a letter and must be must begin with a letter and must be different from the five strings of two different from the five strings of two characters that are reserved for characters that are reserved for programming use. How many different programming use. How many different variable names are there in this version of variable names are there in this version of BASIC?BASIC?

30Copyright M.R.K. Krishna Rao 2003

Blackboard Exercises for Section Blackboard Exercises for Section 4.14.1

Problem 4.1.19 (a,b,c,f). How many Problem 4.1.19 (a,b,c,f). How many positive integers with exactly three positive integers with exactly three decimal digits are…decimal digits are…– ……divisible by 7?divisible by 7?– ……odd?odd?– ……have the same 3 digits?have the same 3 digits?– ……are not divisible by 3 or 4?are not divisible by 3 or 4?

Problem 4.1.49. How many ways can the Problem 4.1.49. How many ways can the letters a,b,c,d be arranged such that a letters a,b,c,d be arranged such that a is not immediately followed by b?is not immediately followed by b?

31Copyright M.R.K. Krishna Rao 2003

4.2 The Pigeonhole Principle4.2 The Pigeonhole Principle

The pigeonhole principle is the (rather The pigeonhole principle is the (rather obvious) statement:obvious) statement:

If there are If there are nn+1 pigeons, which must fit into +1 pigeons, which must fit into n n pigeonholes then some pigeonhole pigeonholes then some pigeonhole contains 2 or more pigeons.contains 2 or more pigeons.

Less obvious: Why this would ever be Less obvious: Why this would ever be useful… Principle often applied in useful… Principle often applied in surprising ways.surprising ways.

32Copyright M.R.K. Krishna Rao 2003

Pigeonhole PrinciplePigeonhole Principle

EG: Given 12 or more numbers between 0 and EG: Given 12 or more numbers between 0 and 1 inclusive, there are 2 (or more) numbers 1 inclusive, there are 2 (or more) numbers x,yx,y who are strictly within 0.1 of each other. who are strictly within 0.1 of each other.

ProofProof. Any pigeonhole principle application . Any pigeonhole principle application involves discovering who are the pigeons, involves discovering who are the pigeons, and who are the pigeonholes. In our case:and who are the pigeonholes. In our case:

Pigeons: The 12 numbersPigeons: The 12 numbers

Pigeonholes: The sets Pigeonholes: The sets

[0,0.1), [0.1,0.2), [0.2,0.3), … ,[0.9,1), {1}[0,0.1), [0.1,0.2), [0.2,0.3), … ,[0.9,1), {1}

There are 11 pigeonholes so some There are 11 pigeonholes so some x and yx and y fall fall in one of these sets, and have difference < in one of these sets, and have difference < 0.10.1

33Copyright M.R.K. Krishna Rao 2003

Pigeonhole PrinciplePigeonhole Principle

Harder Example: In a party of 2 or more Harder Example: In a party of 2 or more people, there are 2 people with the same people, there are 2 people with the same number of friends in the party. (Assuming number of friends in the party. (Assuming you can’t be your own friend and that you can’t be your own friend and that friendship is mutual.)friendship is mutual.)

ProofProof..

Pigeons –the Pigeons –the n n people (with people (with n n > 1).> 1).

Pigeonholes –the possible number of friends. Pigeonholes –the possible number of friends. I.e. the set {0,1,2,3,…I.e. the set {0,1,2,3,…nn-1}-1}

34Copyright M.R.K. Krishna Rao 2003

Pigeonhole PrinciplePigeonhole Principle

The proof proceeds with 2 cases.The proof proceeds with 2 cases.

There is a pigeonhole which isn’t hit. In that There is a pigeonhole which isn’t hit. In that case, left with case, left with n n -1 remaining -1 remaining pigeonholes, but pigeonholes, but nn pigeons, so done. pigeons, so done.

Every pigeonhole hit. In particular, the Every pigeonhole hit. In particular, the friendship numbers friendship numbers n-n-1 as well as 0 1 as well as 0 were hit. Thus someone is friends with were hit. Thus someone is friends with everyone while someone else is friends everyone while someone else is friends with no-one. This is a contradiction so with no-one. This is a contradiction so this case cannot happen! this case cannot happen!

��

35Copyright M.R.K. Krishna Rao 2003

Generalized Pigeonhole PrincipleGeneralized Pigeonhole Principle

If If NN objects are placed into objects are placed into kk boxes, there is at boxes, there is at least one box containing least one box containing N/k N/k objects.objects.

Specialize to Specialize to NN = = nn+1+1 and and kk = = n, n, gives at least gives at least ((nn+1)+1)/n /n = 2 objects in one box, which is = 2 objects in one box, which is the regular pigeonhole principle.the regular pigeonhole principle.

Q: Suppose that Tokyo has more than Q: Suppose that Tokyo has more than 7,000,000 inhabitants and that the human 7,000,000 inhabitants and that the human head contains at most 500,000 hairs. Find a head contains at most 500,000 hairs. Find a guaranteed minimum number of people in guaranteed minimum number of people in Tokyo that all have the same number of Tokyo that all have the same number of hairs on their heads.hairs on their heads.

36Copyright M.R.K. Krishna Rao 2003

Generalized Pigeonhole PrincipleGeneralized Pigeonhole Principle

A: A: 7,000,000 7,000,000 / / 500,001500,001 = 14= 14

Example:Example: If there are 11 players in a soccer If there are 11 players in a soccer team that wins 12-0, there must be at least team that wins 12-0, there must be at least one player in the team who scored at least one player in the team who scored at least twice.twice.

Example:Example: If you have 6 classes from Saturday If you have 6 classes from Saturday to Wednesday, there must be at least one day to Wednesday, there must be at least one day on which you have at least two classes.on which you have at least two classes.

Example:Example: In a 36-student class, at least 8 In a 36-student class, at least 8 students will get the same letter grade (A, B, students will get the same letter grade (A, B, C, D, or F).C, D, or F).

37Copyright M.R.K. Krishna Rao 2003

Generalized Pigeonhole PrincipleGeneralized Pigeonhole Principle

Example:Example: Assume you have a drawer containing Assume you have a drawer containing a random distribution of a dozen brown socks a random distribution of a dozen brown socks and a dozen black socks. It is dark, so how many and a dozen black socks. It is dark, so how many socks do you have to pick to be sure that among socks do you have to pick to be sure that among them there is a matching pair?them there is a matching pair?

There are two types of socks, so if you pick at There are two types of socks, so if you pick at least 3 socks, there must be either at least two least 3 socks, there must be either at least two brown socks or at least two black socks.brown socks or at least two black socks.

Generalized pigeonhole principle: Generalized pigeonhole principle: 3/23/2 = 2. = 2.

38Copyright M.R.K. Krishna Rao 2003

Generalized Pigeonhole PrincipleGeneralized Pigeonhole Principle

Example:Example: During a period of 75 hours a wrestler plays at least 1 mach an hour, but no more than 125 total matches.Show that there must be a period of some number of consecutive hours during which the wrestler must play exactly 24 matches.

Proof:Proof: Let ajj be the number of matches played on or before the jth hour. Then, a11, a22, …, a7575 is an increasing sequence of integers, with 1 ≤ ajj ≤125.

Moreover, a11+24, a22+24,…, a7575+24 is also an increasing sequence of integers, with 25 ≤ ajj+24 ≤149.

39Copyright M.R.K. Krishna Rao 2003

Generalized Pigeonhole PrincipleGeneralized Pigeonhole Principle

Proof (cont.):Proof (cont.): The 150 positive integers a11, a22, …, a7575, a11+24, a22+24,…, a7575+24 are all less than or equal to 149.Hence, by the pigeonhole principle at least two of these integers are equal.

Since the integers a11, a22, …, a7575 are all distinct and the integers a11+24, a22+24,…, a7575+24 are all distinct, there must be indices i and j with aii = ajj+24.

This means that exactly 24 matches were played from hour j+1 to hour i. Q.E.D.

40Copyright M.R.K. Krishna Rao 2003

Blackboard Exercises for Section Blackboard Exercises for Section 4.24.2

Problem 4.2.9: Problem 4.2.9: How many students, each of whom comes from one of 50 states, should be enrolled in a university to ensure that there are at least 100 coming from the same state?

Problem 4.2.27: There are 38 different time periods Problem 4.2.27: There are 38 different time periods during which classes at a university can be during which classes at a university can be scheduled. If there are 677 different classes, how scheduled. If there are 677 different classes, how many different rooms will be needed?many different rooms will be needed?

Show that if Show that if S S {1,2,3,…,50} & | {1,2,3,…,50} & |S S |> 9 then there are |> 9 then there are at least 2 different 5 element subsets in at least 2 different 5 element subsets in SS all having all having the same sum.the same sum.

Read and understand Examples 7 and 8 in sec. 4.2.Read and understand Examples 7 and 8 in sec. 4.2.

41Copyright M.R.K. Krishna Rao 2003

4.3 Permutations and 4.3 Permutations and CombinationsCombinations

How many ways are there to pick a set of 3 people How many ways are there to pick a set of 3 people from a group of 6?from a group of 6?

There are 6 choices for the first person, 5 for the There are 6 choices for the first person, 5 for the second one, and 4 for the third one, so there aresecond one, and 4 for the third one, so there are66554 = 120 ways to do this.4 = 120 ways to do this.

This is not the correct result!This is not the correct result!

For example, picking person C, then person A, and For example, picking person C, then person A, and then person E leads to the then person E leads to the same groupsame group as first as first picking E, then C, and then A.picking E, then C, and then A.

However, these cases are counted However, these cases are counted separatelyseparately in in the above equation.the above equation.

42Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

So how can we compute how many different So how can we compute how many different subsets of people can be picked (that is, we want subsets of people can be picked (that is, we want to disregard the order of picking) ?to disregard the order of picking) ?

To find out about this, we need to look at To find out about this, we need to look at permutations and combinations.permutations and combinations.

A A permutationpermutation of a set of distinct objects is an of a set of distinct objects is an ordered arrangement of these objects.ordered arrangement of these objects.

An ordered arrangement of r elements of a set is An ordered arrangement of r elements of a set is called an called an r-permutationr-permutation..

43Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

Example:Example: Let S = {1, 2, 3}. Let S = {1, 2, 3}.The arrangement 3, 1, 2 is a permutation of S.The arrangement 3, 1, 2 is a permutation of S.The arrangement 3, 2 is a 2-permutation of S.The arrangement 3, 2 is a 2-permutation of S.

The number of r-permutations of a set with n The number of r-permutations of a set with n distinct elements is denoted by distinct elements is denoted by P(n, r).P(n, r).

We can calculate P(n, r) with the product rule:We can calculate P(n, r) with the product rule:

P(n, r) = nP(n, r) = n(n – 1)(n – 1)(n – 2) (n – 2) ……(n – r + 1).(n – r + 1).

(n choices for the first element, (n – 1) for the (n choices for the first element, (n – 1) for the second one, (n – 2) for the third one…)second one, (n – 2) for the third one…)

44Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

Example:Example: 8 runners compete for 3 medals (Gold, S, 8 runners compete for 3 medals (Gold, S, B). How many ways can the race finish?B). How many ways can the race finish?

P(8, 3) = 8P(8, 3) = 8776 = 3366 = 336 = (8= (87766554433221)/(51)/(54433221)1)

General formula:General formula:

P(n, r) = n!/(n – r)!P(n, r) = n!/(n – r)!

Knowing this, we can return to our initial question:Knowing this, we can return to our initial question:

How many ways are there to pick a set of 3 people How many ways are there to pick a set of 3 people from a group of 6 (disregarding the order of picking)?from a group of 6 (disregarding the order of picking)?

45Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

An An r-combinationr-combination of elements of a set is an of elements of a set is an unordered selection of r elements from the set.unordered selection of r elements from the set.Thus, an r-combination is simply a subset of the Thus, an r-combination is simply a subset of the set with r elements.set with r elements.

Example:Example: Let S = {1, 2, 3, 4}. Let S = {1, 2, 3, 4}.Then {1, 3, 4} is a 3-combination from S.Then {1, 3, 4} is a 3-combination from S.

The number of r-combinations of a set with n The number of r-combinations of a set with n distinct elements is denoted by C(n, r).distinct elements is denoted by C(n, r).

Example:Example: C(4, 2) = 6, since, for example, the 2- C(4, 2) = 6, since, for example, the 2-combinations of a set {1, 2, 3, 4} are {1, 2}, {1, combinations of a set {1, 2, 3, 4} are {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}.3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}.

46Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

How can we calculate C(n, r)?How can we calculate C(n, r)?

Consider that we can obtain the r-permutation of a Consider that we can obtain the r-permutation of a set in the following way:set in the following way:

First,First, we form all the r-combinations of the set we form all the r-combinations of the set(there are C(n, r) such r-combinations).(there are C(n, r) such r-combinations).

Then,Then, we generate all possible orderings in each we generate all possible orderings in each of these r-combinations (there are P(r, r) such of these r-combinations (there are P(r, r) such orderings in each case).orderings in each case).

Therefore, we have:Therefore, we have:

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

47Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

C(n, r) = P(n, r)/P(r, r)C(n, r) = P(n, r)/P(r, r) = n!/(n – r)!/(r!/(r – r)!)= n!/(n – r)!/(r!/(r – r)!) = n!/(r!(n – r)!)= n!/(r!(n – r)!)

Now we can answer our initial question:Now we can answer our initial question:

How many ways are there to pick a set of 3 people How many ways are there to pick a set of 3 people from a group of 6 (disregarding the order of from a group of 6 (disregarding the order of picking)?picking)?

C(6, 3) = 6!/(3!C(6, 3) = 6!/(3!3!) = 720/(63!) = 720/(66) = 720/36 = 206) = 720/36 = 20

There are 20 different ways, that is, 20 different There are 20 different ways, that is, 20 different groups to be picked.groups to be picked.

48Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

Corollary:Corollary: Let n and r be nonnegative integers with r Let n and r be nonnegative integers with r n. n.Then C(n, r) = C(n, n – r).Then C(n, r) = C(n, n – r).

Note that Note that “picking a group of r people from a “picking a group of r people from a group of n people”group of n people” is the same as is the same as “splitting a “splitting a group of n people into a group of r people and group of n people into a group of r people and another group of (n – r) people”.another group of (n – r) people”.

Please also look at proof on page 252.Please also look at proof on page 252.

49Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

Example:Example: How many programming teams (4 members) How many programming teams (4 members) can be selected from 10 CS students?can be selected from 10 CS students?

C(10, 4) = 10!/(4!C(10, 4) = 10!/(4!6!) = (10 6!) = (10 9 9 8 8 7) / (4 7) / (4 3 3 2)= 2)= 210210

Example:Example: In how many ways can we select 4 players In how many ways can we select 4 players from 10 to play the first 4 matches, 1from 10 to play the first 4 matches, 1stst, 2, 2ndnd… of the day?… of the day?

P(10, 4) = 10!/6! = 10 P(10, 4) = 10!/6! = 10 9 9 8 8 7= 5040 7= 5040

Example:Example: A soccer club has 8 seniors and 7 juniors. For today’s match, the coach wants to have 6 seniors and 5 seniors on the grass. How many ways can he choose?

C(8, 6) C(7, 5) = 8!/(6! C(7, 5) = 8!/(6!2!) 2!) 7!/(5! 7!/(5!2!) = 282!) = 2821 = 21 = 588588

50

Permutations and CombinationsPermutations and Combinations

Several ways of interpreting questionSeveral ways of interpreting question

““How many ways are there to buy 13 different How many ways are there to buy 13 different bagels out of 17 types?bagels out of 17 types?””

11stst Interpretation: order matters Interpretation: order matters

22ndnd Interpretations: order doesn Interpretations: order doesn’’t matter so t matter so only care about set of bagels that have at only care about set of bagels that have at the end, not about the purchasing the end, not about the purchasing instructions.instructions.

………………

………………same purchase

51

Permutations and CombinationsPermutations and Combinations

Usually 2Usually 2ndnd interpretation is taken, but in other interpretation is taken, but in other situations, 1situations, 1stst interpretation makes more interpretation makes more sense. EG: Suppose the bagels were sense. EG: Suppose the bagels were destined to the 13 alphabetized children in a destined to the 13 alphabetized children in a kindergarten. kindergarten.

………………

AlAl Bina Charmaine Darlene Bina Charmaine Darlene

52

First Interpretation:First Interpretation:Number of Number of rr-permutations-permutations

Solution under 1Solution under 1stst interpretation: interpretation:

………………

AlAl Bina Bina Charmaine Charmaine DarleneDarlene

1717 ·· 1616 ·· 15 15 ·· 14 14 ·· …… ·· 6 6··5 5

= = P P (17,13) = 17!/4!(17,13) = 17!/4!

= Number of 1-to-1 Functions (13b + 17g)= Number of 1-to-1 Functions (13b + 17g)

53

Second Interpretation:Second Interpretation:Number of Number of rr-combinations-combinations

In the second interpretation of bagel counting In the second interpretation of bagel counting problem, order doesnproblem, order doesn’’t matter. t matter.

Answer will be Answer will be C C (17,13) -- rather than (17,13) -- rather than P P (17,13).(17,13).

= 17!/(13! . 4!)= 17!/(13! . 4!)

2380)4,17(1234

1415161712345678910111213

567891011121314151617

)13,17(

C

C

54

String Counting ExampleString Counting Example

EG: How many length 38 ASCII strings are EG: How many length 38 ASCII strings are there s.t.there s.t.– All characters are distinctAll characters are distinct– The characters The characters ‘‘aa’’ ‘‘bb’’ and and ‘‘cc’’ appear in appear in

orderorder– (and assume that |ASCII| = 128)(and assume that |ASCII| = 128)

55

String Counting ExampleString Counting Example

Method for computing answer:Method for computing answer:Decide where to put (a,b,c). There are 38 Decide where to put (a,b,c). There are 38

spots, out of which 3 must be chosen spots, out of which 3 must be chosen together, therefore together, therefore C C (38,3) choices.(38,3) choices.

After 1 has been chosen, 35 spots remain. So After 1 has been chosen, 35 spots remain. So there are 125=128-3 choices for the first, there are 125=128-3 choices for the first, 124 for the second, etc. Therefore, 124 for the second, etc. Therefore, P(125,35) choices.P(125,35) choices.

Product rule applies to (1) and (2) to obtain Product rule applies to (1) and (2) to obtain the final answer:the final answer:

C C (38,3) (38,3) ·· P(125,35) P(125,35)

56Copyright M.R.K. Krishna Rao 2003

CombinationsCombinations

We saw the following:We saw the following:

),(!)!(

!

)]!([)!(

!),( rnC

rrn

n

rnnrn

nrnnC

This symmetry is intuitively plausible. For This symmetry is intuitively plausible. For example, let us consider a set containing six example, let us consider a set containing six elements (n = 6).elements (n = 6).

Picking twoPicking two elements and elements and leaving fourleaving four is is essentially the same as essentially the same as picking fourpicking four elements elements and and leaving twoleaving two..

In either case, our number of choices is the In either case, our number of choices is the number of possibilities to number of possibilities to dividedivide the set into the set into one set containing two elements and another one set containing two elements and another set containing four elements.set containing four elements.

57Copyright M.R.K. Krishna Rao 2003

CombinationsCombinations

Pascal’s Identity:Pascal’s Identity:

Let n and k be positive integers with n Let n and k be positive integers with n k. k.Then C(n + 1, k) = C(n, k – 1) + C(n, k).Then C(n + 1, k) = C(n, k – 1) + C(n, k).

How can this be explained?How can this be explained?

What is it good for?What is it good for?

58Copyright M.R.K. Krishna Rao 2003

CombinationsCombinations

Imagine a set S containing n elements and a set T Imagine a set S containing n elements and a set T containing (n + 1) elements, namely all elements containing (n + 1) elements, namely all elements in S plus a new element in S plus a new element aa..

Calculating C(n + 1, k) is equivalent to answering Calculating C(n + 1, k) is equivalent to answering the question: How many subsets of T containing k the question: How many subsets of T containing k items are there?items are there?

Case I:Case I: The subset contains (k – 1) elements of S The subset contains (k – 1) elements of S plus the element plus the element aa: C(n, k – 1) choices.: C(n, k – 1) choices.

Case II:Case II: The subset contains k elements of S and The subset contains k elements of S and does not contain does not contain aa: C(n, k) choices.: C(n, k) choices.

Sum Rule:Sum Rule: C(n + 1, k) = C(n, k – 1) + C(n, k). C(n + 1, k) = C(n, k – 1) + C(n, k).

59Copyright M.R.K. Krishna Rao 2003

Pascal’s TrianglePascal’s Triangle

In Pascal’s triangle, each number is the sum of In Pascal’s triangle, each number is the sum of the numbers to its upper left and upper right:the numbers to its upper left and upper right:

11

11 11

11 22 11

11 33 33 11

11 44 66 44 11

…… …… …… …… …… ……

60Copyright M.R.K. Krishna Rao 2003

Pascal’s TrianglePascal’s Triangle

Since we have C(n + 1, k) = C(n, k – 1) + C(n, k) andSince we have C(n + 1, k) = C(n, k – 1) + C(n, k) andC(0, 0) = 1, we can use Pascal’s triangle to simplify the C(0, 0) = 1, we can use Pascal’s triangle to simplify the computation of C(n, k):computation of C(n, k):

C(0, 0) = C(0, 0) = 11

C(1, 0) = C(1, 0) = 11C(1, 1) = C(1, 1) = 11

C(2, 0) = C(2, 0) = 11C(2, 1) = C(2, 1) = 22C(2, 2) = C(2, 2) = 11

C(3, 0) = C(3, 0) = 11C(3, 1) = C(3, 1) = 33C(3, 2) = C(3, 2) = 33C(3, 3) = C(3, 3) = 11

C(4, 0) = C(4, 0) = 11C(4, 1) = C(4, 1) = 44C(4, 2) = C(4, 2) = 66C(4, 3) = C(4, 3) = 44C(4, 4) = C(4, 4) = 11

kk

nn

61Copyright M.R.K. Krishna Rao 2003

4.4 Binomial Coefficients4.4 Binomial Coefficients

Expressions of the form C(n, k) are also called Expressions of the form C(n, k) are also called binomial coefficientsbinomial coefficients..

How come?How come?

A A binomial expressionbinomial expression is the sum of two terms, is the sum of two terms, such as (a + b).such as (a + b).

Now consider (a + b)Now consider (a + b)2 2 = (a + b)(a + b).= (a + b)(a + b).

When expanding such expressions, we have to When expanding such expressions, we have to form all possible products of a term in the first form all possible products of a term in the first factor and a term in the second factor:factor and a term in the second factor:

(a + b)(a + b)22 = a·a + a·b + b·a + b·b = a·a + a·b + b·a + b·b

Then we can sum identical terms:Then we can sum identical terms:

(a + b)(a + b)22 = a = a22 + 2ab + b + 2ab + b22

62Copyright M.R.K. Krishna Rao 2003

Binomial CoefficientsBinomial Coefficients

For (a + b)For (a + b)3 3 = (a + b)(a + b)(a + b) we have= (a + b)(a + b)(a + b) we have

(a + b)(a + b)3 3 = aaa + aab + aba + abb + baa + bab + = aaa + aab + aba + abb + baa + bab + bba + bbbbba + bbb

(a + b)(a + b)3 3 = a= a33 + 3a + 3a22b + 3abb + 3ab22 + b + b33

There is only one term a3, because there is only one possibility to form it: Choose a from all three factors: C(3, 3) = 1.There is three times the term a2b, because there are three possibilities to choose a from two out of the three factors: C(3, 2) = 3.Similarly, there is three times the term ab2 (C(3, 1) = 3) and once the term b3 (C(3, 0) = 1).

63Copyright M.R.K. Krishna Rao 2003

Binomial CoefficientsBinomial Coefficients

This leads us to the following formula:This leads us to the following formula:

jn

j

jnn bajnCba

0

),()(

With the help of Pascal’s triangle, this formula With the help of Pascal’s triangle, this formula can considerably simplify the process of can considerably simplify the process of expanding powers of binomial expressions.expanding powers of binomial expressions.

For example, the fifth row of Pascal’s triangleFor example, the fifth row of Pascal’s triangle(1 – 4 – 6 – 4 – 1) helps us to compute (a + b)(1 – 4 – 6 – 4 – 1) helps us to compute (a + b)44::

(a + b)(a + b)44 = a = a44 + 4a + 4a33b + 6ab + 6a22bb22 + 4ab + 4ab33 + b + b44

(Binomial (Binomial Theorem)Theorem)

64Copyright M.R.K. Krishna Rao 2003

Permutations and CombinationsPermutations and Combinations

Example: What is the coefficient of xWhat is the coefficient of x1212yy1313 in in the expansion of (x+y)the expansion of (x+y)2525??

C(25,13) = 25! / (13! 12!) = 5,200,300 C(25,13) = 25! / (13! 12!) = 5,200,300

Example: What is the coefficient of xWhat is the coefficient of x1212yy1313 in in the expansion of (2x-3y)the expansion of (2x-3y)2525??

C(25,13).2C(25,13).21212.(-3).(-3)1313 = - (25! 2 = - (25! 21212 3 31313) / (13! 12!)) / (13! 12!)

Theorems 4, 5 and 7:…Theorems 4, 5 and 7:…Blackboard ExercisesBlackboard Exercises: 14,16,18,20,24,32,48: 14,16,18,20,24,32,48

( ) ( , ) ( ) ( )2 3 25 2 325 25

0

25

x y C j x yj j

j

65Copyright M.R.K. Krishna Rao 2003

4.5 Generalized Permutations and 4.5 Generalized Permutations and CombinationsCombinations

It turns out the the following algorithmic It turns out the the following algorithmic problem is very important to computer problem is very important to computer science. In fact, almost every algorithmic science. In fact, almost every algorithmic problem can be converted to this problem as problem can be converted to this problem as it is “NP-complete”.it is “NP-complete”.

Integer Linear ProgrammingInteger Linear Programming: Given integer : Given integer variable inequalities with integer coefficients, variable inequalities with integer coefficients, find a solution to all variables simultaneously find a solution to all variables simultaneously which maximizes some function.which maximizes some function.

EG: Find integers EG: Find integers x,y,zx,y,z satisfying: satisfying:x x 0, 0, yy 0, 0, zz 0, 0, xx++yy++z z 136, 136, xx++yy++zz 136 136 and maximizing and maximizing f f ((xx) = 36) = 36xx - 14 - 14yy + 17 + 17zz

66Copyright M.R.K. Krishna Rao 2003

Integer Linear ProgrammingInteger Linear Programming

Unfortunately, there is no known fast algorithm Unfortunately, there is no known fast algorithm for solving this problem. In general, forced to for solving this problem. In general, forced to try every possibility and keep track of (try every possibility and keep track of (x,y,zx,y,z) ) with current best with current best f f ((x,y,zx,y,z). ).

Would like to get an idea at least, of how many Would like to get an idea at least, of how many non-negative integer solutions there are to non-negative integer solutions there are to xx++yy++z z = 136 before commencing search for = 136 before commencing search for best (best (x,y,zx,y,z) so have idea of how long solution ) so have idea of how long solution will take to find.will take to find.

67Copyright M.R.K. Krishna Rao 2003

Permutations with Repetitions

How many strings of length n can be formed using the 26 lower case letters of the alphabet?

2626nn

Every letter can be used repeatedly, so it is like a n digit number to the base 26

This is an application of the product rule.

The number of r-permutations of a set of n objects with repetition allowed is nr r .

68Copyright M.R.K. Krishna Rao 2003

Permutations with Repetitions

An An anagramanagram of a string is a of a string is a rearrangement of the letters in the rearrangement of the letters in the string.string.

EG, ignoring white-space and capitals an EG, ignoring white-space and capitals an anagram on “William Gates” is:anagram on “William Gates” is:

““Will I tame gas”Will I tame gas”

-also allow random anagrams such as-also allow random anagrams such as

““liiwlegamast”liiwlegamast”

Q: How many different anagrams are Q: How many different anagrams are there on the string “williamgates”?there on the string “williamgates”?

69Copyright M.R.K. Krishna Rao 2003

Permutations with Repetitions

A: Alphabetize A: Alphabetize ““williamgateswilliamgates”” to get: to get:““aaegiillmstwaaegiillmstw”” (length = 12) (length = 12)

Choose 2 spaces to put the aChoose 2 spaces to put the a’’s: s: C C (12,2)(12,2)Choose 1 space of remaining for e: Choose 1 space of remaining for e: C C (10,1)(10,1)Choose 1 of remaining 9 for g: Choose 1 of remaining 9 for g: C C (9,1)(9,1)Choose 2 of remaining 8 for i: Choose 2 of remaining 8 for i: C C (8,2)(8,2)Choose 2 of remaining 6 for l: Choose 2 of remaining 6 for l: C C (6,2)(6,2)Choose 1 of remaining 4 for m: Choose 1 of remaining 4 for m: C C (4,1)(4,1)Choose 1 of remaining 3 for s: Choose 1 of remaining 3 for s: C C (3,1)(3,1)Choose 1 of remaining 2 for t: Choose 1 of remaining 2 for t: C C (2,1)(2,1)Only one choice remains for w ( Only one choice remains for w ( CC(1,1) = 1 )(1,1) = 1 )

70Copyright M.R.K. Krishna Rao 2003

Permutations with Repetitions

Use product rule obtaining answer:Use product rule obtaining answer:

C (12,2)C (10,1)C (9,1)C (8,2)C (6,2)C (4,1)C C (12,2)C (10,1)C (9,1)C (8,2)C (6,2)C (4,1)C (3,1) C (2,1) = 66·10·9·28·15·4·3·2 = (3,1) C (2,1) = 66·10·9·28·15·4·3·2 = 59,875,20059,875,200

In general, if there are n letters and the In general, if there are n letters and the repetition numbers are arepetition numbers are a11, a, a2 2 , a, a3 3 , …. , a, …. , ak k then then the number of anagrams is given bythe number of anagrams is given by

n ! / (an ! / (a11! a! a2 2 ! a! a3 3 ! … a! … ak k !)!)

the number of anagrams of “William Gates” isthe number of anagrams of “William Gates” is

12! / (2! · 2! · 2!) = 479,001,600 / 8 = 12! / (2! · 2! · 2!) = 479,001,600 / 8 = 59,875,20059,875,200

71Copyright M.R.K. Krishna Rao 2003

Permutations with Repetitions

Theorem:Theorem: The number of ways to distribute The number of ways to distribute n distinguishable objects into k n distinguishable objects into k distinguishable boxes so that adistinguishable boxes so that a ii objects are objects are placed into box i is placed into box i is n ! / (an ! / (a11! a! a2 2 ! a! a3 3 ! ! …… aak k !).!).

Example:Example: In how many ways can we In how many ways can we distribute 40 different books into 4 distribute 40 different books into 4 numbered boxes, with 10 books each?numbered boxes, with 10 books each?

Example:Example: In 40!/(10!. 10!. 10!. 10!) ways. In 40!/(10!. 10!. 10!. 10!) ways.

72Copyright M.R.K. Krishna Rao 2003

Select 4 pieces of fruit from a bowl that contains 5 apples, 5 pears and 5 oranges.

4a, 3a + 1p, 3a + 1o, 2a + 2p, 2a + 1p + 1o4p, 3p + 1a, 3p + 1o, 2p + 2o, 2p + 1a + 1o4o, 3o + 1a, 3o + 1p, 2o + 2a, 2o + 1a + 1p

15 ways altogether

Combinations with Repetitions

73Copyright M.R.K. Krishna Rao 2003

Reconsider our problem of selecting 4 pieces of fruit where the four pieces are made up of apples, pears and oranges

• We can think of this as having 3 containers• one for apples• one for pears• one for oranges

• The sum of the contents has to be 4• We can draw this as “stars and bars”

• we have 4 stars• one for each piece of fruit

• two bars• to give us 3 regions

74Copyright M.R.K. Krishna Rao 2003

• apples|pears|oranges

****| | 4 apples **|**| 2 apples, 2 pears **| |** 2 apples, 2 oranges **|*|* 2 apples, 1 pear, 1 orange *|**|* 1 apple, 2 pears, 1 orange *|*| ** 1 apple, 1 pear, 2 oranges• and so on

• Therefore we consider all arrangements of

• 4 stars and 2 bars (6 objects)• Pick 2 positions (out of 6) for 2 bars

• C(6,2) = 15

75Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with Repetitions

There are C(n+r-1,r) r-combinations from a set of n elements when repetition of elements is allowed.

Example:Example: Revisiting the fruit question, we want the number of 4-combinations (4 pieces of fruit, r = 4) from a set with three elements (apples, pears, oranges, n = 3) allowing repetitions. C(n+r-1,r) = C(6,4) = 15.

Example:Example: Find the number of non-negative Find the number of non-negative integers solutions to integers solutions to xx++yy++z z = 136.= 136.

Solution: Solution: C(n+r-1,r) = C(138,136) = 9453.

Q: How many ways are there to buy 13 bagels Q: How many ways are there to buy 13 bagels from 17 types from 17 types with repetitionswith repetitions? ?

76Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with Repetitions

A: How many ways are there to buy 13 bagels A: How many ways are there to buy 13 bagels from 17 types?from 17 types?

Let Let xxii = no. of bagels bought of type = no. of bagels bought of type i.i.

Interested in counting the number of solutions Interested in counting the number of solutions to to xx11++xx22++……++xx1717 = 13. Therefore,= 13. Therefore,

answer is answer is C C (16+13,13) = (16+13,13) = C C (29,13) = (29,13) = 67,863,915. 67,863,915.

Q: How many solutions in Q: How many solutions in NN are there to are there to xx11++xx22++xx33++xx44++xx55 = 21 if = 21 if xx11≥ 1 ?≥ 1 ?

77Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with Repetitions

A: A: xx11++xx22++xx33++xx44++xx55 = 21 & = 21 & xx11≥ 1 :≥ 1 :

||{{xx11++xx22++xx33++xx44++xx55 = 21 | = 21 | xx11≥ 1 } ≥ 1 } ||

= = ||{{xx11++xx22++xx33++xx44++xx55 = 20} = 20} ||This is because one This is because one is forced to be on pile 1, is forced to be on pile 1,

so are asking how many ways are there to so are asking how many ways are there to distribute remaining 20 distribute remaining 20 ’s.’s.

Answer = Answer = C C (24,4) = 10,626(24,4) = 10,626

Q: How many solutions in Q: How many solutions in NN are there toare there to

xx11++xx22++xx33++xx44++xx55 = 21 = 21

if if xx11≥ 2 , ≥ 2 , xx22≥ 2 , ≥ 2 , xx33≥ 2 , ≥ 2 , xx44≥ 2 and ≥ 2 and xx55≥ 2 ?≥ 2 ?

78Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with Repetitions

A: A: xx11++xx22++xx33++xx44++xx55 = 21, = 21, xx11≥2, ≥2, xx22≥2 , ≥2 , xx3 3 ≥2, ≥2, xx4 4

≥ 2 and ≥ 2 and xx5 5 ≥ 2 :≥ 2 :

Same idea. 2 Same idea. 2 ’s are forced to remain on each ’s are forced to remain on each of 5 piles. So this is the same as counting of 5 piles. So this is the same as counting solutions of solutions of

xx11++xx22++xx33++xx44++xx55 = 11.= 11.

So answer is So answer is C C (15,4) = 1365(15,4) = 1365

Q: How many solutions in Q: How many solutions in NN are there toare there to

xx11++xx22++xx33++xx44++xx55 = 21 if = 21 if xx1 1 < 11 ?< 11 ?

79Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with Repetitions

A: A: xx11++xx22++xx33++xx44++xx55 = 21 & = 21 & xx1 1 < 11:< 11:

||{{xx11++xx22++xx33++xx44++xx55 = 21 | = 21 | xx11<11} <11} ||

= = ||{{all solutionsall solutions} - {} - {solutions with xsolutions with x1 1 ≥≥ 11} 11}||= = C C (25,4) - (25,4) - C C (14,4) = 11,649(14,4) = 11,649

Q: How many solutions in Q: How many solutions in NN are there toare there to

xx11++xx22++xx33++xx44++xx55 = 21, = 21, xx11<4, 1≤<4, 1≤xx22<4, <4, xx3 3 ≥ 15 ?≥ 15 ?

80Copyright M.R.K. Krishna Rao 2003

Counting with RepetitionsCounting with RepetitionsA: A: xx11++xx22++xx33++xx44++xx55 = 21, = 21, xx11<4, 1≤<4, 1≤xx22<4, <4, xx3 3 ≥ 15 :≥ 15 :

|| { {xx11++xx22++xx33++xx44++xx55 = 21| = 21| xx11<4, 1≤<4, 1≤xx22<4, <4, xx3 3 ≥ 15 } ≥ 15 } ||

= = || { {xx11++xx22++xx33++xx44++xx55 = 5|= 5|xx11<4, <4, xx22<3} <3} ||(1 (1 stuck on pile #2 and 15 stuck on pile #2 and 15 ’s stuck on #3)’s stuck on #3)

So:So: | |{{all solutionsall solutions}}||--||{{solutions with xsolutions with x11≥ 4 OR ≥ 4 OR xx22≥ 3}≥ 3}||

Inclusion-Exclusion principle implies:Inclusion-Exclusion principle implies: ||{{xx11≥ 4 OR ≥ 4 OR xx22≥ 3}≥ 3}|=||=|{{xx11≥ 4}≥ 4}|+||+|{{xx22≥ 3}≥ 3}|-||-|{{xx11≥ 4 AND ≥ 4 AND xx22≥ 3}≥ 3}||

==C C (5,1) + (5,1) + C C (6,2) - “(6,2) - “C C (2,4)” = 5 +15 -0= 20(2,4)” = 5 +15 -0= 20Number of solutions = Number of solutions = C C (9,4) (9,4) - - 20 = 126-20 = 10620 = 126-20 = 106

81Copyright M.R.K. Krishna Rao 2003

Blackboard Exercises for 4.6Blackboard Exercises for 4.6

How many solutions in How many solutions in NN are there toare there to

xx11++xx22++xx33++xx44++xx55 ≤ 21 ?≤ 21 ?

How many solutions in How many solutions in NN are there toare there to

xx11++xx22++xx33++xx44++xx55 > 21 ?> 21 ?