CS 415, Section 001 | Sonoma State University | Spring, 2023 |
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although much of this course is well developed, some details can be
expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.
Work for this assignment falls into two groups:
Consider the following adjacency matrix S for a weighted graph. (Note: 0 means there is no edge.)
A | B | C | D | E | F | G | H | |
A | 0 | 3 | 0 | 0 | 4 | 7 | 0 | 0 |
B | 0 | 0 | 5 | 0 | 0 | 0 | 0 | 0 |
C | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
D | 2 | 0 | 0 | 0 | 8 | 0 | 0 | 0 |
E | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 5 |
F | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 0 |
G | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 |
H | 0 | 8 | 0 | 5 | 0 | 0 | 0 | 0 |
Consider the following graph, specified in terms of sets of vertices and directed edges.
Suppose a connected graph has v vertices and e edges. What is the complexity of a breadth-first search?
In this section, you are asked to write three programs, all involving loop invariants for singly-nested loops. Of course, all programs must follow the C/C++ Style Guide!
The Reading on an Introduction to Loop Invariants discusses two implementations of a binary search, based on different loop invariants, and program binary-searches.c provides the full code for each of these invariants, as well as a framework for testing these functions.
The reading identifies two variations for the desired result of a binary search:
Following the second variation as the desired result,
expand binary-searches.c to include a new
function search3
that implements and tests a binary
search that uses the following loop invariant.
To clarify both the processing and the desired results,
left
is the largest array index for which it is
known that a[left] < item
(provided some
elements are known to be less than item
, or equivalently
left
is the first index for
which a[left+1]
is unprocessed (provided some
elements have not yet been examined).
right
is the largest array index for
which a[right]
is unprocessed (provided some
elements have not yet been examined), or equivalently
right
is the first index .for which it is
known that a[right+1] > item
(provided some
elements are known to be larger than item
To clarify the required return value, consider the following array of 13 elements:
The Dutch National Flag Problem was first proposed by W.H.J. Feijen and made famous by Edsger W. Dijkstra. The following formulation relates the problem to arrays in C.
Enumerations in C are described in Kernighan and Ritchie, Section 2.3, page 39. For example, an enumeration with three colors could be declared as:
enum color { red, white, blue };
and we may consider an array of colors:
#define size 50 /* number of elements in an array */ color colors [size];
When we begin, we do not know the number of elements of each color, and we are not even assured that each color is actually present.
The Dutch National Flag Problem seeks to sort this array, so that red's come first, then white's, and then blue's. Movement of array elements may be accomplished only by swapping two items.
Although one approach to this problem involves simple sorting (just consider red < white < blue), the problem can be solved in a single pass of the data. The idea is to identify an array diagram that describes sections of colors as loop invariants. Writing the code then is reasonably straightforward; we just have to maintain the invariant!
For this problem, at least four different pictorial loop invariants initially come to mind:
In each case, one must introduce variables to keep track of the edge of the red, white, and blue sections. Initially, these sections contain no elements, and the entire array is unprocessed. Then as processing proceeds, the program looks at successive unprocessed elements and puts them in their correct locations — maintaining the loop invariant.
Write a complete program to solve this problem, using either Invariant A or Invariant D.
For your chosen invariant, initialize your variables, so that the pictorial loop invariants are satisfied at the start of processing.
Complete the loop processing, maintaining the identified loop invariant.
Once code segment for the Dutch National Flag Problem is written, place it within a complete program, so you can test that your code works properly.
In submitting your work for this problem, include both the complete program and sample test runs.
Repeat Exercise 5, choosing either Invariant B or Invariant C.
created August 7, 2022 revised August 9, 2022 revised December 29, 2022-January 2, 2023 minor editing February 15 and 22, 2023 minor editing Summer, 2023 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2022
by Henry M. Walker.
|