# HO34 - Maggie Johnson CS103A Handout#34 Combinatorics Key...

This preview shows pages 1–4. Sign up to view the full content.

Maggie Johnson Handout #34 CS103A Combinatorics Key Topics: * Sum Rule and Product Rule Review * The Pigeonhole Principle * Permutations and Combinations * Binomial Coefficients and the Binomial Theorem * Permutations and Combinations with Repetition * Summary of Theorems Sum Rule and Product Rule Review The Sum Rule : If there are n(A) ways to do A, and n(B) ways to do B, and these tasks cannot be done at the same time, then the number of ways to do either task is n(A) + n(B). This can be expanded to any number of terms. The Product Rule : Suppose that a procedure can be broken down into two tasks. If there are n(A) ways to do A, and n(B) ways to do B after the first task has been done, then there are n(A) * n(B) ways to do the procedure. This can be expanded to any number of terms. There are 18 math majors and 200 CS majors at Stanford. How many ways are there to pick two representatives, so that one is a math major and one is a CS major? How many ways are there to pick one representative who is either a math major or a CS major? How many different three-letter initials are there (with repetition and without)? How many different three-letter initials are there that begin with the letter A? How many bit strings of length 10 begin and end with a 1?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
How many strings are there of lowercase letters of length four or less? You may be wondering why we care about counting at all as computer scientists. Well, take a look at the following: What is the value of k after the following code has been executed? k = 0; for (i1 = 1; i1 <= n1; i1++) k = k + 1; for (i2 = 1; i2 <= n2; i2++) k = k + 1; . . . for (im = 1; im <= nm; im++) k = k + 1; What is the value of k after the following code has been executed? k = 0; for (i1 = 1; i1 <= n1; I1++) { for (i2 = 1; i2 <= n2; i2++) { ... // m nested loops for (im = 1; im <= nm; im++) { k = k + 1; // in the innermost loop } } } Now that you are all warmed up, we can look at some more complex problems using the sum and product rule together. Example In one version of BASIC, the name of a variable is a string of one or two alphanumeric chars, where uppercase and lowercase are not distinguished. So much for meaningful variable names. Incidentally, alphanumeric means either one of the 26 English letters or one of the 10 digits. In addition, all variables must begin with a letter and must be different from the five reserved words. How many different variable names are possible in this (very simplistic) version of BASIC? Solution: Let V equal the number of different variable names. Let V1 be the number of variable names one-char long, and V2 be the number of variable names two-chars long. So,
by the Sum Rule, V = V1 + V2. V1 must equal 26 since we can’t start with a digit. By the Product Rule, V2 = 26 * 36. But five of these strings must be excluded so we get: V2 = 26 * 36 - 5 = 931. V = V1 + V2 so V = 26 + 931 = 957 different names. The Pigeonhole Principle

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

HO34 - Maggie Johnson CS103A Handout#34 Combinatorics Key...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online