37.7k views
5 votes
Given a constant function C : {0, .., N} tot → B, an up edge is an index i ∈ {0, ..N} such that ¬C(i)∧C(i+ 1).To ensure an up edge exists, we will assume ¬C(0) and C(N). Consider the specification f = ⟨B⟩ where B is ¬C(i ′ ) ∧ C(i ′ + 1)

Derive a solution using the binary search technique and the method of invariants. Make sure you use plenty of English prose as well as formal derivation to explain your solution. Here is a suggested outline: Propose an invariant I. Find a command that refines m = ⟨I′ ⟩. Propose a loop guard A so that I ∧ ¬A ⇒ Be is valid. (Show it is.). Find a loop body that implements h = ⟨A ∧ I ⇒ I′ ⟩. (Show that it does.)

User Ughzan
by
7.6k points

1 Answer

5 votes

Final answer:

Using the invariant that an up edge exists within a subarray, we apply binary search to locate an up edge within a function that transitions from false to true. The loop guard ensures our range for searching, and the correctness is demonstrated by refining the mid-point selections and maintaining the invariant.

Step-by-step explanation:

The question presented revolves around the use of binary search to find an 'up edge' within a constant function defined on a discrete domain. We assume the specifics of the function C such that C(0) is false and C(N) is true, which guarantees the existence of at least one up edge, due to the transition from false to true. We proceed step by step to construct a solution using invariants.

Proposing an Invariant I

We denote the invariant I as 'there exists an up edge within the subarray {i, ..., N}'. Formally, if I(k) applies, then the 'up edge' exists for some index j, k ≤ j < N.

Refining the Selection m = ⟦I'⟧

To refine the selection and maintain the invariant, we choose the mid-point of the current interval. If C(m) is false, we consider the interval {m + 1, ..., N}; if true, we consider {i, ..., m}.

Proposing a Loop Guard A

Our loop guard A will be 'i < N' which ensures we have a range to search within. We make sure that I & !(A) implies B is valid; when the loop concludes, we know our post-condition B, which signifies the 'up edge', is found.

Implementing the Loop Body h = ⟦A & I ⇒ I'⟧

Our loop body will evaluate C at the mid-point 'm'. As described earlier, depending on the result of the evaluation, we adjust our search interval either to the left or the right to maintain the invariant I and ensure that we are constraining the search to a smaller interval where an up edge must exist. This process effectively implements the binary search algorithm within our function's context.

User Jpeskin
by
8.6k points