Due Wednesday, April 21 2004 at start of class. (Place stabled assignment with name, id number, section number, lab number, etc on top of desk at front of class.) |

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.) |

twice as large). grow() takes time proportional to the current table_size. |

twice as large). The operation grow() takes time proportional to the current table_size. |

of half the size. This saves memory. |

of half the size. This saves memory. The operation |

there is a sequence of N push() and pop() operations that takes time proportional to N^{2}. |

there is a sequence of N push() and pop() operations that takes total time proportional to N^{2}. |

operations taking more than O(N) time). Hint: if a the set_size() call does resize the table, how many push() or pop() operations must have proceeded |

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 |

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

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

In 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 time taken to support N UNION or FIND operations |

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 |

Now consider the alternate implementation outlined in section 4.2.3 of the text: |

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

Each set is represented as a tree containing the elements in that set, with edges directed from each element to its parent in the tree. |

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. |

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

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. |

set with less elements) to point to the other root (thus making one tree out of two). |

set with less elements) so that it points to the root of the other tree (thus making one tree out of two). |

3A. Show that ∑_{i=1}^{n} log i is O(n log n).It is enough to show that the sum is less than (c n log n) for some constant c. |

3A. Show that ∑_{i=1}^{n} 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). It is enough to show that the sum is at least (c n log n) for some constant c > 0. |

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. |

Note: recall from class that ∑_{i=0}^{n} c^{i} = Θ(c^{n}) for c>1.This will not be useful for the above exercises. |

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.

### 2. Union-Find using parent pointers.

### 3. O-notation, sums

### 4. Induction

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

### 5. Recurrence relations

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 N^{2}.

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()?

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).

3A. Show that ∑_{i=1}^{n} 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=0}^{n} i^{c} = Θ(n^{c+1}).
It is enough to show that the sum is both O(n^{c+1}) and Ω(n^{c+1}).

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?

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.

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) + n^{2}, T(1) = 0

5B. T(n) = 4 T(n/2) + n^{2}, T(1) = 0

5C. T(n) = 5 T(n/2) + n^{2}, T(1) = 0