Please let me know via email (chew@cs.cornell.edu) if you find mistakes in the solutions. Past experience indicates that I may have made some.
The Final Exam will take place at noon on Monday, December 11 in Barton Hall on the east side. Note that Econ 101 students will be taking an exam at the same time in Barton central and Barton west.
The use of extra material is not permitted during the exam (i.e., no textbook, no notes, no calculator, etc.).
The exam includes all material from the entire semester. See the Materials Page for a list of topics covered in lecture and the corresponding readings.
The exam will attempt to gauge your knowledge and understanding of course concepts. You should understand the terms and concepts used in the course, but you shouldn't need to memorize the specific code given in the text or in lecture.
The review questions given here are not meant to exhaust the possible topics for exam questions. Check the online material and consider reviewing Prelims I and II, the lectures, the homework assignments, and the previous Review Questions (Review I, Review II, Extra Review Problems).
(e) (f) | | | | |17 |15 | | 3 | 10 | 1 (A)-------------(c)-------------(g)---------(h) | | | / | | | / |7 |9 |11 /5 | | | / | 12 | 14 | / (b)-------------(d)-------------(i)
max height | min height |
---|---|
1 /|\ 2 3 5 / | \ 4 6 7 / 8 |
1 /| | | | |\ 2 3 4 5 6 7 8 |
We are building trees on 8 nodes and all such trees have 7 edges. (You should be able to prove by induction that all n-node trees have n-1 edges.) Each Union operation creates a single edge, so to build an 8-node tree, we always need at least 7 Union operations.
We know that a sequence of n union/find operations using union-by-size and
path compression takes time O(n alpha(n)) where alpha(n) grows extremely
slowly. What if all the union operations are done first? Show
that a sequence of n unions followed by m finds takes time O(n + m). [Hint: The
time for a find operation is proportional to the number of links that it
changes. How many links are there?]
First recall that each Union operation takes time O(1);
thus, all we have to show is that not too much time is spent on Finds. Observe
that each edge of a tree has to be created by a Union operation; thus, the total
number of tree edges is O(n). Suppose we want to count only edges that are deep
in a tree, i.e., just those edges that are not edges from the root. There are
O(n) such deep edges.
A Find operation on item i is slow only when i is deep in a tree; then it takes
time proportional to the depth of i. The Find operation also converts a bunch of
deep edges into root edges. As a matter of fact, the Find operation takes time
proportional to a constant plus the number of deep edges converted to root
edges. Since there aren't very many deep edges (just O(n) of them), the total
time spent on Find operations is at most O((number of Find operations)+(number
of edges converted from deep edges to root edges)) or O(m+n).
Quick Sort | Insertion Sort | Merge Sort | Heap Sort |
---|---|---|---|
start: 2557316 swap: 1557326 split: 1,557326 swap: 1,257356 swap: 1,237556 split: 1,23,7556 ... |
2557316 2355716 1235576 1235567 |
start: 2557316 split: 255,7316 split: 2,55,7316 split: 2,5,5,7316 merge: 2,55,7316 merge: 255,7316 split: 255,73,16 split: 255,7,3,16 merge: 255,37,16 split: 255,37,1,6 merge: 255,37,16 merge: 255,1367 merge: 1235567 |
start: 2557316 build: 2567315 2765315 7265315 7562315 get: 556231,7 655231,7 get: 15523,67 55123,67 get: 3512,567 5312,567 get: 231,5567 321,5567 get: 12,35567 21,35567 get: 1,235567 get: 1235567 |
I've only shown part of the QuickSort; it goes on for a few more steps. For Merge Sort, I'm arbitrarily assuming that whenever we can't split evenly then the first half is smaller. For Heap Sort, I'm arbitrarily assuming that whenever there is a tie between two large children, we'll use the right child.
Using Insert | Bottom Up |
---|---|
A / \ A E / \ / \ C S R T / \ / \ / \ U D T U T R |
A / \ A E / \ / \ C S R R / \ / \ / \ U D T U T T |
Consider the following sorting methods: Insertion Sort, Merge Sort, Quick Sort, and Heap Sort.