1 / 98

CPSC 221: Data Structures Lecture #23 Counting

CPSC 221: Data Structures Lecture #23 Counting. Steve Wolfman (stolen from Alan J. Hu who stole some from me which I stole from Kim Voll) 2011W2. Learning Goals. After this unit, you should be able to:

marc
Download Presentation

CPSC 221: Data Structures Lecture #23 Counting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPSC 221: Data StructuresLecture #23Counting Steve Wolfman (stolen from Alan J. Hu who stole some from me which I stole from Kim Voll) 2011W2

  2. Learning Goals After this unit, you should be able to: • Apply counting principles to determine the number of arrangements or orderings of discrete objects, with or without repetition, and given various constraints. • Use appropriate mathematical constructs to express a counting problem (e.g. counting passwords with various restrictions placed on the characters within). • Identify problems that can be expressed and solved as a combination of smaller sub problems. When necessary, use decision trees to model more complex counting problems. 2

  3. Counting: Motivation • This is sometimes really useful, e.g.: • How many possible passwords, encryption keys, etc. • How many possible legal inputs, how many possible data structure configurations, how many paths through a network, etc. • Crucial step in computing probabilities. • Sometimes fun – like a puzzle or game.

  4. Counting: Warning • However, once you get beyond basic problems, it can be very subtle, and there are no simple tricks that always work. Sometimes, you just have to think really, really hard and really really carefully! • Epp textbook is pretty good. Must read on your own. (3rd ed 6.1-6.5, 4th ed. 9.1-9.6) • Old slides follow textbook presentation closely. • I am going to try to de-mystify some of the formulas.

  5. Outline • Case-Splitting, Decision Trees • “Addition Rule” Deconstructed • “Multiplication Rule” Deconstructed • Alan’s “Division Rule” • Permutation and Combination Formulas • Dealing with Overlapping Cases: Inclusion-Exclusion

  6. Counting • The basic problem is to count how many items are in some set, which might be specified in a non-obvious way. • E.g., how many different min-heaps are possible with the numbers 1,…,7? • How many poker hands beat a pair of kings? • Etc.

  7. Basic Technique: Case Splitting • This is the most fundamental way to break a problem down into simpler parts. • The cases must be disjoint (no overlap). (We’ll handle overlap later.) • Combined, the cases must cover all possibilities. • Count each case separately, then add them all up. • Whenever you are totally lost, always fall back to case-splitting. Point of interest: disjoint but covering sets form a partition.

  8. Case Splitting Example • What’s the population of Canada?

  9. Case Splitting Example • What’s the population of Canada? • What’s the population of BC? • What’s the population of AB? • What’s the population of SK? • What’s the population of MB? • etc. • Add them all up!

  10. Other case splits are possible • What’s the population of Canada? • How many men in Canada? • How many women in Canada? • Add them all up!

  11. “Addition Rule” Demystified • From Epp: “Suppose a finite set A equals the union of k distinct mutually disjoint subsets A1, A2, …, Ak. Then N(A) = N(A1)+N(A2)+…+N(Ak).”

  12. Addition Principle (Sum Rule) If X1, X2, …, Xt are pairwise disjoint sets (i.e., Xi ∩ Xj = ∅ ∀i ∀j s.t. i, j∈{1, 2, …, t }, i ≠ j), then the number of ways to select an element from any of X1 orX2 or … or Xt is: | X1 | + | X2 | + … + | Xt |

  13. Case Splitting Error: Missing Cases • What’s the population of Canada? • Liberals • Conservatives • NDPs • Add them all up! • If we miss any cases (e.g., Greens, Rhinoceros Party, Marijuana Party, people without political parties, etc.), we’ll undercount.

  14. Case Splitting Error: Overlap • What’s the population of Canada? • Francophone Canadians • Anglophone Canadians • Add them all up! • Double counts anyone who speaks both. • (We’ll see how to deal with overlap later.) Also misses anyone who speaks neither, of course!

  15. Cases can be split into sub-cases • What’s the population of Canada? • What’s the population of BC? • What’s the population of Vancouver? • What’s the population of Burnaby? • What’s the population of Richmond? • etc. Add them all up. • What’s the population of AB? • What’s the population of SK? • What’s the population of MB? • etc. • Add them all up!

  16. Decision Trees • When you split cases into subcases, it’s often nice to draw this as a tree. • These are informally called “decision trees”, although a proper decision tree has probabilities on branches. We’ll just ignore this. (Epp calls them “possibility trees” which is descriptive, but not standard.)

  17. Case Splitting Example • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • How many have 2 at the root? • How many have 3 at the root? • How many have 4 at the root? • How many have 5 at the root? • How many have 6 at the root? • How many have 7 at the root? • Add them all up!

  18. Case Splitting Example • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? ??? • How many have 2 at the root? 0 • How many have 3 at the root? 0 • How many have 4 at the root? 0 • How many have 5 at the root? 0 • How many have 6 at the root? 0 • How many have 7 at the root? 0 • Add them all up!

  19. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at left child? • And have 4 at left child? • And have 5 at left child? • And have 6 at left child? • And have 7 at left child? • Add them all up! • People often draw these as trees.

  20. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? ??? • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ??? • And have 6 at left child? 0 • And have 7 at left child? 0

  21. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • And have 4 at right child? • And have 5 at right child? • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  22. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • And have 4 at right child? • And have 5 at right child? • 6 and 7 must be leaves of right child • Therefore 3 and 4 are leaves of left child • Therefore 4 possibilities in this case. • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  23. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • And have 4 at right child? • Right subtree leaves are 5, 6: • Right subtree leaves are 5, 7: • Right subtree leaves are 6, 7: • And have 5 at right child? 4 • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  24. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • And have 4 at right child? • Right subtree leaves are 5, 6: 4 • Right subtree leaves are 5, 7: 4 • Right subtree leaves are 6, 7: 4 • And have 5 at right child? 4 • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  25. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • And have 4 at right child? 12 • And have 5 at right child? 4 • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  26. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • Leftmost leaf is 4: • Leftmost leaf is 5: • Leftmost leaf is 6: • Leftmost leaf is 7: • And have 4 at right child? 12 • And have 5 at right child? 4 • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  27. Case Splitting: Splitting into Sub-Cases • How many different min-heaps are possible with the numbers 1,…,7? • How many have 1 at the root? • And have 2 at left child? • And have 3 at right child? • Leftmost leaf is 4: • Leftmost leaf is 5: • Leftmost leaf is 6: • Leftmost leaf is 7: • And have 4 at right child? 12 • And have 5 at right child? 4 • And have 3 at left child? ??? • And have 4 at left child? ??? • And have 5 at left child? ???

  28. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • Leftmost leaf is 4: • Leftmost leaf is 5: • Leftmost leaf is 6: • Leftmost leaf is 7:

  29. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • Leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 5: • Leftmost leaf is 6: • Leftmost leaf is 7:

  30. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • Leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 5: • Next leaf is 4 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 6: • Leftmost leaf is 7:

  31. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • Leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 5: • Next leaf is 4 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 6: • etc. • Leftmost leaf is 7: • etc. What do you call it when you add up the same number a bunch of times?

  32. Multiplication! E.g.: suppose there are 30 NHL teams, each with 23 players. How many players are there in total?

  33. Multiplication! If some case split results in a bunch of identical-sized sub-cases, the total is just the number of cases times the size of each sub-case.

  34. Multiplication! If some case split results in a bunch of identical-sized sub-cases, the total is just the number of cases times the size of each sub-case. • Note that the cases don’t have to be identical, but just have the same number of things in them.

  35. Multiplication Principle (Product Rule) If an operation consists of t steps and: Step 1 can be performed in n1 ways, Step 2 can be performed in n2 ways, … Step t can be performed in nt ways, then the entire operation can be performed in n1n2…nt different ways. Example: How many postal codes begin with the letter V and end with the digit 4? __ __ __ __ __ __

  36. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • Leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 5: • Next leaf is 4 • Next leaf is 6 • Next leaf is 7 • Leftmost leaf is 6: • etc. • Leftmost leaf is 7: • etc. 4 cases with same number of elements in each one.

  37. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • 4 times: • The number of cases with leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 4 cases with same number of elements in each one.

  38. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • 4 times: • The number of cases with leftmost leaf is 4: • Next leaf is 5 • Next leaf is 6 • Next leaf is 7 3 cases with same number of elements in each one.

  39. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • 4 times: • 3 times: • The number of cases with next leaf 5 3 cases with same number of elements in each one.

  40. Case Splitting: Splitting into Sub-Cases • How many have 1 at the root, 2 at left child, and 3 at right child: • 4 times: • 3 times: • 2 times • 1

  41. Permutations and Factorial The number of ways to arrange n distinct items in a row is n! • This comes straight from the product rule(which comes from case splitting) These are called permutations of the n items.

  42. Permutations P(n,r) • What if we only want permutations of r elements out of the n objects? • E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there?

  43. Permutations P(n,r) • What if we only want permutations of r elements out of the n objects? • E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there? • Product Rule: 20^5 if you can win repeatedly • Product Rule: 20*19*18*17*16 if you can win at most one prize.

  44. Permutations P(n,r) • What if we only want permutations of r elements out of the n objects? • E.g., If there are 20 people here, and I have 5 different prizes to give away to people in this class, how many possible winning situations are there? • Product Rule: 20^5 if you can win repeatedly • Product Rule: 20*19*18*17*16 if you can win at most one prize. This is P(20,5), the number of permutations of 5 objects out of 20.

  45. Permutations P(n,r) Note the underline below the r. This notation is due to Knuth and is probably the most standard notation for this, but nothing is really standard.

  46. Permutations P(n,r) You can understand this by canceling out terms. But there’s an intuitive way to understand this, too.

  47. P(n,r)=n!/(n-r)! Intuition • How many ways to line up 20 people? • If I only give prizes to first 5, the order of remaining 15 doesn’t matter. • How many ways can I order 15 people?

  48. P(n,r)=n!/(n-r)! Intuition • How many ways to line up 20 people? • If I only give prizes to first 5, the order of remaining 15 doesn’t matter. • How many ways can I order 15 people? • Therefore, the 20! is counting each winning possibility of the top 5 an extra 15! times. • So, the answer is 20!/15!

  49. P(n,r)=n!/(n-r)! Intuition • How many ways to line up n people? • If I only give prizes to first r, the order of remaining (n-r) doesn’t matter. • How many ways can I order (n-r) people? • Therefore, the n! is counting each winning possibility of the top r and extra (n-r)! times. • So, the answer is n!/(n-r)!

  50. Alan’s “Division Rule” • If the things you are trying to count are really identical-sized groupings of things from a larger set, you can divide the size of the larger set by the size of the groupings. • E.g., if I want to know how many cars drove over a sensor strip, I can divide the number of sensor inputs by 2. Why? Steve also uses this but didn’t think to name it after himself. Drat!It’s often handy to think about this type of “overcounting”.

More Related