CSC 301  Grinnell College  Fall, 2011 
Analysis of Algorithms  
Quick Links: September October November December 
The accompanying table indicates assignments and due dates for Computer Science 301 and are subject to the following notes.
Unless otherwise indicated, textbook references are to Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein, Introduction to Algorithms, Third Edition, Cambridge, Massachusetts: The MIT Press, 2009; ISBN 9780262003848.
Supplemental problems are stated later on this page.
Since collaboration is allowed on some assignments, but not on others, the following table clarifies the rules for collaboration for each assignment.
When turning in problems from the text, the solutions must be given in problem order. That is, if problem A has an exercise number lower than problem B, then the solution to problem A must be given before the solution to problem B.
Due Date  Collaboration  Chapters/Sections  Problems  Notes 

Fri., Sept. 2  34  
Wed., Sept. 7  34 revisions  
Fri., Sept. 9  Choose 3 of 4.21 to 4.36  
Wed., Sept. 14  5.12 (can assume the number of integers in [a, b] is a power of 2), 5.13, 5.21, one of (5.22, 5.23, 5.24)  
Mon., Sept. 19  6.57, 6.59, 7.45  
Wed., Sept. 21  8.41, 8.42, 82  
Fri., Sept. 23  Hour Test 1  
Wed., Sept. 28  supplemental problem 1  
Wed., Oct. 5  11.31, 11.41, two of (11.21 through 11.26)  
Mon., Oct. 10  Lab on Pictorial Loop Invariants  Reading on Pictorial Loop Invariants  
Wed., Oct. 12  supplemental problem 2  Solutions with identical PQ.java, PQImplementation.java (Also, PQItem.java identical for array and LinkedList versions)  
Wed., Oct. 26  supplemental problems 3, 4  
Wed., November 9  Take Home Test due  
Fri., November 18  2 of (22.14, 22.15, 22.16); 2 of (22.42, 22.34, 22.27)  
Wed., November 23  supplemental problem 5  
Mon, December 5  Take Home Test due  
Due Date  Collaboration  Chapters/Sections  Problems  Notes 
Comparison of Sorting Algorithms
In this lab, you are to compare the performance of five sorting algorithms, including:
In developing the comparison, you will need to code each algorithm in the same programming language (possible choices are Scheme, C, or Java), and you will need to run all algorithms on the same data sets of varying sizes. Here are some details.
Try to code each algorithm in a similar style, in order to minimize the impact of idiosyncrasies of coding.
Your tests should generate three data sets for files of size 10,000, 20,000, 40,000, 80,000, ... . One data set should be ordered in ascending order, one data set should contain random data, and one data set should be in descending order.
Each data set is to contain only positive integers, the numbers may have up to 8 decimal digits (a size is needed for the radix sort), and each data set is to be sorted in ascending order by each algorithm.
Your program should determine the time spent by the sorting algorithm separately for each data set. The timing should NOT include any set up before the sorting algorithm (e.g., not array initialization), and the timing should NOT include any clean up at the end (e.g., memory deallocation, printing). However, the timing should include any required initialization of variables required for the algorithm.
Since processor speeds, machine capabilities, and runtime environments will vary by language, the program should continue testing algorithms with data sets that progressively double in size until some algorithm requires over 10 seconds to finish on one data set.
The program should produce a table of times in the following format:
Data Set Times Algorithm Size Ascending Order Random Order Descending Order Insertion sort 10000 Merge sort 10000 Heap sort 10000 Quicksort 10000 Radix sort 10000 Insertion sort 20000 Merge sort 20000 Heap sort 20000 Quicksort 20000 Radix sort 20000 ...
Once you have the times collected for the various algorithms, plot the times versus the data set size using any graphing software you wish. (A spreadsheet might be used, but you also might use MatLab, Maple, or other package.) For each algorithm, the times for the three data sets should be connected (that is, one line would connect the times for the insertion sort with ascending data against sizes 10000, 20000, 40000, ... .)
If the graphing software allows, it would be helpful to have the graphs for all algorithms with ascending order combined on one plot, the graphs for all algorithms with random data on a second plot, and the graphs for all algorithms with descending data on a third plot. In addition, a graph of a line (f(n)=n) and a quadratic (f(n) = n^2) would be helpful (but not required).
Kway Merge with Priority Queue/Heap
Exercise 6.59 in Cormen et al asks for the development of a Kway merge of sorted lists, using a minheap (or priority queue). This problem asks you to write a Java program to implement this algorithm. In particular,
To ensure encapsulation of the heap/priority queue, your Kway merge must provide an implementation of the abstract class PQ.java.
Elements inserted into the minheap (or priority queue) must be elements of
class PQItem that implements Java's Comparable
interface. That is, the header for PQItem should be
public class PQItem implements Comparable<PQItem>
Priority of elements in PQ.java are determined by
the compareTo
method within class PQItem, where
object item1
is given higher priority than item1
if item1.compareTo(item2) < 0
.
Your Kway merge may begin with sorted data in either K lists or K arrays. The program should not require that these lists or arrays have the same length.
Your Kway merge should produce sorted data in a single list or array, consistent with the type of the inputs. For example, if your beginning sorted data come from lists, then the resulting data should also be in a list.
RedBlack Tree Insertion: (Patterned after Cormen et al, 13.32) Show the redblack tree that results after successively inserting the keys 50, 75, 85, 25, 10, 20, 15, 0 into a tree that was initially null.
RedBlack Tree Deletionion: Consider the following RedBlack Tree:
Show the redblack tree that results after successively deleting the keys 5, 26, 38, 40, 56, 80, 90, 50.
Graphs: Consider the weighted, directed graph with:
Edges:  <A,I>: 2  <B,A>: 3  <B,I>: 4  <C,B>: 2  <D,C>: 3  <D,E>: 4  <D,G>: 8  
<E,F>: 2  <F,G>: 3  <F,L>: 3  <G,H>: 2  <G,K>: 4  <H,J>: 5  <I,H>: 3  <L,D>: 1 
When considered as a directed, weighted graph, this gives the following diagram
In several of the following exercises, the relevant algorithm sometimes allows choices of vertices or edges that meet certain criteria. Whenever there is a choice of vertices, choose the one that comes first in alphabetical order. Whenever there is a choice of edges, choose the edge with initial vertex coming first in alphabetical order; if two edges have the same initial vertex in alphabeticqal order, choose the edge with final vertex coming first in alphabetical order.
This document is available on the World Wide Web as
http://www.cs.grinnell.edu/~walker/courses/301.fa11/assignments.shtml
created 17 August 2011 last revised 22 October 2011 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 