Sonoma State University | ||
Algorithm Analysis
|
||
Instructor: Henry M. Walker
Lecturer, Sonoma State University |
Although CS 415 has been well developed for several years, last year the CS faculty made a significant, long-term curricular change regarding SSU's Upper Division GE Area B Requirement.
Historically, CS Majors could satisfy this requirement by taking CS 454, Theory of Computation, and CS 454 will continue in this role for the next several semesters.
At some time in the future (but not Spring 2025), CS 415, Algorithm Analysis, will allow students to satisfy SSU's Upper Division GE Area B Requirement.
During an anticipated time of transition:
For future semesters, students should check with the CS faculty regarding which course(s) (CS 415 and/or CS 454) will satisfy SSU's Upper Division GE Area B Requirement.
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 | I | |
A | 0 | 3 | 0 | 2 | 0 | 0 | 0 | 0 | 0 |
B | 3 | 0 | 5 | 0 | 0 | 0 | 0 | 0 | 0 |
C | 0 | 5 | 0 | 0 | 0 | 10 | 0 | 0 | 0 |
D | 2 | 0 | 0 | 0 | 8 | 0 | 4 | 0 | 0 |
E | 0 | 0 | 0 | 8 | 0 | 2 | 0 | 5 | 0 |
F | 0 | 0 | 10 | 0 | 2 | 0 | 0 | 8 | 4 |
G | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 7 | 0 |
H | 0 | 0 | 0 | 0 | 5 | 8 | 7 | 0 | 1 |
I | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 1 | 0 |
Notes:
Consider the following graph, specified in terms of sets of vertices and directed edges.
Notes:
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 smallest array index for
which a[right] > item
(provided some elements
are known to be larger than item
), or equivalently
right
is the first index for which it is known
that a[right-1]
is unprocessed (provided some
elements are known to be larger than item
)
To clarify the required return value, consider the following array of 13 elements:
Consider the following problem. Given an array a with n distinct integer numbers, find the second largest number of the array. (Note that no assumptions are made regarding the nature of the integers—they could be small or large, negative, positive, zero, or a combination.)
This problem can be solved in many ways. For example,
This problem follows the third approach, utilizing a specified loop invariant.
Write a program to find the second largest element in an array a of size n, utilizing a single pass through the array.
After reading the array size n and the array elements, the program should have a single loop that starts at some value ?? and proceeds through the array:
for (i = ??; i < n-1; i++)
with the following loop invariant: for each i
largest
holds the value of the largest
element in a[0..i-1]
second
holds the value of the
second largest elementin a[0..i-1]
At the end of processing, the program should print the second largest elementin the array.
Notes:
created August 7, 2022 revised August 9, 2022 revised December 29, 2022-January 2, 2023 minor editing February 15 and 22, 2023 revised December 5, 2024 |
|
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
Copyright © 2011-2025
by Henry M. Walker.
|