SE 504
Spring 2009
HW # 5: More Selection, plus an Invariant
Due: March 12

1. Prove

   |[ var i,m : int;
      con b : array of int;
      {P ∧ i<#b}
      if b.i ≥ b.m  →  m := i
      [] b.i ≤ b.m  →  skip
      fi;
      i := i+1
      {P ∧ i≤#b}
   ]| 
where P : 0≤m<i ∧ (∀j | 0≤j<i : b.j ≤ b.m)

Note that the program above is not a selection command; rather, it is a catenation S;T in which S is a selection and T is an assignment.


2. Prove

   |[ var p,q : int;
      con b : array of int;
      {P &and 0≤p<q<#b}
      if b.p ≤ b.q  →  p := p+1
      [] b.p ≥ b.q  →  q := q-1
      fi
      {P ∧ 0≤p≤q<#b}
   ]| 
where P: b.p max b.q = (MAX i | 0≤i≤p ∨ q≤i<#b : b.i)

Note that predicate P corresponds to the statement that either b.p or b.q is the maximum among the values occurring in either b[0..p] or b[q..#b). (It does not say that b.p (respectively, b.q) is the maximum among the elements in b[0..p] (respectively, b[q..#b)).)

In doing the proof, you may find useful these theorems with respect to the max operator:

  1. (x ≥ y) = (x = x max y) (This serves as a definition of max.)
  2. (x ≤ y) = (y = x max y) (Equivalent to line above.)
  3. (x = x max y) ∨ (y = x max y)
  4. x = x max x

3. Consider a two-player game that begins with a pile of matches (or any other kind of objects) on a table. The players take alternating turns; on each turn, a player must remove at least one match, but no more than three. The player who removes the last match is the winner. We refer to the two players as A and B, the former being the one who moves first.

Let M > 0 be the number of matches on the table initially. Then, depending upon the value of M, either A or B has a winning strategy, meaning a method/plan/procedure that, if faithfully executed, necessarily leads to victory.

For each possible value of M, indicate which player has the winning strategy, and describe that strategy. Use the loop invariant concept to help you identify the winning strategy.