Sonoma State University
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

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.

Assignment on Graph Basics and Singly-nested Loop Invariants

Work for this assignment falls into two groups:

Graph Basics

Adjacency Matrices and Lists

  1. 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
    1. Draw a picture of the graph represented by this adjacency matrix S, including the weights.
    2. Can this graph be interpreted as being directed? Explain.
    3. Can this graph be interpreted as being undirected? Explain.
    4. List the vertices in depth-first order beginning with vertex A. When you have a choice among vertices, pick them in alphabetical order. (For example, if at some point during the execution of the algorithm, both vertices B and D needed to be enqueued or pushed onto a queue or stack, respectively, then B would be enqueued/pushed before D.)
    5. List the vertices in breadth-first order beginning with vertex A. As in Part 1d, when you have a choice among vertices, pick them in alphabetical order.

    Notes:

A Graph Specified by Sets

  1. Consider the following graph, specified in terms of sets of vertices and directed edges.


    1. Write the adjacency matrix for this graph, based upon the alphabetical ordering of the vertices given.
    2. Draw the adjacency list representation for this graph.
    3. Can this graph be interpreted as being directed? Explain.
    4. Can this graph be interpreted as being undirected? Explain.
    5. List the vertices in depth-first order beginning with vertex E. As in Problem 1, when you have a choice among vertices, pick them in alphabetical order.
    6. List the vertices in breadth-first order beginning with vertex A. As in Problem 1, when you have a choice among vertices, pick them in alphabetical order.

    Notes:

Analytical/Structural Results

  1. Suppose a connected graph has v vertices and e edges. What is the complexity of a breadth-first search?

    1. Assume the queue is implemented with an array, and the graph by an adjacency matrix. Justify your answer.
    2. Assume the queue is implemented with a linked list, and the graph with adjacency lists. Justify your answer.

Invariants for Singly-nested Loops

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!

Binary Search Algorithms

  1. 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.

    an alternative loop invariant
                                        for a binary search

    To clarify both the processing and the desired results,

    To clarify the required return value, consider the following array of 13 elements:

    a sorted array

The Second Smallest Element in an Array

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.

  1. 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

    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
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2025 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.