Information --- Staff --- Homework --- Reading/Lecture note --- Newsgroup --- Feedback

Finish up counting, quickly:

Permutations with repetition (identical elements):
The Contemporary Art Museum (CAM, corner of bissonet & main, free, worthwhile)
is pushing their new CAM diet, where you get 3 mangos(M), 2 apples(A),
and a chocolate bar (C); each day you can have one one of these for dessert.
Of course, each artist wants their own individual diet plan;
how many possible diets are there?
[Note: this is the same as the number of strings which
can be made out of the letters 'McMama']

Sol'n: Imagine all letters are distinguished 
(not just two "A"s, but "A1" and "A2");
there are clearly 6! possibilities.
But for every entry we can match it with the swapped-A version:
each pair is the same, but our 6! is countint it twice.
Divide by 2, to compensate for over-counting the A's.
Even after this, we see that for every word, we can re-arrage the three M's
in any of 3! = 6 says; thus every entry is part of a group of 6 identical
strings, and we count all 6 instead of just one.
Thus our final answer is
   6!/2!3! = 60 artistically meaningful diets.



Summary of various options -- not reviewed in lecture:

How many ways to put
5 students into 3 classes, where:
- each student and class distinguishable
  [ie, Amy in Comp210 and Ben in Huma101 is different from vice-versa.]
- classes distinguished, students not
  [ie   Of 5 foreign-exchange students, how to divvy them
        up between 3 classes]
- students distinguished, classes not:
  [ie  how many ways to assign
       Amy, Ben, Cat, Deng, and Eustace
       into three classes;   AB/CD/E is the same as CD/AB/E.]
- neither distinguished
  [5 balls in 3 boxes, those boxes can move around
   so 4/1/0 is same as 4/0/1.]




Quickly, Inclusion-Exclusion:
Let A be the set of Freshmen, 
B the set of Jones students,
and C the set of econ majors.
What is the size of A ∪ B ∪ C?
Well, |A|+|B|+|C| is an over-count, since anybody in
multiple sets is being counted repeatedly.
   [Draw Venn Diagram]
In particular, the people in B∪C are counted twice,
so subtract them out; similar for A∪B and A∪C.
    |A|+|B|+|C|            (note: C(3,1) choices)
  - |A∪B| - |A∪C| - |B∪C|    (note: C(3,2) choices).
This now correctly counts people in just one set,
and correctly counts people in exactly two,
but the people in all three were added three times
were added three times then subtraced three times.
We must add them in once more:
Correct answer:
    |A|+|B|+|C|            (note: C(3,1) choices)
  - |A∩B| - |A∩C| - |B∩C|    (note: C(3,2) choices).
  + |A∩B∩C|


This generalizes to more than three:
To calculate the size of four sets, it's
      the size of all C(4,1) original sets,
minus the size of all C(4,2) 2-way intersections,
plus  the size of all C(4,3) 3-way intersections,
minus the size of all C(4,4) 4-way intersections.


A note, for probability:
  |A∪B∪C| ≤ |A|+|B|+|C|.
This can be a handy approximation esp. when each of A,B,C are all small
compared to the entire domain, and/or when there is little-to-no overlap.



======== Recurrence relations ========

A divide-and-conquer algorithm takes its input, makes several
smaller-problems,
solves those recursively, and then combines the
sub-results into the result for the full original problem.

The canonical example: Mergesort.

Another example:
  Given n points in the plane, find the minimum distance between points.
(See Rosen Section 6.3, Example 12.  Comp360 deals with problems like this.)

[To think about:
Compare to: Separate the points into thirds by x-coord;
find the min distance in left-two-thirds, and in right-two-thirds.
Does this work?  What is base case?  How to worry about points
w/ same x-coord?]

What is the (worst-case) running-time of this algorithm, for input size n?
  T(n) = ??
It's not clear what a closed-form answer is.
But we can easily find a recursive formulation:
  T(n) = 2T(n/2) + 7n;   T(2)=1.

  [Draw out tree, assuming n = 16, say.
   In blue, write the amount of work done at that node;
   the total time for a node is that blue plus all the blue underneath.
   Add up all the blue: observe that by rows-of-tree, it's 7n on each row.
   There are log(n) rows, if n a perfect power of 2.
   Thus, total 7*n*log(n) + #leaves*1;
   #leaves is 2^height = 2^(lg(n)) = n.
   Total work: O(n log n).]

Exercise: show that this big-oh still holds for n not being a perfect
power of 2.  Not too hard, if we assume that more input never takes *less*
time.  Then just bound T(n) with T(nearest-power-of-2-bigger-than-n)
(which is less than 2n).

Now, consider other divide-and-conquer algorithms (ignoring floor/ceiling):
 - mergesort: Tm(n) = 2Tm(n/2)+Θ(n).
   Answer from comp210: Tm(n) = O(n log n).
 - binary search: Tbs(n) = Tbs(n/2)+1.
   Answer from comp210: Tm(n) = O(log n).
(Remember, these are worst-case running times.)

"Master th'm", for recurrence relations arising from divide-and-conquer:
     For f(n)=a*f(n/b)+c*n^d,
     if a < b^d:   f(n) is O(n^d)
     if a = b^d:   f(n) is O(n^d log n)
     if a > b^d:   f(n) is O(nlogb(a))
 (Rosen 6.3 Th'm 2) (Stupid, non-descriptive name.)




  - Does this cover all types of recurrences?  No; consider:
 - breaking an n-digit password.
   Brute force:
      T(n)= 10 T(n-1)
    Clearly the closed-form answer is 10n.

 What of Fibonacci?
                      { fib(n-1)+fib(n-2)   if n ≥ 2
   - define: fib(n) = { 1                   if n = 1
                      { 0                   if n = 0
     [Consider: different initial conditions; running the process backwards.]

   - computing answer by def'n:
     Hmm, lots of terms, maybe even 2n?
   - can memoize / work forward: O(n).
   - Would be nice if we had a closed form relation
     for the answer: O(1)  (???).
        αn/sqrt(5) + alphaBarn/sqrt(5),
     where α = (1+sqrt(5))/2, alphaBar = (1-sqrt(5))/2.
     
     [Got to here 2004.apr.13]

     But *why* is this the answer / closed form?
     What if we change the initial coniditions?

     Approach to recurrences of the form f(n)= b*f(n-1) + c*f(n-2):
        (1)  *Guess* a sol'n of the form f(n)=αn;  what is α?
             Well, we get α^2 - b*α - c = 0,
             [The "characteristic polynomial"; would be cubic if we had involved f(n-3).]
             With possible sol'ns α = ... .
        (1b) Sure enough, can check back that these satisfy the recurrence,
             but what about the initial conditions?
             Hmm, doesn't go.
        (2)  Realize that sol'ns are closed under (a) sums, (b) constant-factors.
             (That is, linear combo of two sol'ns is also a sol'n.)
             if α1n and α2n are sol'ns,
             then so are: k*α1n +  j*α2n.
        (2b) Hmm, *these* we can get
        (3)  Trust the mathemeticians: these are *all* the sol'ns.

     This generalizes:
     Guess an exponential, get a polynomial of degree n,
     solve roots α1, ..., αn.
     A linear combination of those alpha's makes up all the possible answers.
     Caveat: if αi occurs with multiplicity m, then you also include
       include αin, n*αin, n^2*αin, ..., n^m*αin.

     Will not cover: the nonhomogenous version, where the recurrence relation
        includes a non-recursive term.
        Punchline: if you can find one sol'n h(n) to the recurrence,
         then every sol'n must be of the form g(n)+h(n), where g(n) is a
         sol'n to the corresponding homogenous form.

      When you cover PDE's, you'll use the exact same approaches,
      except that instead of f(n-k) you use f^(k)(n) -- it's just the analog analog.






Information --- Staff --- Homework --- Reading/Lecture note --- Newsgroup --- Feedback

Comp280 Home Please notify us of any broken links, etc. Last modified 2004.Apr.15.