Fund. CS II (CS152 2005F)

Exam 4: Final Examination

Distributed: Friday, 2 December 2005
Due: 5:00 p.m., Friday, 16 December 2005
No extensions.

This page may be found online at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Exams/exam.04.html.

Contents

Preliminaries

There are four problems on the exam. Some problems have subproblems. Each problem is worth twenty-five (25) points. The point value associated with a problem does not necessarily correspond to the complexity of the problem or the time required to solve the problem.

This examination is open book, open notes, open mind, open computer, open Web. However, it is closed person. That means you should not talk to other people about the exam. Other than as restricted by that limitation, you should feel free to use all reasonable resources available to you. As always, you are expected to turn in your own work. If you find ideas in a book or on the Web, be sure to cite them appropriately.

Although you may use the Web for this exam, you may not post your answers to this examination on the Web (at least not until after I return exams to you). And, in case it's not clear, you may not ask others (in person, via email, via IM, by posting a please help message, or in any other way) to put answers on the Web.

This is a take-home examination. You may use any time or times you deem appropriate to complete the exam, provided you return it to me by the due date.

I expect that someone who has mastered the material and works at a moderate rate should have little trouble completing the exam in a reasonable amount of time. In particular, this exam is likely to take you about six hours, depending on how well you've learned topics and how fast you work.

You must include both of the following statements on the cover sheet of the examination. Please sign and date each statement. Note that the statements must be true; if you are unable to sign either statement, please talk to me at your earliest convenience. You need not reveal the particulars of the dishonesty, simply that it happened. Note also that inappropriate assistance is assistance from (or to) anyone other than Professor Rebelsky (that's me).

1. I have neither received nor given inappropriate assistance on this examination.
2. I am not aware of any other students who have given or received inappropriate assistance on this examination.

Because different students may be taking the exam at different times, you are not permitted to discuss the exam with anyone until after I have returned it. If you must say something about the exam, you are allowed to say This is among the hardest exams I have ever taken. If you don't start it early, you will have no chance of finishing the exam. You may also summarize these policies. You may not tell other students which problems you've finished. You may not tell other students how long you've spent on the exam.

You must both answer all of your questions electronically and turn in a printed version of your exam. That is, you must write all of your answers on the computer, print them out, number the pages, put your name on every page, and hand me the printed copy. You must also email me a copy of your exam by copying the various parts of your exam and pasting it into an email message. Put your answers in the same order as the problems. Please write your name at the top of each sheet of the printed copy. Failing to do so will lead to a penalty of two points.

In many problems, I ask you to write code. Unless I specify otherwise in a problem, you should write working code and include examples that show that you've tested the code.

Just as you should be careful and precise when you write code and documentation, so should you be careful and precise when you write prose. Please check your spelling and grammar. Since I should be equally careful, the whole class will receive one point of extra credit for each error in spelling or grammar you identify on this exam. I will limit that form of extra credit to five points.

I will give partial credit for partially correct answers. You ensure the best possible grade for yourself by emphasizing your answer and including a clear set of work that you used to derive the answer.

I may not be available at the time you take the exam. If you feel that a question is badly worded or impossible to answer, note the problem you have observed and attempt to reword the question in such a way that it is answerable. If it's a reasonable hour (before 10 p.m. and after 8 a.m.), feel free to try to call me in the office (269-4410) or at home (236-7445).

I will also reserve time at the start of classes next week to discuss any general questions you have on the exam.

Problems

Problem 1: Deletion in Linear-Probe Hash Tables, Revisited

Topics: Hash Tables, Vectors, Data Structure Design

Expected time: Two hours

Code Base:

As we discussed in our exploration of hash tables, there are two strategies we can use to handle conflicts in hash tables: We can put multiple values in each cell (the bucket strategy) or we can provide some spare space in the vector and shift values into neighboring cells (the linear-probe strategy).

We explored the linear-probe strategy in some depth in exam 3. As we discovered in that exam, deletion from a linear-probe hash table can be difficult if we decide to shift values after deletion.

An alternate strategy is to put a special value in a cell in the Vector when we remove something. This special value represents there was something here, but there is no longer. It should not be null, which represents nothing there. When we search the table, we do not stop at this special value, but rather continue until we either find the matching value or reach a space (null). When we insert, we also continue through this space, but remember it as a potential place to insert. In addition, when we find something, we may shift it into this space to speed the next search.

Consider what happens if we decide to remove 25 from the following table.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|68|70|  |53|  |55|25|17|35|49|
+--+--+--+--+--+--+--+--+--+--+

We put in the special placeholder.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|68|70|  |53|  |55|**|17|35|49|
+--+--+--+--+--+--+--+--+--+--+

We're now done with removal.

Suppose we next search for 45. We look in space 5. 45 is not there. We look in space 6. The special symbol is there, so we skip over it (and remember it). We look in space 7. 45 is not there. We look in space 8. 45 is not there. We look in space 8. 45 is not there. We wrap around to space 0. 45 is not there. We look in space 1. 45 is not there. We look in space 2. Space 2 is empty (null), so we give up and indicate that 45 is not in the table.

Suppose we next search for 35. We look in space 5. 35 is not there. We look in space 6. The special symbol is there, so we skip over it (and remember it). We look in space 7. 35 is not there. We look in space 8. 35 is there, so we've found it. Since we skipped over a special space, we move the 35 there and make space 8 special.

We put in the special placeholder.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|68|70|  |53|  |55|35|17|**|49|
+--+--+--+--+--+--+--+--+--+--+

Suppose we again search for 35. We look in space 5. 35 is not there. We look in space 6. 35 is there, so we're done.

Now let us consider insertion into the table. Suppose we insert 68. We look in space 8. The special symbol is there. We might be tempted to insert 68 immediately, but we have a slight problem (as you know from looking at the table), there might be another 68 in the table. Hence, we search in space 9 and then 0 to find the 68 and then delete it.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|**|70|  |53|  |55|35|17|68|49|
+--+--+--+--+--+--+--+--+--+--+

Suppose we now insert 18. We look in space 8. That space is full and does not contain 18. We look in space 9. That space is full and does not contain 18. We wrap around to 0. That space contains the special marker, so we remember it. We look in space 1. That space is full and does not contain 18. We look in space 2. That space is empty, so 18 is not already in the table. We can put it in space 0 (which we remembered).

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|18|70|  |53|  |55|35|17|68|49|
+--+--+--+--+--+--+--+--+--+--+

Note that we can have multiple special symbols in the table. For example, suppose we've deleted both 49 and 70. The table now resembles the following

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|18|**|  |53|  |55|35|17|68|**|
+--+--+--+--+--+--+--+--+--+--+

Suppose we now try to insert 38. We look in 8. It can't go there. We look in 9. We remember the potential placement. We wrap around to 0. It can't go there. We look in 1. We skip over it. We reach 2. We know that 38 is not in the table. Should we insert it in space 9 or space 1? Clearly, it should go in space 9, since that space is closer to the original hash place.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|18|**|  |53|  |55|35|17|68|38|
+--+--+--+--+--+--+--+--+--+--+

Suppose we now delete the 68 in space 8.

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|18|**|  |53|  |55|35|17|**|38|
+--+--+--+--+--+--+--+--+--+--+

Now, suppose we insert 10. We look in space 0. That space is full. We look in space 1. That space contains the placeholder. We look in space 2. That space is empty. Hence, we put the 10 in space 1 (the first placeholder we saw).

  0  1  2  3  4  5  6  7  8  9  
+--+--+--+--+--+--+--+--+--+--+
|18|10|  |53|  |55|35|17|**|38|
+--+--+--+--+--+--+--+--+--+--+

Rewrite the linear-probe hash table code to use this strategy.

Note that in addition to writing remove, you will need to rewrite find and insert.

Problem 2: Expanding Linear-Probe Hash Tables

Topics: Hash Tables, Vectors, Data Structure Design, Data Structure Analysis

Expected time: One hour

As I noted in class, hash tables only perform reasonably well if they are not too full.

a. Rewrite LinearProbeHashTable so that it expands the underlying Vector (and, presumably, rehashes all the values) whenever the vector is more than 60% full.

b. Experimentally determine the longest sequence of filled cells in randomly-filled linear-probe hash tables of sizes 128, 256, 512, and 1024. Please try at least three experiments per size.

Problem 3: Bottom-Up Merge Sort

Topics: Sorting, Vectors, Comparison, algorithm analysis

Expected time: Two hours

Code files:

We traditionally think of merge sort as a top-down algorithm. You divide the vector or list in two, sort the two halves, and merge the sorted halves. However, it is also possible to write merge sort as a bottom-up algorithm. First, you merge neighboring elements into sorted pairs. Then you merge neighboring pairs into sorted quadruplets. Then you merge neighboring quadruplets into sorted octuplets. You continue until you've merged everything together. Of course, you won't always have a power of two as the size of the Vector, so you'll need to handle the special case in which one of the things you're merging is not as big as the other.

Let's consider the way in which we might merge sort the following Vector of size 15

+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|11|27|05|99|82|05|77|26|15|01|98|12|33|44|50|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

We start by merging neighboring elements to form pairs. Each pair will now be in order. The 50 at the end is by itself.

|     |     |     |     |     |     |     |  |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|11|27|05|99|05|82|26|77|01|15|12|98|33|44|50|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

We merge each two pairs to form quadruplets. The last set of three forms a near-quadruplet.

|           |           |           |        |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|05|11|27|99|05|26|77|82|01|12|15|98|33|44|50|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

We merge the quadruplets. The last set of seven forms a near-octuplet.

|                       |                    |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|05|05|11|26|27|77|82|99|01|12|15|33|44|50|98|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

We make the final merge.

|                                            |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|01|05|05|11|12|15|26|27|33|44|50|77|82|98|99|
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

a. Implement this algorithm in MergeSorter.java.

b. Experimentally determine whether or not the running time is the claimed O(nlog2n).

Problem 4: Improved Vectors

Topics: Inheritance, Vectors, Parameterized Classes

Expected time: One hour

As you may have noted, in spite of the wide range of methods provided by the Vector class, it still misses some key operations that we regularly need. In particular, Vectors lack (a) a method to swap two elements, (b) a method to make a copy of the Vector and ensure that it's the correct type, and (c) a method to reverse the order of elements in the vector.

Extend the Vector class to create a new class, BetterVector that also provides these three methods.

You should give the methods the following signatures.

Test your work.

Some Questions and Answers

These are some of the questions students have asked about the exam and my answers to those questions.

Errors

Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to one point of extra credit for everyone. I limit such extra credit to five points. After the first five points, each five errors correspond to one additional point of extra credit.

 

History

Thursday, 8 December 2005 [Samuel A. Rebelsky]

Friday, 9 December 2005 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri Dec 9 10:14:14 2005.
The source to the document was last modified on Fri Dec 9 10:14:09 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005F/Exams/exam.04.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu