Algorithms and OOD (CSC 207 2014S) : Labs

Loop Invariants


Summary: We ground our understanding of loop invariants by writing some of our own and using them to develop algorithms.

Preparation

Review the reading to remind yourself of the structure of loop invariants, particularly loop invariants over arrays.

Exercises

Exercise 1: The Dutch National Flag

This exercise only asks you to analyze a problem. You need not write code, although you should write pseudocode.

The Dutch National Flag problem is a problem that traditionally benefits from the close analysis you get from using loop invariants. Here's the problem: You have an array of values in no particular order. Each value can be classifed as “red”, “white”, or “blue”. Your goal is to rearrange the array so that the blue elements appear first, then the white elements, and then the red elements. (Believe it or not, but we'll find some interesting uses of this algorithm in the near future.)

How do we know what “color” a value is? You can assume that we have three functions, isBlue, isWhite and isRed and that exactly one of these functions holds for each value.

As you should recall from the reading, our first step is to design a loop invariant. Here's one that I've found useful.

+---------+---------+---------+---------+
|   red   |  white  | unknown |   blue  |
+---------+---------+---------+---------+
|         |         |         |         |
0         r         i         b         length

As you might guess, our goal is to shrink the unknown portion of the array until there's nothing left.

a. How might we represent the pictoral loop invariant more formally?

b. How should we initialize r, i, and b to ensure that the invariant holds at the beginning of the program?

c. How do we represent the rule “continue as long as there are are unknown elements”?

d. How can we update the array so that we simultaneously decrease the unknown area and maintain the loop invariant? (Yes, you will probably need a conditional to do so.)

Hint: You should look at one more element in the unknown section and decide what to do with it.

e. Put everything together into psuedocode for a solution to the Dutch National Flag algorithm.

Exercise 2: Binary Search, Revisited

The version of binary search in the reading has two tests for the middle value: we first test if it's less than the midpoint and then test if it's equal to the midpoint. Some programmers find that approach inelegant, or perhaps inefficient.

Rewrite binary search so that it only does one test. Your revised binary search will likely continue until there is only one element left in the array. At that point, you can check whether it's the desired element and either return the index or throw an exception.

Exercise 3: Exponentiation, Revisited

The reading provides an implementation of exponentiation that has a running time of O(n). Earlier this semester, you've seen a recursive implementation of exponentiation that is O(log2(n)). Using appropriate invariants, write an iterative O(log2(n)) exponentiation algorithm.

Hint: You may find it useful to keep track of two intermediate values, one of which only takes on values of x2k and one of which holds the product of any other x's you need in the result.

Copyright (c) 2013-14 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.