# CS 226: Homework 1

## This assignment is due by Monday, October 18 at 11:59 pm. You should work independently (not in pairs or teams). Submit your solution as a MS Word file (.doc) or an Adobe Acrobat file (.pdf) (useful if you want to work in LaTex or some other editor).  Pictures and equations may, for example, be done with the tools in Word or other digital tools, or you can write/draw neatly on paper, scan, and insert as an image. There is no need to zip this file. (If you want to submit in some alternative format, please contact the TA in advance for permission).

(For problems from the textbook, you have permission to use the hints at http://java.datastructures.net)

1. Suppose an implemented algorithm takes 8sqrt(n) milliseconds to execute on an input of size n. What is the largest problem size n that can be completed in 1 minute? In 1 hour? In 1 day? Now suppose you solve the problem by implementing a different algorithm, which takes  0.001n2 milliseconds to execute. What is the largest problem size solveable in 1 minute? In 1 hour? In 1 day? For what problem size do both implementations take the same running time, and how long is that running time? (show your work).

2. Prove that 10n3logn + 15n2 is O(n4).

3. a) Give a detailed asymptotic analysis using big-Oh notation for the (worst case) running time of the following algorithm, Tweedle (see pages 126-127 in the 3rd edition or 124-125 in the 2nd edition for examples of an appropriate amount of detail).

Algorithm Tweedle(n)
Input: an integer
Output: an integer

if (n <= 0) then
return n
k=0
for i = 0 to n do
k += i
return k

b) Give a detailed asymptotic analysis of the following algorithm, Dee.
Algorithm Dee(n)
Input: a positive, even integer
Output: an integer

k = 1
j = 0
while (k <= n) do

j += Tweedle(logk) + Tweedle(2*k) + Tweedle(k*k)
k *= 2
return j

4. Chapter 3, exercise C-3.13, page 137 (3rd edition).

5. a) Give pseudo-code to implement the methods of a Deque abstract data type (insertFirst, insertLast, removeFirst, removeLast, first, last, size, and isEmpty) using 2 Stack data structures (which  take linear space) plus a constant amount of additional space. In other words, given a Stack implementation, we could use your pseudocode to implement the methods of a class StackDeque as follows:

public class QueueDeque implements Deque {
private Stack S1;
private Stack S2;

public void insertFirst(Object o) {...};
public void insertLast(Object o) {...};
public Object removeFirst() {...};
public Object removeLast() {...};
public Object first() {...};
public Object last() {...};
public int size() {...};
public boolean isEmpty() {...};
}
b) Give a detailed asymptotic analysis for the running time of each of your methods using big-Oh notation.

6. Chapter 5, Exercise C-5.5, page 239 (3rd edition).

7. Chapter 5, Exercise C-5.7, page 239 (3rd edition). Also, assuming there are n items in the sequence, analyze the asymptotic running time of the two new methods for both the case of an array-based implementation and a linked-list-based implementation.