Presentation is loading. Please wait.

Presentation is loading. Please wait.

Counting 3-Dec-18.

Similar presentations


Presentation on theme: "Counting 3-Dec-18."— Presentation transcript:

1 Counting 3-Dec-18

2 Philosophy This set of slides is about counting things I believe:
Much of it is a branch of mathematics called combinatorics I believe: Mathematics is logical and makes sense If you understand the math, you don’t need to memorize a bunch of equations Some people believe: If you memorize a bunch of equations, you don’t need to understand the math The second approach works a lot of the time, but… Real-life problems don’t come with a little tag attached telling you what equation to use

3 Number systems A number system base B is a set of B “digits” representing the numbers 0 through B-1 Example: Decimal (base 10) uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 Example: Ternary (base 3) uses the digits 0, 1, and 2 If the base isn’t obvious from context, it is written as a subscript of the number Example: means “2120 base 3” Bases used in computer programming: Decimal Binary (base 2) uses the binary digits (bits) 0 and 1 Octal (base 8) uses the octal digits 0, 1, 2, 3, 4, 5, 6, and 7 Hexadecimal, or “hex” (base 16) uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F

4 Counting in various bases
The rule for getting the “next number” (adding one) in any system is always the same: If a digit is the largest possible, reset it to zero and add 1 to the digit on the left Decimal examples: 9  10, 1299  1300,  83990 Octal examples: 7  10, 1277  1300,  75770 Hex examples: F  10, A2FF  A300, F5FEF  F5FF0 Binary examples: 1  10, 1011  1100,  The same number is usually represented different ways in different number systems Example: 4810 = 3016 = 608 = But: 510 = 516 = 58 = 1012

5 Powers In any base B, the digits of a number represent a multiplier of a power of B Example: = 4* * * *100 Example: = 4* * * * = 4* * * *1 = It is useful to learn the powers of 2: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 It is especially useful to remember that 210 = 1024 is approximately equal to 103 = 1000 In any base B, you can represent BK different numbers (0 through BK-1) with K digits

6 Counting subsets Given a set of N distinct values, {A, B, C}, there are exactly 2N possible subsets of those values You can show this by counting in binary: A 1 means it’s in the subset, a 0 means it’s not { A, B, C }  { } (the empty subset)  { C }  { B }  { B, C }  { A }  { A, C }  { A, B }  { A, B, C } (the “improper” subset)

7 Grouping Suppose we have a collection of N unique items, and we want to sort them into two piles The piles don’t need to be equal in size In fact, it’s OK for one of the piles to be empty How many different ways can we sort the items into two piles? Suppose we have a collection of N unique items, and we want to sort them into three piles In fact, it’s OK for one, or even two, of the piles to be empty How many different ways can we sort the items into three piles?

8 Permutations Given a set of N distinct values, a permutation is a possible ordering of those values Example: There are two permutations of the set {A, B}: AB and BA Example: There are six permutations of the set {A, B, C}: ABC, ACB, BAC, BCA, CAB, CBA Example: There are 24 permutations of the set {A, B, C, D}: ABCD, ACBD, BACD, BCAD, CABD, CBAD, ABDC, ACDB, BADC, BCDA, CADB, CBDA, ADBC, ADCB, BDAC, BDCA, CDAB, CDBA, DABC, DACB, DBAC, DBCA, DCAB, DCBA

9 Counting permutations
To find the permutations of {A, B, C, D, E}, there are: Five possible choices for the first item Four possible choices for the second item Three remaining possible choices for the third item Two remaining possible choices for the fourth item, and Only one possible “choice” for the final item For any positive integer N, we define N! (“N factorial”) as the product of all the positive integers up to and including N Example: 5! = 1 * 2 * 3 * 4 * 5 = 120 Given any N distinct items, there are N! possible permutations of those items

10 Array permutations Sorting an array is finding a particular permutation of the elements in the array that satisfies some condition If you have an array of 50 elements, there can be as many as 50! possible ways to order those values This is an extremely large number! If some of the elements are the same, there are fewer possible orderings For example, if all the elements in the array are equal, there is only one possible ordering

11 Computing permutations
It’s easy to describe a recursive procedure for finding all permutations (it’s not so easy to implement): There is only one permutation of one object (base case): A Given all the permutations of N objects, find the permutations of N+1 objects by inserting the new object in each possible position of each permutation of N objects Example: From A, get BA and AB Example: From BA and AB, get CBA, BCA, BAC, CAB, ACB, ABC

12 Permutations of non-unique values
How many possible permutations are there of the values A, A, A, B, C ? If the values were all distinct, there would be 5! possible permutations--but they aren’t all distinct We can’t distinguish one ABAAC from another ABAAC But if we pretend that we can tell one A from another, we find that there are 3! = 6 possible ways to get ABAAC: ABAAC, ABAAC, ABAAC, ABAAC, ABAAC, ABAAC In fact, every other permutation, such as ACABA, is represented in six different ways This is because there are six ways to permute the As: AAA, AAA, AAA, AAA, AAA, AAA Hence, there are really only 5!/3! = 120/6 = 20 possible permutations: AAABC, AABAC, AABCA, ABAAC, ABACA, ABCAA, BAAAC, BAACA, BACAA, BCAAA, AAACB, AACAB, AACBA, ACAAB, ACABA, ACBAA, CAAAB, CAABA, CABAA, CBAAA

13 More permutations How many possible permutations are there of the values A, A, A, B, C, C ? How many ways could a 32-bit integer have exactly ten of its bits set to 1 ? Second problem: Don’t forget to permute the zeros as well as the ones.

14 Subset permutations Suppose there are eight entrants in a contest, and you need to choose first, second, and third prize winners There are 8 ways you could choose the first prize winner There are 7 remaining ways to choose the second prize winner There are 6 remaining ways to choose the third prize winner You don’t care about the rest Hence, there are 8 * 7 * 6 = 336 ways to choose the top three prize winners This looks like part of 8! = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 The part that is missing is 5! = 5 * 4 * 3 * 2 * 1 You could compute this as 8! / 5! = / 120 = 336 There are N!/(N-K)! ways to choose a sequence (permutation) of K items from a set of N distinct items

15 Computational common sense
Suppose there are 50 entrants in a contest, and you need to choose first, second, and third prize winners There are 50! / (50-3)! = 50!/47! ways to do this You do not want to compute this as 50!/47! Why not? Hint: It isn’t a matter of simple efficiency The direct solution, 50 * 49 * 48 = , is easy to compute--and this is how you should do it, not with factorials However, the answer 50!/47! would be perfectly satisfactory on a test Unless, of course, I ask for an efficient answer

16 Choices Suppose there are 50 entrants in a contest, and you need to choose three (equal) winners--order doesn’t matter You might choose Molly, Bill, and Sally; or you might choose Bill, Molly, and Sally; or you might choose Sally, Bill, and Molly; or… There are 50 * 49 * 48 = (that is, 50! / (50 - 3)! ) ways to choose the three winners in order But…you can arrange these three winners in any of 3! = 6 different orders (permutations) 117600/6 = = 50! / (50-3)! / 3! = 50! / ((50-3)! * 3!) More generally, there are N!/((N-K)! * K!) ways to choose K of N items, when the order of the K items is not important

17 Counting problems Suppose you have 20 unique items
How many ways are there to sort them into two piles, each containing 10 items? Suppose you have 30 unique items How many ways are there to sort them into three piles, each containing 10 items? Hint: Do one pile at a time

18 Yet more counting problems
How many different 8-letter variable names can you make using just the letters a, b, and c ? How many different variable names can you make of up to 8 letters, using just the letters a, b, and c ? Suppose you have a large supply of each of three different kinds of items How many different permutations of 8 items can you make? How many different combinations of 8 items can you make? Problem 1b: 3 ^0 + 3^1 + 3^2 + 3^3 + …+ 3^8 Problem 2b: 9 choices for a (0-8), 9-a choices for b, c fixed: sum(i from 0 to 9) = 45

19 Summary Congratulations! You have just learned a semester’s worth of combinatorial mathematics! You won’t use this material every day, but--I wouldn’t teach it if it weren’t useful Warning: This material may make sense now, but it won’t tomorrow, unless you try a few practice problems very soon, while the ideas are still fresh in your mind

20 The End On average, men who smoke cut their lives short by 13.2 years, and female smokers lose 14.5 years. —U.S. Surgeon General Admiral Richard H. Carmona


Download ppt "Counting 3-Dec-18."

Similar presentations


Ads by Google