Recurrence relations

Divide-and-Conquer

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 7n⋅log(n) + #leaves⋅1;
#leaves is 2height = 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):

(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⋅nd,

(Rosen 6.3 Th'm 2) (What a stupid, non-descriptive name.)

Exponential Growth

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
  fib(n) ≡ { 1                   if n = 1
           { 0                   if n = 0
[Consider: different initial conditions; running the process backwards.]

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).]
    We can compute the solutions to this, α = … .
  2. Sure enough, can check back that these answers satisfy the recurrence, but what about the initial conditions? Hmm, doesn't go.
  3. Realize that sol'ns to the recurrence relation (w/o the initial conditions) are closed under (a) sums, (b) constant-factors. If α1n and α2n are sol'ns, then so are: k⋅α1n + j⋅α2n, for any j,k ∈ ℜ. (That is, ``linear combo of two sol'ns is also a sol'n'', which math people go so far as to say ``the set of solutions form a vector space''.)
  4. Hmm, these j,k we can solve for. (Do so.)
  5. Are we done? Are there more solutions that aren't of the form αn? At this point, we ask the mathematicians, and trust us when they give us the answer we want: yes, this is all solutions.

This technique generalizes:

  1. Guess an exponential, get a polynomial of degree n,
  2. Solve roots α1, …, αn.
  3. A linear combination of those alpha's makes up all the possible answers.
  4. Caveat: if αi occurs with multiplicity m, then you also include include αin, n⋅αin, n2⋅αin, …, nm⋅αin.

Will not cover: the nonhomogenous version, where the recurrence relation includes a non-recursive term.
In general, this involves a lot of voodoo to find one solution.

However: 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.

Example: the game Lights Out. Consider all moves that take an empty board back to an empty board; this is a homogeneous solution; there might be several such solutions. Now consider a game where there are some lights on; if you can find one way to turn them off, then all-possible-solutions must be your one particular-solution, joined with any single homogenous solution. (Or put differently, there aren't any other solutions which aren't just your original solution plus a homogenous solution.)

(Note that we haven't talked about how to find one solution. For Lights Out: Subtle hint: systems of boolean equations…)

When you cover ODE's in engineering-math classes, you'll use the exact same approaches as here, except that instead of f(n-k) you use the kth derivative f(k)(n) — it's just the analog analog.

Grammars

Examples:
  1. We can think of Sentences with Articles, Nouns, Verbs, Prepositions:
    S → ANV.
    S → ANVPAN.
    N → JN
    A → a
    A → the
    P → to
    P → over
    N → baby
    N → owl
    N → beer
    N → ufo
    V → eats
    V → drinks
    V → burps
    What are some sentences this grammar generates? Think of the set of sentences this grammar can generate; is the set a subset of English sentences?
  2. We might have a grammar for (numeric) property lists:
    S→S,A:B
    A→(any string)
    B→(any number)
  3. Or a grammar for Java
How would we represent a Grammar in a program?
class Grammar {
  Set<String> vocab;
  Set<String> terminals;
  char startState;
  Set<String×String> productions;
  }
Rosen says the same thing in a different way: "a grammar is a four-tuple ⟨V,T,S,P⟩ where T⊆V, S∈V-T, and P∈(V*→V*).

Finite State Machines (FSMs)

A technique, particularly suited to controllers. (Originally hardware, but also good in software.)

A small Example

A door-pad controller (keypresses "a","b","c"), which opens on the password ``bcaa'', and lights up the "denied" lamp if not.

We use an internal clock "tick" input, to get us out of states open-door and denied, back to initial state.

Repreresentations of FSMs

Example: FSM for 3-disc CD controller:

(This next might be redundant for lecture, if students already got involved w/ the previous example and added lots of features/fixes.)

Attempt 1:
   
   state  |   S/S    EOD   SOD   
----------+--------------------
 idle     |  play    ??     ??
 play     |  idle   rotate  ??
 rotate   |  rotate  ??    play

The state "play" is wired to the disc-spin motor, and the state "rotate" is wired to the carousel-rotate motor.

Note that the "??" are inputs that shouldn't be generated if all goes well. (General policy: "fail-safe": on failure, do something safe. In this case, "idle" seems reasonable, to keep black smoke from billowing out the back of the CD player, or to keep, the laser from blinding kittens.)

Some observations on this FSM:

Let's remedy this last problem: Attempt 2:
   state  |   S/S     EOD      SOD   
----------+------------------------
 idle     |  play1     ??       ??
 play1    |  idle     rotate2   ??
 rotate2  |  rotate2   ??      play2
 play2    |  idle     rotate3   ??
 rotate3  |  rotate3   ??      play3
 play3    |  idle      idle     ??

"play1"…"play3" are all just hooked up to the disc-spin motor, and "rotate2", is hooked up to the carousel-rotate motor.

Okay this attempt is better, but still has problems.
  • What happens after listening to all discs, and then coming along and pressing Start/Stop? [Which disc is cued up? don't be mis-led by the state name!]
  • When we stop, (wait a day), and then start, which disc gets resumed? This might be the desired behavior, or might not be.

  • On your own:

    1. Fix the first problem above.
    2. Now, add a toggle button rpt/rpt-all/seq/shuffle. Note that this requires more state, to remember which we're in.
    3. In particular, nearly an entire copy of our original machine. This is called the "cross-product construction". (At a high-level, this would of course be an awful program. In general you might consider adding variables in addition to state, but only judiciously — else we could do everything with a big program and one state. Alternately, we could consider the "rpt" toggle as an input, which we ignore most of the time, and make transitions like "EOD & rpt". How to avoid ANDing inputs? Add an extra state (effectively computing AND)!

    Another exercise

    We'll model the progression of a volleyball game with a finite state machine. Our first attempt will be overly simplistic, but we'll add some features to arrive at a slightly less simplistic version.

    1. Let the set of states be
      S = { team1-to-serve, team1-to-return, team2-to-serve, team2-to-return }.
      (You can abbreviate these Srv1,Ret1,Srv2,Ret2.) Let the inputs be the set I = {Succeed,Fail} (you can abbreivate S,F), indicating a ref's call that the current state's action just succeeded or failed, resp. Thus we think of our model proceeding along the lines of:
      state:  team2-to-serve
      input:    Succeed         [indicates team 2 served successfully.]
      state:  team1-to-return5
      input:    Fail            [oops, team 1 failed to return the ball.]
      state:  team-2-to-serve
      ...
      
      Specify the 4-state FSM best modeling a volleyball game. Express your answer in table format.
    2. Give a new FSM which allows for the possibility of a "re-serve": If a team is to serve but fails, they get one further attempt6 before losing possession. You will need to add a little more state.
      Express your answer as a picture of a directed graph with labeled edges (as in lecture, similar to Rosen 11.2). Neatness improves clarity!
    3. Scoring in volleyball is interesting: only the team which served may score a point7. Our FSM won't actually keep track of score, but it will indicate when one team or the other scores a point, via two new states "increment-team1-score" and "increment-team2-score". (After an increment, the referee will always give the input S.)
      This requires quite a few more states. Don't write down the answer for this part -- instead, just include your answer as part of the next subproblem.
    4. Currently, our FSM models a volleyball game that never ends. We'll modify this by letting the referee keep score8, and (after a team's score is incremented) they'll give the input "S" to continue the game, and "F" to indicate that that team just won.
      Express your answer as a graph, as before.

    Brief comment about implementing in a program; OO: clearly states are objects; the transitions (edges) might also be objects.


    4 Well, you might be writing code for MegaProVolleyballPlus, and the referee might actually be the physics simulator, which is calculating the outcome based on your or another module's output. (back)

    5 We'll consider a return as a single event, and not model how it might be made up of up to three individual hits by one team. Clearly, you could certainly model the up-to-three hits by refining your FSM, though it might need inputs (the ref) distinguishing between a hit which wasn't a return but wasn't drop-the-ball either. (back)

    6 This was how we played in my (dreaded) junior high PE; however more competitive volleyball might only allow re-serves when the serve hit the net; I'm not sure.

       Regardless, Once a re-serve resolves, the botched serve is forgotten; thus there can be a re-serve every single volley. (back)

    7 Well in real volleyball, there are apparently "rally points" in overtime, a mode where every play results in somebody getting a point. You can see that we could also model this, but it would require a referee input telling us to switch into rally mode, and would also mean a bunch more states. (back)

    8 The direct way to model the points would be to augment our definition of FSM to allow a couple of integer variables, let certain states modify them, and allow transitions to depend on them.

       It's more cumbersome (but possible) to do this even without augmenting the FSM with variables. For every possible score (how many possible scores are there?), we could make a copy of the simple FSM, and then tweak the scoring transitions. (back)