Algorithms and OOD (CSC 207 2014F) : EBoards

CSC207.01 2014F, Class 24: Reasoning About Loops with Loop Invariants


Overview

Preliminaries

Admin

Upcoming Work

Cool Things Coming to Campus

Academic

Peer Support

Miscellaneous

Questions

A Problem: Incorrect Algorithms

A (Partial) Solution: Consider State

Loop Invariants

Lab (Collaborative)

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

How might we represent this more formally (in Math/Code)

How might we arrange things so that this holds when our algorithm starts? (What values of r, i, and b guarantee those three characteristics?)

Given the following state, what can we do to shrink the size of unknown?

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

If the value at position i is blue ...

+---------+---------+-----------+---------+
|   red   |  white  |b unknown x|   blue  |
+---------+---------+-----------+---------+
|         |         |           |         |

Swap it to right before the blue stuff

+---------+---------+-----------+---------+
|   red   |  white  |x unknown b|   blue  |
+---------+---------+-----------+---------+
|         |         |           |         |

Move the blue boundary

+---------+---------+----------+----------+
|   red   |  white  |x unknown |b   blue  |
+---------+---------+----------+----------+
|         |         |          |          |

If the element is white, move the white boundary up

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

+---------+----------+--------+---------+
|   red   |  white  w|unknown |   blue  |
+---------+----------+--------+---------+
|         |          |        |         |

If the element is red, things are a bit more complicated. You'll need to swap the element to the end of the reds and move the red boundary.

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

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

If our goal is to look at each element once, then it's concerning that we'll have to look at the x we passed by. But that x was in the white section, so we know it's white and can also increment the white boundary.

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

Lab (Individual)

We didn't get here.