Book contents
- Frontmatter
- Contents
- Preface
- Introduction
- Part One Iterative Algorithms and Loop Invariants
- 1 Iterative Algorithms: Measures of Progress and Loop Invariants
- 2 Examples Using More-of-the-Input Loop Invariants
- 3 Abstract Data Types
- 4 Narrowing the Search Space: Binary Search
- 5 Iterative Sorting Algorithms
- 6 Euclid's GCD algorithm
- 7 The Loop Invariant for Lower Bounds
- Part Two Recursion
- Part Three Optimization Problems
- Part Four Appendix
- Part five Exercise Solutions
- Index
2 - Examples Using More-of-the-Input Loop Invariants
Published online by Cambridge University Press: 05 June 2012
- Frontmatter
- Contents
- Preface
- Introduction
- Part One Iterative Algorithms and Loop Invariants
- 1 Iterative Algorithms: Measures of Progress and Loop Invariants
- 2 Examples Using More-of-the-Input Loop Invariants
- 3 Abstract Data Types
- 4 Narrowing the Search Space: Binary Search
- 5 Iterative Sorting Algorithms
- 6 Euclid's GCD algorithm
- 7 The Loop Invariant for Lower Bounds
- Part Two Recursion
- Part Three Optimization Problems
- Part Four Appendix
- Part five Exercise Solutions
- Index
Summary
We are now ready to look at more examples of iterative algorithms. For each example, look for the key steps of the loop invariant paradigm. What is the loop invariant? How is it obtained and maintained? What is the measure of progress? How is the correct final answer ensured?
In this chapter, we will encounter some of those algorithms that use the more-of-the-input type of loop invariant. The algorithm reads the n objects making up the input one at a time. After reading the first i of them, the algorithm temporarily pretends that this prefix of the input is in fact the entire input. The loop invariant is “I currently have a solution for the input consisting solely of these first i objects (and maybe some additional information).” In Section 2.3, we also encounter some algorithms that use the more-of-the-output type of loop invariant.
Coloring the Plane
See Figure 2.1.
1) Specifications: An input instance consists of a set of n (infinitely long) lines. These lines form a subdivision of the plane, that is, they partition the plane into a finite number of regions (some of them unbounded). The output consists of a coloring of each region with either black or white so that any two regions with a common boundary have different colors. An algorithm for this problem proves the theorem that such a coloring exists for any such subdivision of the plane.
2) Basic Steps: When an instance consists of a set of objects, a common technique is to consider them one at a time, incrementally solving the problem for those objects considered so far.
[…]
- Type
- Chapter
- Information
- How to Think About Algorithms , pp. 29 - 42Publisher: Cambridge University PressPrint publication year: 2008