49
Discrete Math for CS Chapter 6: Combinatorics

Chapter 6: Combinatorics

  • Upload
    brit

  • View
    84

  • Download
    2

Embed Size (px)

DESCRIPTION

Chapter 6: Combinatorics. How to Count:. Combinatorics is about counting. Pigeonhole was about counting too. General Counting Problem: considers the selection of items from a bigger set either with or without repetition and with or without a specific order of selection. Example:. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6: Combinatorics

Discrete Math for CS

Chapter 6: Combinatorics

Page 2: Chapter 6: Combinatorics

Discrete Math for CS

How to Count: Combinatorics is about counting.

Pigeonhole was about counting too.

General Counting Problem:

considers the selection of items from a bigger set either with or without repetition and with or without a specific order of selection

Page 3: Chapter 6: Combinatorics

Discrete Math for CS

Example: Problem 1: At the end of the day a small bakery has a number

of unsold cakes:

4 vanilla cakes, 2 chocolate cakes and 3 fruit cakes

If a customer comes in in a rush and orders 1 cake, how many different choices does the customer have?

ANS: 4+2+3 = 9 choices

Page 4: Chapter 6: Combinatorics

Discrete Math for CS

Example: Problem 2: A mixed-double team is to be chosen to represent a

local tennis club. There are:

6 male players and, 9 female players

How many different possible teams are there?

ANS: any one of 6 males and 9 females so 6x9 = 54 possible teams

Page 5: Chapter 6: Combinatorics

Discrete Math for CS

Addition Principle: If A and B are disjoint events and n

1 ways A can happen while

n2 ways B can happen then there are n

1 + n

2 ways either A or B

can happen.

Problem 1 is an example of the Addition Principle

Page 6: Chapter 6: Combinatorics

Discrete Math for CS

Multiplication Principle: If there is a sequence of k events and n

1 ways the first event

can happen, n2 ways the second event can happen,etc. and

finally nk ways the kth event can happen then the total number of

possible outcomes of all k events is are n1 x n

2 x ... x n

k.

Problem 2 is an example of the Mulitplication Principle

Page 7: Chapter 6: Combinatorics

Discrete Math for CS

Inclusion and Exclusion Principle: From Chapter 3 we have the following formula

The Addition Principle is a special case of this formula where the two sets are disjoint. Hence A ∩ B =

| A ∪ B | = | A | + | B | - | A ∩ B |

Page 8: Chapter 6: Combinatorics

Discrete Math for CS

Multiplication Principle seen Mathematically: Let A

i denote the n

i outcomes of ith event, i = 1,...,k.

Then any sequence of k events is corresponds to an element of the cartesian product

so the number of different sequences is equal to the number of elements in the cartesian product

A1 x A

2 x ... x A

k

|A1| x |A

2| x ... x |A

k|

Page 9: Chapter 6: Combinatorics

Discrete Math for CS

Example: Problem 3: How many 3-digit numbers begin with 3 or 4?:

ANS: Two disjoint cases: 3-digit numbers beginning with 3 and 3-digit numbersbeginning with 4. We use the Addition Principle to add the number of outcomes of each case.Case 1: there are 10 choices of the 2nd digit and 10 more choices of the 3rd so100 choices of a 3-digit number beginning with 3. This is just the Multiplication Principle.Case 2: 100 choices of a 3-digit number beginning with 4.

A total of 100+100 = 200 different numbers.

Page 10: Chapter 6: Combinatorics

Discrete Math for CS

Example 1: I take two pieces of fruit each day for lunch. I have 3 bananas, 4

apples and 2 pears at home. How many different ways can I select two fruit for lunch?

ANS: If I select a banana and apple I have 4 x 3 choices If I choose a banana and pear I have 4 x 2 = 8 choices If I choose an apple and a pear I have 3 x 2 = 6 choices.

I have 12 + 8 + 6 = 26 different ways to choose the fruit for lunch.

Page 11: Chapter 6: Combinatorics

Discrete Math for CS

Example 2: How many different licence plates are there consisting of 6

characters, the first 3 characters are letters and the last 6 are digits.

ANS: I have 26 x 26 x 26 = 17,576 ways to choose the letters. I have 10 x 10 x 10 = 1,000 ways to choose the digits.

I have 17,576 x 1,000 = 17,576,000 different possible licence plate combinations.

Page 12: Chapter 6: Combinatorics

Discrete Math for CS

Counting Formulae: A child has a bag of three kinds of candies Christmas Stripe(A),

butterscotch(B) and Hershey's(C) - . In how many ways can the child select two candies.

Hard to answer this question because we don't know if it is permitted to choose two of the same kind or if choosing a butterscotch candy and then a Hershey's is the same as choosing a Hershey's (BC) and then a butterscotch (CB)

Page 13: Chapter 6: Combinatorics

Discrete Math for CS

4 Cases Repeats allowed and Order matters:

Repeats allowed but Order does not matter:

Repeats not allowed but Order matters:

Repeats not allowed and Order does not matter:

{AA, AB, AC, BA, BB, BC, CA, CB, CC} – total of 9 ways

{AA, AB, AC, BB, BC, CC} – total of 6 ways

{ AB, AC, BC, BA, CA, BC} – total of 6 ways

{ AB, AC, BC} – total of 3 ways

Page 14: Chapter 6: Combinatorics

Discrete Math for CS

How to Decide Which Case Applies? Perhaps we are told that the child blindly puts her hand into a

bag containing the candy. This implies repeats are allowed but really doesn't tell us if the girl cares about the order.

If we are told that she picks the candies out and gives the first chosen candy to friend X and the second chosen candy to friend Y then order clearly matters, at least to X and Y.

Page 15: Chapter 6: Combinatorics

Discrete Math for CS

Order Matters and Repetition Allowed: We choose k items from a set of n items.

We call such a selection a k-sample.

Since repetition is allowed there are n ways to choose an item each time a choice is made.

The Multiplication Principle tells us there are

possible k-samples.

n x n x ... x n = nk

Page 16: Chapter 6: Combinatorics

Discrete Math for CS

Example: A computer represents integers using N binary digits.

The first digit indicates sign (+ or -).

The remaining N-1 digits represent the magnitude of the integer.

How many distinct integers can be represented?

ANS: There are 2N different binary strings. Each one is a separateinteger except for 00...0 and 10...0. Both these have magnitude 0and opposite signs so represnt the same integer; namely 0.

So there are 2N-1 different integers represented this way.

Page 17: Chapter 6: Combinatorics

Discrete Math for CS

k-permutations: Select k objects from a set of n objects and repetitions are not

allowed and order matters.

Such a selection is called a k-permutation.

Example: You have n distinct things and each time you pick one itis no longer available to be chosen again.

Pick 5 students from a class of 20 to become the class leaders. How many different executive teams of students can you form?

Page 18: Chapter 6: Combinatorics

Discrete Math for CS

k-permutations: P(n,k) = the number of ways of choosing k objects from a set of

n objects without repetition and order matters

There are n ways to chose the first itemFor the second choice, the first-chosen item is missing so n-1 ways of choosing the second item.For the third choice, the first two-chosen items are missing so n-2 ways of choosing the third item.

P(n,k) = n(n-1)(n-2)...((n-k+1) = n!/(n-1)!

Page 19: Chapter 6: Combinatorics

Discrete Math for CS

Example: How many four-letter words can you make from distinct letters

in the list a, g, m, o, p and r?

NOTE: Saying “distinct” means no repetitions. Order matters since a different letter order gives a different word.

P(6,4) = 6!/(6-4)! = 6!/2! = 6 •5 •4 •3 = 360

Please ignore the text on this example.

Page 20: Chapter 6: Combinatorics

Discrete Math for CS

k-combination: Select k objects from a set of n objects where order does not

matter and repetitions are not allowed.

Such a selection is called a k-combination.

Example: You have n things whose differences are not important to youand each time you pick one it is no longer available to be chosen again.

Pick 5 students from a class of 20 to set up chairs for a school assembly. How many different teams of students can you form?

Page 21: Chapter 6: Combinatorics

Discrete Math for CS

k-combinations: C(n,k) = the number of ways of choosing k objects from a set of

n objects without repetition and order does not matter

If order matters we get the number P(n,k).

By the multiplication principle there are k! orderings of each distinct choice of k elements. So the number of k-permutations of k distinct objects selected from n objects is the same as the number of unordered ways to select the same objects times the number of ways to order the objects chosen.

P(n,k) = C(n,k) •k!

So C(n,k) = P(n,k) k!

Page 22: Chapter 6: Combinatorics

Discrete Math for CS

Example: In a Chinese Restaurant you can order any three dishes for

what they call the 3-combination meal. How many different 3-combination meals are there if there are 7 main dishes on teh menu.

It doesn't make any difference what order you receive the dishesin and we assume you must order three different dishes.

So order doesn't matter and there is selection without replacement.

C(7,3) = 7!/4!3! = 35

Page 23: Chapter 6: Combinatorics

Discrete Math for CS

k-selection: We finally consider an unordered selection of k objects from a

collection of n objects with repetitions allowed.

This is called k-selection

Example: Since order is unimportant and repetitions are allowed we cangroup all like-objects together.

Suppose we want to make a k-selection of 5 objects from the letters a, b and c.

<group of as>|<group of bs>|<group of cs>

For example aa|b|cc or aaa||cc.

This reduces to finding out how many ways we can insert the two markers, | and |, in a sequence of 7 slots. The other 5 slots are letters.

This number is C(7,2) = 21.

Page 24: Chapter 6: Combinatorics

Discrete Math for CS

Example: Five dice are thrown. How many different outcomes are

possible if an outcome is a list of the upper faces in ascending order.

Each dice has 6 outcomes. If 5 dice are thrown we have the result

<sequence of 1s>|<sequence of 2s>| ...|<sequence of 6s>

and we are really asking how many ways we can insert 5 markers in 10 slots. For example 111||3||5|. This is C(10,5) = 252.

Page 25: Chapter 6: Combinatorics

Discrete Math for CS

k-selection formula: The number of k-selections from n items is:

C(n+k-1,n-1) = (n+k-1)!/k!(n-1)!

Page 26: Chapter 6: Combinatorics

Discrete Math for CS

Summary:

Order Matters Order Doesn't Matterk-selection: (n+k-1)!/k!(n-k)!

k-permutation: n!/(n-k)! k-combination: n!/k!(n-k)!

k-sample: nk

Page 27: Chapter 6: Combinatorics

Discrete Math for CS

Further Examples: The key is to know what formula to use.

In a National Lottery a twice-weekly drawing takes place in which 6 numbers are randomly drawn from the numbers 1..49.

Problem: Find the probability of winning.

Analysis: Without replacement and order does not matter.

Solution:

k-combination: C(49,6) ways to choose the numbers

Prob(winning) = 1/C(49,6) = 1/13,983,816

Page 28: Chapter 6: Combinatorics

Discrete Math for CS

Further Analysis: Should I buy a ticket?

Statistically this equates to calculating the statistical outcome of buying a ticket.

Simplify the situation and assume there is only one winner. The initial lottery pot in NYS is about $3M.

Outcome = amount won * prob of winning + amount lost * prob of losing

= 3,000,000 * 1/13,983,816 + -1 * 13,983,815/13,983,816

= -0.79

So the outcome is negative and this is not a good bet.

Page 29: Chapter 6: Combinatorics

Discrete Math for CS

Further Analysis: What if you win $10 if you get three numbers out of the 6? What

is the probability of this happening?

Prob(guessing 3 numbers correctly) = number of ways of guessing three correct numbers/ number of ways of guessing.Guessing 3 correct numbers from the 6 correct numbers canbe done C(6,3) ways – no repeats, order unimportantGuessing three incorrect numbers can be done C(43,3) ways.Total number of winning combinations is: C(6,3) x C(43,3) = 246,820

Prob(winning something) = 246,820/13,983,816 = 0.017

Page 30: Chapter 6: Combinatorics

Discrete Math for CS

Example: Twelve people, including Peter and Mary are candidates for a

committee of 5. How many different committees are possible?

including Peter and Mary, excluding Peter and Mary, containing either Peter and Mary but not both?

Answers: How many committees? No replacement, order unimportant – C(12,5) = 792. Including Peter and Mary? Peter and Mary are selected so we need to choose three more from 10 people – C(10,3) = 120 excluding Mary and Peter? Select 5 from 10 – C(10,5) = 252 either Peter or Mary? Mary only – C(10,4). Peter only – C(10,4) either Mary or Peter = 2 x C(10,4) = 420 Alternatively, the 792 possible committees fall into one of the above categories. So the third category has 792 – 120 – 252 = 420 members.

Page 31: Chapter 6: Combinatorics

Discrete Math for CS

Binomial Expansion: The numbers C(n,k) are the coefficients of the expansion of

(a+b)n.

This turns out to be true because when we multiply out (a+b)(a+b)(a+b) we are essentially selecting either an a or a b from each term (a+b) and the coefficient of akbn-k is the number of ways of selecting k a's from n without repetition and ignoring order.

Of course, 0! = 1 by definition.

Example: (a+b)3 = (a+b)(a+b)(a+b) = a3 + 3a2b + 3ab2 + b3

and C(3,0) = 1, C(3,1) = 3, C(3,2) = 3 and C(3,3) = 1

Page 32: Chapter 6: Combinatorics

Discrete Math for CS

(a+b)n:

The above is called the binomial expansion of (a+b)n.

Each C(n,k) is called a binomial coefficient.

(a+b)n = C(n,0)an + C(n,1)an-1b + C(n,2)an-2b2 + ... + C(n,n-1)abn-1 + C(n,n)bn

Page 33: Chapter 6: Combinatorics

Discrete Math for CS

Pascal's Triangle:

1

11

11 2

11 33

11 44 6

Page 34: Chapter 6: Combinatorics

Discrete Math for CS

Pascal's Triangle:

1

11

11 2

11 33

11 44 6

C(n,k) = C(n-1,k-1) + C(n-1,k)

Page 35: Chapter 6: Combinatorics

Discrete Math for CS

Theorem:

C(n,k) = C(n-1,k-1) + C(n-1,k).

Proof: C(n-1,k-1) + C(n-1,k) = (n-1)!/(k-1)!(n-k)! + (n-1)!/k!(n-k-1)!

= (n-1)!/(k-1)!(n-k-1)! x (1/(n-k) + 1/k)

= (n-1)!/(k-1)!(n-k-1)! x n/((n-k)k)

= n!/(n-k)!k! = C(n,k)

Page 36: Chapter 6: Combinatorics

Discrete Math for CS

Final Topic: How many arrangements can be made of objects (possibly

repeated) from a given set?

Example: Consider the word “defender”. This contains 4 distinct letters, some repeated more than once. We can arrange these letters 8! ways, but then we will see some are repetitions since we can't tell one from another., for example. Since there are 2 d's there are 2! ways of arranging these. Since there are 3 e's there are 3! ways of arranging these.

There are a total of 8!/2!3! distinct orderings of the letters of “defender”.

Page 37: Chapter 6: Combinatorics

Discrete Math for CS

Rearrangement Theorem:

There are n!/(n1!n

2!n

3!...n

k!) ways of rearranging a collection of n

objects where there are n1 identical objects of one type, n

2

identical objects of another type, ..., and nk identical objects of a

final type. Clearly n = n1 + n

2 + ... +n

k.

Page 38: Chapter 6: Combinatorics

Discrete Math for CS

Example: How many ways can we divide 15 students into 3 groups of 5

so that each group studies a different topic?

We have 15 objects divided into 3 identical groups of 5 each.

This can be done 15!/(5!5!5! ) = 68,796 ways.

Alternatively, imagine lining the 15 people up so that the first 5 go into group 1, the next 5 into group 2 and the last 5 into group 3.

For each distinct set of the first 5 students there are 5! different orders we can ignore; likewise for the other two groups.

So there are 15!/(5!5!5!) different group assignment possibilities.

Page 39: Chapter 6: Combinatorics

Discrete Math for CS

Multinomial: The term n!/(n

1!n

2!n

3!...n

k!) is called a multinomial.

It is the form of the coefficients of the expansion of

This follows since the term x1

n1x2

n2...xknk gets its coefficient by

selecting n1 instances of x

1, n

2 instances of x

2, etc from the

bracketed terms (x1 + x

2 + ... + x

k).

This is the same as arranging n objects, n1 of type x

1, n

2 of type

x2, etc.

(x1 + x

2 + ... + x

k)n

Page 40: Chapter 6: Combinatorics

Discrete Math for CS

Example: Find the coefficient of x3y2z4 in the expansion of (x+y+z)9.

Find the coefficient of x3y2 in the expansion of (x+y+3)7.

9!/(3!2!4!) = 1260

If this were (x+y+z)7, then the coefficient of x3y2z2 would be7!/(3!2!2!) = 210. Since z = 3, the coefficient is 210x32. = 1890.

Page 41: Chapter 6: Combinatorics

Discrete Math for CS

Algorithm Efficiency: Time and Space efficiency of a computer algorithm is a big

deal.

We often measure these aspects of an algorithm by counting how much work is done or how much time is consumed as a function of n, the size of the initial problem.

Example: Find if a particular word, X, is in a dictionary of size n.

Sequential Search:This search compares X with the first word, the second word and so onuntil either it is found or not found. The “worse case” requires n comparisons – not there and X starts with a lot of zzz.

Binary Search: Compare X to the middle word and use lexicographical ordering of words to decide whether to continue the search in the first half or last half of the dictionary. This method requires 1 + log

2n comparisons.

Page 42: Chapter 6: Combinatorics

Discrete Math for CS

Example: Suppose 5 algorithms – A, B, C, D and E – involve n, 3n2,

2n2+4n, n3 and 2n elementary operations. If each operation takes 1 millisecond estimate the running times for n = 1, 10, 100 and 1000.

A B C D E

n1 1 ms 3 ms 6 ms 1 ms 2 ms

10 10 ms 300 ms 240 ms 1 sec 1.024 sec100 100 ms 30 sec 20.4 sec 0.28 hr1000 1000 ms 0.83 hr 0.56 hr 11.6 days

3n2 2n2+4n n3 log2n

4x107 cent10176 cent

polynomial time exponential timelinear time

Polynomial time algorithms with the same degree arein the same ballpark.

Page 43: Chapter 6: Combinatorics

Discrete Math for CS

Time Complexity Functions: Suppose f(n) and g(n) measure the time complexity of two

algorithms.

We say f(n) is of order at most g(n) (written O(g(n))) if there exists a positive constant C such that |f(n)| <= C|g(n)| for all but a finite number of distinct values of n.

Colloquially we say “f(n) is big-O of g(n)”.

Page 44: Chapter 6: Combinatorics

Discrete Math for CS

Example: Show 2n2+4n is O(n2).

Note: It is also the case that n2 <= 2n2+4n for n>=1 so we can say n2 is O(2n2+4n) or that these two time complexity functions have the same order of magnitude.

n <= n2 for n >= 1. 2n2+4n <= 2n2+4n2 = 6n2. for n >= 1

So letting C=6 in the above definition we can conclude 2n2+4n is O(n2)

Page 45: Chapter 6: Combinatorics

Discrete Math for CS

Order of Magnitude Hierarchy There is a hierarchy of functions, each of which is a greater

order of magnitude than its predecessors.

There are actually many such hierarchies but one is:

1 log n n n2 n3 n4 ... nk ... 2n

Exercise: Where does nlogn (n(log n)) fit in?

As n increases the values of the above time complexity functions increasemore rapidly on the right than on the left.

Page 46: Chapter 6: Combinatorics

Discrete Math for CS

n

20481024512256128643216

8421

Page 47: Chapter 6: Combinatorics

Discrete Math for CS

Example: A function like f(n) = 9n+3n6+7log n is assigned the time

complexity of O(n6) because 9n is O(n), 3n6 is O(n6) and 7log n is O(log n). Since n and 7log n occur earlier in the hierarchy than n6 we can say both n and 7log n are O(n6).

Since all three terms of the function f(n) are O(n6) the function itself is O(n6).

Page 48: Chapter 6: Combinatorics

Discrete Math for CS

What to Count: Consider the following pseudocode:

Determine the time complexity by counting the number of times the assignment statement executes.

begin for i:= 1 to 2n do for j:= 1 to n do for k := 1 to j do x := x + 1end

The outer loop executes 2n times. The loop indexed by j executes n times for each iFor each value of j the statement x := x+1 executes j timesSo for each value of i, the statement x := x+1 executes 1 + 2 + ... + n = 1/2(n(n+1)) times. So the time complexity of the loops is T(n) = 2n(1/2(n(n+1))) = n2(n+1) So T(n) is O(n3).

Page 49: Chapter 6: Combinatorics

Discrete Math for CS

Pascal's Triangle:Pascal's Triangle:Pascal's Triangle:Pascal's Triangle:Pascal's Triangle: