ClassS04CS141/Hw1

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

This homework is due on Wednesday, April 21 2004 at the start of the class. (Place your stapled assignment with your name, id number, section number, lab number, etc, on top of my desk at the front of the class.)

1. Stack via shrinkable array.

Consider an Array class (as described for programming assignment 1 /Prog1) implemented by doubling the array size when necessary (as described in class wednesday week 1 --- /Notes 03 31).

Consider adding the following method (described in pseudo-code below) to that data structure:

set_size(int i) 
   max_referenced = i-1.
   // possibly resize the table:
   while (i >= table_size) grow()   --- double the size of the table until i < table_size.
   while (i < table_size/2) shrink()  --- cut the table in half until i >= table_size / 2.

The function grow() doubles the size of the table as described in class (by replacing the current array with an appropriately initialized array twice as large). The operation grow() takes time proportional to the current table_size.

The function shrink() decreases the size of the table to half its current size, by replacing the current array with an appropriately initialized array of half the size. This saves memory. The operation shrink() takes time proportional to the current table_size.

Now suppose we implement a Stack class using the Array class as follows:

template <class VALUE>
class Stack {
  Array<VALUE> array;

public:
  void push(const VALUE &v) {  array[array.size()] = v;  }
  VALUE top() const { if (array.size() > 0) return array[array.size()-1]; else exit(-1); }
  VALUE pop() 
    if (array.size() > 0) {
       VALUE v = top();
       array.set_size(array.size()-1);
       return v;
    } else exit(-1);
  }
}

1A. Show that with this implementation of the Stack class, there is a sequence of N push() and pop() operations that takes total time proportional to N2.

1B. Now suppose that the set_size() method of the Array class is modified as follows:

set_size(int i) 
   max_referenced = i-1.
   while (i >= table_size) grow()
   while (i < table_size/4) shrink()  -- note that the "2" has been changed to "4"

Claim: if the Stack class is implemented with the Array class modified in this way, then any sequence of N push() and pop() operations on such a Stack takes O(N) time.

Prove this claim, or find a counter-example (a sequence of N push() and pop() operations taking more than O(N) time). Hint: if a set_size() call does grow or shrink the table, how many push() or pop() operations must have preceded the call since the previous resizing of the table by set_size()?

2. Union-Find using parent pointers.

Recall the UNION-FIND data type discussed in class on Friday, week 1 (/Notes 04 02).

In that class we discussed a particular implementation of the data type (also described in Section 4.2.2 of the text), and showed that, for that implementation, the total time taken to support N UNION or FIND operations on M elements was O(N + M log M).

Now, consider the following alternate implementation (also outlined in section 4.2.3 of the text):

Each set is represented as a tree; the nodes of the tree represent the elements in that set. The tree edges are directed from each node to its parent node in the tree. The root of the tree has its parent pointer directed to itself.

FIND(e) is implemented by tracing the parent pointers from the node corresponding to e to the root of e's tree, and returning the name of the element stored at the root.

UNION(i,j) is implemented by taking the root node of i's tree and the root node of j's tree, and changing the parent pointer of the root of the smaller tree (the tree of the set with less elements) so that it points to the root of the other tree (thus making one tree out of two).

(See figure 4.8 of the text.)

Suppose the data structure is implemented as described above. (That is, with Union-by-size, but without path compression, as described in section 4.2.3.)

2A. Find a sequence of N UNION and FIND operations on M elements that takes time at least proportional to N log M.

2B. Prove that any sequence of N UNION and FIND operations on M elements takes time at most O(N log M).

3. O-notation, sums

3A. Show that i=1n log( i) is O(n log n). Hint: It is enough to show that the sum is less than (c n log n) for some constant c.

3B. Show that the sum above is Ω(n log n). Hint: It is enough to show that the sum is at least (c' n log n) for some constant c' > 0.

3C: Show that i=0n ic = Θ(nc+1). It is enough to show that the sum is both O(nc+1) and Ω(nc+1).

4. Induction

4A. Prove by induction that every tree with N nodes has N-1 edges.

4B. What exactly is wrong with the following line of proof? Which step is flawed?


Claim: For every n=1,2,3,..., in any set of n people, all people in the set share the same name.

Proof: By induction on n. The base case is n=1. Clearly the claim is true when n=1: in any set containing just one person, all the people in the set have the same name (since the set has just one person).

For the inductive step, suppose n > 1. Consider any set of n people. Line the people up in a row. Consider people 1,2,..,n-1. Since these people form a group of n-1, we can assume by induction that they all share the same name, say, X. Now consider people 2,3,...,N. Since these people form a group of n-1, we can assume by induction that they all share the same name, say, Y. Now, since people 2,3,..,N-1 are in both groups, and each has name X as well as name Y, it must be that X = Y (each person has only one name). Therefore, all people 1,2,...,N share the same name.


5. Recurrence relations

Describe the recurrence tree, and give the best big-O upper bounds you can for T(n), for each of the following recurrences. Explain why your big-O upper bounds are correct.

5A. T(n) = 3 T(n/2) + n2, T(1) = 0

5B. T(n) = 4 T(n/2) + n2, T(1) = 0

5C. T(n) = 5 T(n/2) + n2, T(1) = 0


ClassS04CS141 | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 17, 2004 6:16 pm by 66-215-226-222.riv-eres.charterpipeline.net (diff)
Search: