Homework Assignments

All programming homework assignments must be written in C++ and run on the bin machines (those in Stocker 307). Be sure to test your code on these machines to be sure you will get full credit for your code.

Homework Assignment 6

Due Mar 1, 2012

This assignment focuses on selection, sorting, and dynamic programming.

Clarifications/Hints

First, we need to find a set of students in the top k% of GPA. Assume all the students have the same GPA, then all of them will be in the top k% range. Then my algorithm should return all of the students instead of just k% of the all the students, is this correct?

Yes, if all the students have the same GPA, then the top k% would have to be all of them.

Second, after selecting out the set of students with top k% GPA, should my algorithm return the students in the order of GPA (from lower to higher) or just in the any order?

As the assignment asked for a set, which is inherently unordered, you do not have to sort the set of students returned.


Homework Assignment 5

Due Feb 21, 2012

This assignment focuses on image processing.

Clarifications/Hints

Today (2/16/12) in class we decided the following issues:

  1. Multiple Modes: In the case that more than one value is the mode, we decided that the mode closest to 255 should be reported. (i.e. the brightest mode).
  2. How to interpret percentage of pixels to be turned red. We decided that all pixels at or above the value of the pixel that is at x% of the way from brightest to dimest pixel should be turned red. This means that at least X% of the output will be turned red, but it could be a higher number of pixels than X% that is red.
  3. The sorting order is row-major which means that the dimest pixel should be at the first column of the first row. Rows increase in brightness as the column number increases, and the brightest pixel should be at the bottom right of the output image.
  4. The percentage of pixels to turn red will always be an integer.
  5. The maximum value of a pixel in all the images used to test your code will be no higher than 255.
The example call should have been:

% ./prog2 -p 10 -i overexposedGrey.pgm -s overexposedSorted.pgm -r overexposedRed.ppm

Homework Assignment 4

Due Feb 9, 2012

This assignment consists of working the following 4 problems from the midterm (for this assignment all problems will have equal weight):

2, 5, 6, and 7 (prove both part a and part b)

Clarifications/Hints

I am having trouble finding out why the proof fails on problem #5.

T(n) = 5T(n/5) + 2n
Claim: T(n) = O(n)
Hypothesis: T(n) <= c*n for all m < n
Base case: T(1) = c -> c*1 = c
Proof by induction:
T(m) = 5T(m/5) + 2m         |
<= 5(c*(m/5)) + 2*m         |
     = c*m + 2*m            |
     = (c+2) * m            |
<= c * m                    |  if c >= 2
I don't understand. I can find no algebraic errors, all signs seem to hold true. And I was able to get the equation back to the hypothesis form, which proves it correct, unless I am terribly mistaken.

Hint: if x+2 >= 42 is x >= 42? If not in all cases, then your proof is incorrect.

Also, have you tried using the master theorem to see what it gives for the solution to this recurrence?


Homework Assignment 3

Due Feb. 2, 2012

This assignment focuses on sorting. Clarifications

For problem 5, be sure to justify in detail the run time of your algorithm.

Hi, I am having a problem understanding the first question of homework 3. I don't get what you mean by "achieve the smallest average number of comparisons to sort a list into the the order 1,4,2,3." Could you explain what you mean by "order"? Does 1,4,3,2 represent a leaf of the decision tree? Or is it derived from one of the leafs?

This problem asks you to sort a list into a particular order. The order isn't smallest to largest, it is as stated in the problem. Whatever item in the list is smallest should be the first item in the output list. Whatever item is largest should be the second item in the output list. etc. Thus the output order is 1432 (which represents where each item would go from an input list that was in order smallest to largest). Your job is to produce a decision tree that solves the problem of taking an input list that is in an arbitrary order, and outputting a list sorted as stated. Of all the possible decision trees, your tree must use the fewest average number of comparisons between elements to sort an input list.

E.g. if the input list was "atom", "big", "plant", "little" the output should be "atom", "plant", "big", "little" or if you consider the indexes, the output item in the first position was at the old 1, then 3, then 2 then 4.


Homework Assignment 2

Due Jan. 26, 2012

This assignment focuses on recurrence relations, and simple algorithms.

Clarifications

In class Thr. we decided that no vector of students will consist of fewer than 4 items.

We also decided that not two student records will have the same First name, last name and SSN. At least one of these must be different for each element of the vector of students.

There is a return value num_compares in FindMaxMin() function. Does it mean that we need to count that how many comparisons happen inside FindMaxMin() function?

Yes, your function must count how many comparisons it does. The comparisons you are to count are those involving the entire entry (see also the next question below).

Someone asked if the number of comparisons counted only the comparisons of entire elements in the input.

The intent of the assignment was to get you to think about how to efficiently solve this problem using comparison of entire elements (i.e. Names/SSNs). As for the 4 pt. bonus, solutions that do not use comparison of entire elements would have to be judged on a different basis. A thorough justification for any other method would have to be provided, including data validating why you believe your method is better.

Two other criteria for the assignment were to use an efficient solution (one that asymptotically runs the fastest). And one that on "cpu seconds" consumes the least amount of time. Both of these criteria are also important to scoring well.

Are we allowed to edit the student.h file that we download from prime?

No, you MUST use the student.h file as is. All the member variables are public so you can access them directly. Remember the focus of this assignment is on efficiency. Adding a lot of functions between you and the data will likely increase the access time.

I'm not sure yet how to use FindMaxMin.h and FindMaxMin.cc to come up with functions that both create and read Student objects that are defined in a separate file entirely.

As for these sorts of functions, it might be best not to read the data from a file, but use a function to generate random data. However, it is also reasonable to have a fixed file to use each time so that you can tell if you are making changes that make your code faster.

If you use srand to set the seed of the random number generator, you can ensure that each time you run your program you'll get the same randomly generated data

You should be able to write a very simple function to read a student object in from a file. Since all the member variables are public, simply do a new to create a student object, then read in each of the fields it requires.

Have a quick question about the programming part of the second homework. when calling the function from main.cc, my function just prints this output

Max:5 Min:0
Max2:6 Min2:1
is this sufficient enough?

In addition to showing your function is correct, your submitted main program must thoroughly test your function, and provide timings to show you have tried to optimize your code. In addition, an analysis of the number of comparisons utilized should be reported. One test case is NOT sufficient. Also, your tests should show the time behavior of your function as input sizes increase.


Homework Assignment 1

Due Jan. 17, 2012

This assignment reviews basic material (first three chapters of the book).

Clarifications

I'm having trouble with the nlgn part of problem 1. Do you have any hints to get me going in the right direction? Thank you.

A couple of hints:
1.) You cannot solve equations like this exactly (there is no closed-form solution).

2.) You do have access to a computer that is great at calculating lots and lots of trials.

3.) Look up the bisection method in google for solving the roots of numerical equations. I.e. if you can put the problem in the form of:

f(x) = 0
There are several relatively easy ways to solve it.


Notes about STL

For all of the programming assignments, you will be free to use STL vector, list, stack, and queue classes, but no other STL classes. Remember, you must fully understand how all operations work in any included code in order to be able to properly analyze the space and time complexity of your own code.

In general, since you will be learning how to implement good algorithms, use of the STL algorithm sort, or the list member function sort is not allowed (but see below for exceptions).

Be sure to credit all sources for any code that you did not write yourself! Any code you turn in must be written according to the style guidelines for this class, which includes (among other things) headers that include an asymptotic analysis of space and time complexity.

NOTE: Homework assignments are also available on prime/p1/p2 clients. These files are in the same format as the lecture notes. You can find them in ~cs404/homework. You can print them in the same way as the lecture notes.


Class Homepage

David M. Chelberg <chelberg@ohiou.edu>
Last modified: Wed Feb 29 10:48:22 EST 2012