Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Combinatorics.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Combinatorics."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Combinatorics

2 Introduction –Combinatorics is the study of the arrangement of discrete objects –Who Cares? Counting is important whenever we have finite resources Examples: –Words of memory needed to store … –Instructions needed to solve … –Number of hits per second supported by … –A password must be between six and eight characters long. The characters can be a digit or a letter (case sensitive). Each password must include at least one digit. How many passwords can we support?

3 Useful Counting Cheat Sheet –Summation Notation –Summation Properties –Summation Formulas

4 Basic Counting Principles –Multiplication Principle If there are n 1 possible outcomes for a first event and n 2 possible outcomes for a second even, then there are n 1  n 2 possible outcomes for the sequence of the two events. Extends (by induction) to apply to a sequence of n events –Examples 1.How many different bit strings are there of length seven? 2.If A & B are finites sets, then |A  B| = _______. 3.How many possible 7 digit phone numbers are there? What if you can’t use a digit more than once?

5 Basic Counting Principles –Addition Principle If A and B are disjoint events with n 1 and n 2 possible outcomes, then the total number of possible outcomes for event “A or B” is n 1 + n 2. Extends (by induction) to n disjoint events –Examples 1.How many ways are there to elect an ACM president if the president must be a CS faculty member (from 7) or a CS major (from 250)? 2.If you have two disjoint sets, A & B, |A  B| =_________. 3.How many 7 digit phone numbers begin with 441 or 781?

6 Basic Counting Principles Practice Example US Social Security Numbers are 9 digits (0..9) 1.How many Social Security numbers are there? 2.How many are even? 3.How many have all even digits? 4.How many are palindromes? 5.How many have none of their digits equal to 8? 6.How many have at least one digit equal to 8? 7.How many have exactly one 8?

7 Basic Counting Principles –Inclusion-Exclusion Principle If you have two arbitrary sets, A & B, |A  B| = |A| + |B| - |A  B| Example: How many bit strings of length eight start with a 1 or end with the two bits 00? –Answer: 2 7 + 2 6 – 2 5 = 160 Subsumes the Sum Rule Extends (by induction) to n arbitrary sets (see p. 228 of text) AB A–BB–A ABAB

8 Example: Internet Address (IPv4) Sample: 137.28.109.33 Translation: 32-bits 10001001 00011100 01101101 00100001 Interpretation Restrictions –1111111 not available as netid of Class A network –All 0 s or all 1 s are not allowed as any hostid How many possible computer Internet addresses? –Netid for Class A is 7 bits long, –Netid for Class B is 14 bits long, –Netid for Class C is 21 bits long Plus some additional classes

9 Example: Internet Address How many possible computer Internet addresses? Class A: ((2 7 – 1) * (2 24 – 2)) + 1111111 prohibited as netid Class B: (2 14 * (2 16 – 2)) + all 0’s and all 1’s prohibited Class C: (2 21 * (2 8 – 2)) = 3,737,091,842.

10 Permutations –An ordered arrangement of n elements from a set –An r-permutation is an ordered arrangement of r elements taken from a set of n elements –The number of r-permutations of n items P(n,r) P(n,r) = n! / (n – r)! –Examples: 1.How many ways are there to select a 1 st, 2 nd, and 3 rd place winner from 100 different people? 2.How many 7 digit phone numbers exist where the numbers can’t be repeated? –Notice: P(n,n) = n! P(n,0) = 1 P(n,1) = n

11 Combinations –An unordered arrangement of n elements from a set unordered means order does not matter! –An r-combination is an unordered arrangement of r elements taken from a set of n elements –The number of r-combinations of n items C(n,r) C(n,r) = n! / r!(n – r)! = P(n,r) / r! –Examples: 1.Quality Control wants to test 25 microprocessor chips from the 300 manufacturers. In how many ways can this be done? 2.How many ways can I select 5 different fruits from choice of 10? –Notice: C(n,n) = 1C(n,0) = 1 C(n,1) = nC(n,r) = C(n,n-r)

12 Repetition –Thus far we have assumed that items can not be used twice in one draw –What if they can? Letters in a password Selecting from bag of items with copies –Permutations with repetition P R (n,r) = n r How many 7 digit phone numbers are possible? –Combinations with repetition C R (n,r) = C(r+n–1, r) = (r+n-1)! / [r!  (n-1)!] How many ways can I select 7 pieces of fruit from a selection of apples, oranges, and pears?

13 Repetition –How many different strings can be made by reordering the letters of the word … –“FAILURE”? –“SUCCESS”? We have three Ss, two Cs, one U, and one E. Notice that the three Ss can be placed into the seven positions in C(7,3) ways, leaving four blank positions The Cs can be placed into those four positions C(4,2) ways, leaving two free positions. The U can be placed into those two positions in C(2,1) ways, leaving one free position (allowing E to be placed in C(1,1) ways). From the product rule: C(7,3) * C(4,2) * C(2,1) * C(1,1) which equals (7! * 4! * 2! * 1!) / (3! * 4! * 2! * 2! * 1! * 1! * 1! * 0!) which equals 7! / (3! * 2! * 1! * 1!) which equals 420

14 Repetition –Permutations with indistinguishable objects P I (n,n 1,n 2,…,n k ) = n! / (n 1 !  n 2 !  …  n k !)

15 Repetition –Example: How many ways are there to build four hands of 5 cards from the standard deck of 52 cards? Solution I: Combinations of Objects –Use Combinations for each player’s hand –Use Product Rule for total C(52,5) * C(47,5) * C(42,5) * C(37,5) = 52! / (5!5!5!5!32!) Solution II: Permutations of Indistinguishable Objects –5 indistinguishable objects of each of four types (one for each hand) –32 indistinguishable objects of a fifth type (undealt) 52!/(5!5!5!5!32!)

16 Probability –An area of mathematics that has its origin in counting Developed to study gambling games –circa 1650 by Pascal –generalized circa 1812 by Laplace –Finite Probability experiment – a procedure that yields one of a given finite set of possible outcomes sample space – the finite set of possible outcomes event – a subset of the sample space The probability of an event E, which is a subset of a finite sample space S of equally likely outcomes, is p(E) = |E| / |S|

17 Probability Examples –What is the probability of picking a winning combination of six numbers out of 40 (no repeats)? S : set of all six-number sequences taken from 40 E : the target number |S| = C(40,6) = 3,838,380 |E| = 1  p(E) = 1/3,838,380 (approx. 0.00000026) –What is the probability that a hand of five cards in poker contains four of a kind? S : set of all possible poker hands E : set of all possible four-of-a-kind hands |S| = C(52,5) = 2,598,960 |E| = # of ways to pick rank * # of ways to pick other card = C(13,1) * C(48,1) = 624  p(E) = 624 / 2,598,960 (approx. 0.00024) product rule

18 Probability –Properties 0  p(E)  1 p(S) = 1 p(E) = 1 – p(E) p(E 1  E 2 ) = p(E 1 ) + p(E 2 ) – p(E 1  E 2 ) –disjoint events »p(E 1  E 2 ) = 0 »p(E 1  E 2 ) = p(E 1 ) + p(E 2 ) p(E 2 | E 1 ) = p(E 1  E 2 ) / p(E 1 ) –independent events »p(E 2 |E 1 ) = p(E 2 ) »p(E 1  E 2 ) = p(E 1 )  p(E 2 ) Each outcome “equally likely” implies p(a) = 1/n where a  S and |S| = n. Called uniform distribution Conditional Probability

19 Probability Practice 1.What is the probability of drawing either an ace or a spade from a standard deck of cards? 2.What is the probability of drawing 2 aces in a row? 3.What is the probability of drawing 2 aces in a row (with replacement)?

20 Expected Value First an example Find the average height of four students with heights 68, 72, 67, and 74 inches. Avg height = (68+72+67+74)/4 = 70.25 inches Find the average height of 1000 students with the following heights Avg height = 66(.2)+68(.25)+71(.3)+72(.1) +74(.15) = 69.8 inches Number of Students Height in Inches 20066 25068 30071 10072 15074

21 Expected Value Definitions Suppose we have a probability space with the sample space {e 1, e 2, …, e n } A random variable f (e i ) on the sample space is a real number associated with each outcome e i The expected value (or average) of f (e i ) is given by

22 Expected Value Example Suppose we have an asymmetrical 6-side die: p(1) = 0.2p(2) = 0.15p(3) = 0.1 p(4) = 0.25p(5) = 0.12p(6) = 0.18 1.A random variable on this sample space is the number written on the side The expected value of this random variable is 1(.2) + 2(.15) + 3(.1) + 4(.25) + 5(.12) + 6(.18) = 3.48 2.A random variable on this sample space is the mod 2 function of the number on the side. The expected value of this random variable is 1(.2) + 0(.15) + 1(.1) + 0(.25) + 1(.12) + 0(.18) =.42

23 Application: Monte Carlo Techniques –Deterministic algorithms don’t always exist or aren’t always practical –Use random sampling to compute results –Example: Value of  Let P = set of randomly selected points –(x,y) | 0  x  1  0  y  1 Let S = { (x,y) | (x,y)  P  } Let a = | S | / | P |   4a

24 Application: Monte Carlo Techniques –Example: Efficiency of backtracking algs Generate a “typical” path in the tree using random choices, then estimate the number of nodes in the tree based on this path. Let m 0 be the number of feasible children of the root Randomly generate a feasible node at level 1. Let m 1 be the number of feasible children of this node. Randomly generate a feasible node at level 2. Let m 2 be the number of feasible children of this node.... Randomly generate a feasible node at level i. Let m i be the number of feasible children of this node.... Estimate of total number nodes checked: 1 + t 0 + m 0 t 1 + m 0 m 1 t 2 + … + m 0 m 1 … m i-1 t i + …

25 Application: Monte Carlo Estimate int MonteCarloEstimate( ) { Node v = root of state space tree; while(v has children and is not a solution) { count number of children of v add number of children into total count using 1 + t 0 + m 0 t 1 + m 0 m 1 t 2 + … + m 0 m 1 … m i-1 t i + … store number of feasible children of v v = randomly selected feasible child of v } return total count }

26 Practice Problems –Section 3.2: 3, 5, 6,12, 52 – 56, 69 - 70 –Section 3.3: 7, 10, 12 –Section 3.4: 4, 5, 11, 12, 20 – 23, 29 – 37, 65 –Section 3.5: 17 – 24, 29 – 38, 44


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Combinatorics."

Similar presentations


Ads by Google