These are to be turned in to the HW2 Drop Box. See submission instructions in HW1. After you have submitted, click on the drop box again to verify that your submission was successful.
A single late day may be used or earned for a homework: you must turn in all parts early to earn a late day; if you turn in any part late, you will use a late day.
The following formulas for series sums may be helpful:
Problem numbers [in square brackets] are the numbers from the 3rd edition of the Weiss book.
for (int i=0; i <= n+2; i++)
for (int j=0; j < n * n; j++)
sum++;
for (int i=0; i < n; i++)
for (int j=i; j < n; j++)
sum++;
for (int i=0; i < n; i++)
for (int j=0; j < i * n; j++)
for (int k=0; k < n; k++)
sum++;
for (int i=n; i >= 1; i = i / 2)
sum++;
(10 points) Weiss Problem 5.21 [5.16]. You only need to do the
analysis, not an implementation. This one is more complex than any of
the parts of problem 5.20 [5.15]. As with our analysis of the cubic
algorithm for MCSS in class, you should first derive a (closed-form,
no-summation notation) formula for the exact number of times that the
sum++
statement executes. Be sure
to explain what you are doing, not just write equations. Use your
derived formula to state a big-Theta estimate; you can use the usual
shortcut of dropping lower-order terms and constant factors only at
this last stage.
You do not have to implement and run the code.
(10 points) Weiss Exercise 5.30 [5.23]. Note that your method is
supposed to efficiently search the sorted (increasing) array
a
of integers, looking for an integer
i
for which a[i] == i
and return
true
if and only if there is such an
i
.
You may assume that the array has no duplicate entries, and therefore is strictly increasing. I.e.
a[j-1] < a[j]
for all
j
with
0 < j < a.length
. You may
not
assume that all of the integers in the array are non-negative, because then the problem would be boring.
Show your algorithm and the results of your analysis. Obviously you should try to come up with an efficient algorithm; some of the credit for this problem will be based on your algorithm's efficiency. (Think about it carefully—is O(N) "efficient" for this task? Anyone can write a loop from 0 to a.length.)
These problems are for you to think about and convince yourself that you could do them. It would be good practice to actually do them, but you are not required to turn them in.
Weiss Exercise 6.5 [6.5]. (You may have to come back to it a few times before you figure out what to do.) Here is a more detailed explanation:
The goal of this problem is to implement a collection data type that provides three operations and does them efficiently:
push(obj); // adds obj to the collection.
obj = pop(); // removes and returns the most recently-added object.
obj = findMin(); // returns the smallest object (assume that all
// of the objects in the collection are Comparable).
A single stack can do the first two operations in constant time, but
not the third. But if our implementation uses TWO stacks to represent a
collection, it is possible to do each of the three operations in
constant time. Is should be obvious that our new data structure’s
push
method will have to do more work (than the
push
operation for an ordinary stack would have to do) in order for
findMin
to also be a constant-time operation. All you need to do is show the
code for the three operations. You may assume that there is already a
Stack
class that has its own
push
,
pop
, and
top
methods. The code for each of the three methods that you must provide
is short. We hope that by making things a bit more specific, it will
make it easier for you to get started on this problem.
To further assist you in understanding this problem, here is a framework in which your answers could go: A class declaration with stubs for the three methods you are supposed to write. Each of those methods should be constant time (no loops or recursion)
public class StackWithMin<E> {
// TODO: Give these two stacks names indicating what they are used for:
private Stack<E> stack1, stack2;
public StackWithMin() {
this.stack1 = new Stack<E>();
this.stack2 = new Stack<E>();
}
public void push(E element) { /* TODO: fill in the details */ }
public E pop() {/* TODO: fill in the details */ }
public E findMin() {/* TODO: fill in the details */ }
}