COMP239: Mathematics for Computer Science...

29
COMP239: Mathematics for Computer Science II Prof. Chadi Assi [email protected] EV7.635

Transcript of COMP239: Mathematics for Computer Science...

Page 1: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

COMP239: Mathematics for Computer Science II

Prof. Chadi [email protected]

EV7.635

Page 2: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Generalized Permutations and Combinations

Page 3: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Permutations with RepetitionExample:

For {1, 2, 3, 4}, (2,2,1) and (2,2,3) are permutations with repetition allowed

Example: How many strings of length r can be formed from the English alphabet?

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

Page 4: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with Repetition

Example: How many ways are there to select 5 bills from a box containing 1$, 2$, 5$, 10$, 20$, 50$, and 100$ bills? Assuming that the order in which the bills are chosen does not matter and there are at least 5 bills of each type?

Page 5: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with Repetition

$100 $50 $20 $10 $5 $2 $1

Cash box with seven types of bills

• Each compartment in the cash box is holing one type of bill

• Compartments are separated by dividers• The choice of 5 bills corresponds to placing 5 markers in

the compartments

Page 6: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with Repetition$100 $50 $20 $10 $5 $2 $1

*****

*** **

*****

*****

Selecting 5 bills corresponds to the number of ways to arrange six bars and 5 stars, which is indeed the number of ways to select the positions of (unordered) 5 stars from 11 possible positions = C(11,5)

Page 7: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with RepetitionTheorem:

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

Proof Similar to before, use n-1 bars (to represent ncells) and r stars. Selecting the positions of rstars out of n – 1 + r positions equals to C(n + r − 1, r)

Page 8: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with RepetitionExample:

How many solutions does the equation: x1 + x2 + x3 + x4 = 11 have, where x1, x2, x3, x4 are nonnegative integers?

Solution: A solution corresponds to selecting 11 items from a set with 4 elements (x1, x2, x3, x4) so that x1 items of type 1, x2 items of type 2, x3 items of type 3, and x4 items of type 4 chosen; total number of solutions is number of 11-combinations, repetition allowed, from a set with 4 elements = C(4 + 11 − 1, 11)

Page 9: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Combinations with RepetitionExample:

How many solutions does the equation: x1 + x2 + x3 + x4 = 11 have, where x1 ≥ 1, x2 ≥ 1, x3 ≥ 2, x4 ≥ 4?

Solution: A solution corresponds to selecting 11 items so that x1 items of type 1, x2 items of type 2, x3 items of type 3, and x4 items of type 4 chosen;In addition, there is at least: 1 item of type 1, 1 item of type 2, 2 items of type 3, and 4 items of type 4.Then select 3 (unordered) additional items from the set of 4 elements; C(4 + 3 − 1, 3)

Page 10: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Summary

C(n+r-1,r)Yesr-combinations

nrYesr-permutations

C(n,r)Nor-combinations

P(n,r)Nor-permutations

FormulaRepetition Allowed?

Type

Page 11: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Permutations with Indistinguishable Objects

Example: How many different strings can be made by reordering the letters of the word SUCCESS?

Theorem: The number of different permutations of nobjects, where there are n1 indistinguishable objects of type 1, n2 indistinguishable objects of type 2, ..... , and nk indistinguishable objects of type k, is:

!!....!!

21 knnnn

Page 12: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Distributing Objects into Boxes

Distinguishable objects and distinguishable boxes

Example: How many ways are there to distribute 5 cards to each of four players from a deck of 52 cards?

Solution: in class

Page 13: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Distributing Objects into Boxes

Theorem:The number of ways to distribute ndistinguishable objects into k distinguishable boxes so that ni objects are placed into box i, i = 1, 2, ..., k equals:

Proof (in class)

!!....!!

21 knnnn

Page 14: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Distributing Objects into Boxes

Indistinguishable objects and distinguishable boxes

Example: How many ways are there to place 10 indistinguishable balls into eight distinguishable bins?

Solution: in class

Page 15: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Advanced Counting Techniques

Page 16: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Some problems cannot be solved using counting techniques we explained earlier.

Example: How many bit strings of length n do not contain two consecutive 0?

Such problems can be solved by finding relationships, called recurrence relations.

Page 17: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Example: The number of bacteria in a colony doubles every hour. If a colony begins with 5 bacteria, how many will be present in n hours?

Let an be the number of bacteria at the end of n hours an = 2an-1 (n>0). Knowing the initial condition (a0 = 5), we can solve for an .

Page 18: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Definition: A recurrence relation for the sequence {an} is an equation that expresses an in terms of one or more of the previous terms of the sequence.

A sequence is called a solution of the recurrence relation if its terms satisfy the recurrence relation

Page 19: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Example: (Compound Interest at 8%) Let Pn denote the amount in a bank account after n years. Recurrence relation:Pn = Pn-1 + 0.08Pn-1 = (1.08) Pn-1

If the initial condition is P0, thenPn = (1.08)n P0

If P0 =$10,000, then after 30 years, the account contains $100,626.56

Page 20: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Example: Fibonacci Numbers A young pair of rabbits (one of each sex) is placed on an island. A pair of rabbits does not breed until they are two months old. Thereafter, each pair produces another pair each month. Find a recurrence relation for the number of pairs of rabbits on the island after n months (assume no rabbits ever die).

Page 21: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence RelationsExample: Towers of Hanoi

Move disks to peg 2; never place a larger disk on top of a smaller one. Set up a recurrence relation for the number of moves needed for a tower with n disks.

Peg 1 Peg 2 Peg 3

Page 22: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence RelationsLet Hn be the number of moves needed.

Transfer n - 1 disks to peg3 (using Hn-1 moves); move the largest disk to peg 2; then transfer the n - 1 disks from Peg3 to Peg 2 (using Hn-1 moves).

Peg 1 Peg 2 Peg 3

Page 23: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence RelationsHn = Hn-1 + 1 + Hn-1 = 2 Hn-1 +1

= 2 (2 Hn-2 +1) + 1 = 22 Hn-2 + 2 + 1= 22 (2 Hn-3 +1) + 2 + 1 = 23 Hn-3 + 22 + 2 + 1...= 2n-1 H1 + 2n-2 + 2n-3 + ….+ 2 + 1 (Note, H1 = 1)= 2n-1 + 2n-2 + 2n-3 + ….+ 21 + 20

= 2n - 1

Page 24: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Example:How many bit strings of length n do not contain 2 consecutive 0’s?

Solution: (assume n > 2)Denote an the number of such strings.an = the number of such bits strings ending

with 0 + the number of such bit strings ending with 1.

Page 25: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence RelationsThe number of bits strings of length n, ending with 1 and do not have two consecutive 0s is the number of bit strings of length n - 1 with no two consecutive 0s and a 1 added at the end

hence, there is an-1 such bit strings

The number of bits strings of length n, ending with 0 and do not have two consecutive 0s must have 1 as their (n - 1)th bit these bit strings are equivalent to the bit strings of length n - 2 with no two consecutive 0s and 10 added at the end

hence, there is an-2 such bit strings

Page 26: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Hence, we have the following recurrence relation: an = an-1 + an-2 (n > 2)with initial conditions: a1 = 2 and a2 = 3

Find the number of bit strings of length 5 that do not contain two consecutive 0sa3 = a2 + a1 = 3 + 2 = 5a4 = a3 + a2 = 5 + 3 = 8a5 = a4 + a3 = 8 + 5 = 13

Page 27: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

Example:How many strings of n decimal digits (0-9) contain an even number of 0’s?

Solution:Let an be the number of such valid strings:a1 = 9

There are two ways to construct valid strings of length n from strings of length n-1.

Page 28: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence RelationsThere are two ways to construct valid strings of length n from strings of length n-1.1)- Start from a valid string of length n-1 and append a

digit other than 0 (to obtain a valid string of length n). Hence a valid string can be formed in this manner in 9an-1 ways.

2)- Start from a non-valid string (a string with odd number of 0s) of length n-1 and append a 0 (hence you obtain a string of length n with even number of 0s). In how many ways can this be done?

Page 29: COMP239: Mathematics for Computer Science IIusers.encs.concordia.ca/~assi/courses/comp239s/COMP239-lect7.pdf · Indistinguishable objects and distinguishable boxes Example: How many

Recurrence Relations

In how many ways can this (case 2) be done?the same as the number of invalid strings

of length n –1.The number of strings of length n –1 is:

10n-1

and the number of valid strings of length n –1 is an-1

Hence, the number of invalid strings of length n –1 is 10n-1 – an-1

an = 9an-1 + 10n-1 – an-1 = 8an-1 + 10n-1