Lecture 16

Transcription

Lecture 16
Counting Problems; and
Recursion!
CSCI 2824, Fall 2012!
!
!
Assignments
• To read this week: Sections 5.5-5.6 (Ensley/Crawley
• Problem Set 3 has been sent out today.
• Challenge problem today !
!
!
!!
So, to recap…
•  Order matters, repetition allowed
(“password” problem)
•  Order matters, repetition not allowed
(“traveling salesman” problem)
•  Order doesn’t matter, repetition not allowed
(“committee” problem)
•  Order doesn’t matter, repetition allowed
(“mixed nuts” problem)
Practice Problems
•  Problem 1: How many potential Mastermind
codes are there (6 colors of pegs) with 2 or 3 pegs
the same color?
•  Problem 2: How many poker hands are there with
one pair?
•  Problem 3: Suppose you have 10 disks all of the
same size (indistinguishable). How many ways are
there of placing all these disks on the three Tower
of Hanoi pegs?
Problem 1
No. of 2-of-a-kind No. of 3-of-a-kind
(C(4,2) * 6 * 5 * 4) + (C(4,3) * 6 * 5)
Problem 1 (alternative)
All the ways of getting 4 of a kind: 6
All the ways of getting 4 distinct:
6 * 5 * 4 * 3
All the ways of getting 2 of a kind in the form 2+2
(as opposed to 3+1):
(1/2) * C(4,2) * 6 * 5
Now subtract the sum of all these from 64 Problem 2
(13 * C(4, 2)) * (C(12,3) * 4 * 4 * 4)
Problem 3
This is a “mixed nuts” problem (think of the
pegs as meaning “almonds”, “Brazils”,
“cashews”, and we have to fill a bag of 10
items):
C(10+2, 2) = C(12, 2) = 66
Another type of problem
How many numbers, from 1 to 100, are not
multiples of 2, 3, or 5?
We could start with a simpler
problem (as Polya suggests)
How many numbers between 1 and 100 are
neither multiples of 2 or 3? (2 or 5? 3 or 5?)
What we’re after here is a set
union
•  The union of all multiples of 2, multiples of
3, and multiples of 5 chosen from 1 through
100.
•  The way to do this is to (a) take the sum of
the sizes of all sets, (b) subtract the sum of
all 2-set intersections, then (c) add back the
size of the 3-set intersection
Inclusion/Exclusion
The general idea here is that, to get the size of
the union of n sets, you need to calculate:
Add C(n,1) sets (that is, all the original sets)
Subtract C(n,2) 2-set intersections
Add C(n,3) 3-set intersections
Subtract C(n,4) 4-set intersections…
Add (-1)n-1 C(n,n) n-set intersections Suppose an item is in the
intersection of m sets. How many
times will it be added in?
C(m, 1) – C(m, 2) + C(m, 3) + …. (-1)m-1C(m,m)
Recall from our discussion of Pascal’s triangle:
0 = Σ0<=k<=m C(m,k) xk
So we’re adding in our item exactly 1 (that is, C(m,0) ) times, since C(m,0) is the only term
missing from that top line! A Practice Problem
How many poker hands have at least one card
of every suit?
Challenge Problem No. 3 (due
Thursday Nov. 1)
The “Secret Santa” problem:
Suppose we have 8 children in a classroom, and
we want to assign each of them a “Secret Santa”.
Naturally, we don’t want to assign any kid to be
his or her own Santa. How many distinct legal
arrangements are there? And now that you’ve
solved the problem for 8, write an expression that
solves the problem for n in general. As n gets
large, what is the probability that any Secret Santa
arrangement will be a legal one? RECURSION:
the basic definition
•  In a computer program, a procedure or function is
recursive if its definition involves a call to itself. •  There are slightly more broad definitions that still
make sense: for instance, a set of functions or
procedures that mutually call each other can be
said to be recursive as well. Or we might say of a
definition of some sort of object that the definition
itself has some element of recursion.
A 0-level cat is a cat wearing a hat.
A n-level cat is a cat tipping its
hat to reveal an n-1 level cat.
A well-known recursive
definition in mathematics
Factorial (n)
IF (n = 0) THEN 1
ELSE ( n * Factorial(n -1) )
Another recursive definition:
here, assume n1 and n2 are nonnegative integers
Add (n1 n2)
IF (= n2 0)
THEN n1
ELSE Add (Increment n1) (Decrement n2)
Our earlier recursive solution to the Tower of Hanoi puzzle:
To solve the puzzle with 1 disc, just move the disc directly.
To solve the n-disc puzzle with pegs labelled SOURCE, SPARE,
TARGET:
Solve the (n-1) disc puzzle with pegs relabelled SOURCE TARGET SPARE
Move the largest (bottom) disc from SOURCE to TARGET
Solve the (n-1) disc puzzle with pegs relabelled
SPARE SOURCE TARGET