Data Structures and Object Oriented Development - Exam 2 to Solutions | CS 2606, Exams of Data Structures and Algorithms

Material Type: Exam; Professor: McQuain; Class: Data Structs & OO Development; Subject: Computer Science; University: Virginia Polytechnic Institute And State University; Term: Fall 2007;

Typology: Exams

Pre 2010

Uploaded on 09/24/2008

ramblurr
ramblurr 🇺🇸

19 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2606 Data Structures and OO Devel II Test 2
1
READ THIS NOW!
Print your name in the space provided below.
Unless a question involves determining whether given C++ code is syntactically correct, assume that it is. Unless
a question specifically deals with compiler #include directives, you should assume the necessary header files
have been included.
There are 6 short-answer questions, priced as marked. The maximum score is 100.
When you have finished, sign the pledge at the bottom of this page and turn in the test and your fact sheet.
Aside from the allowed one-page fact sheet, this is a closed-book, closed-notes examination.
No laptops, calculators, cell phones or other electronic devices may be used during this examination.
You may not discuss this examination with any student who has not taken it.
Failure to adhere to any of these restrictions is an Honor Code violation.
Name (Last, First)
printed
Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.
signed
pf3
pf4
pf5

Partial preview of the text

Download Data Structures and Object Oriented Development - Exam 2 to Solutions | CS 2606 and more Exams Data Structures and Algorithms in PDF only on Docsity!

READ THIS NOW!

  • Print your name in the space provided below.
  • Unless a question involves determining whether given C++ code is syntactically correct, assume that it is. Unless a question specifically deals with compiler #include directives, you should assume the necessary header files have been included.
  • There are 6 short-answer questions, priced as marked. The maximum score is 100.
  • When you have finished, sign the pledge at the bottom of this page and turn in the test and your fact sheet.
  • Aside from the allowed one-page fact sheet, this is a closed-book, closed-notes examination.
  • No laptops, calculators, cell phones or other electronic devices may be used during this examination.
  • You may not discuss this examination with any student who has not taken it.
  • Failure to adhere to any of these restrictions is an Honor Code violation.

Name (Last, First) printed

Pledge: On my honor, I have neither given nor received unauthorized aid on this examination.

signed

For questions 1 and 2, consider the following graph G 1 :

  1. [12 points] Draw the spanning tree for G 1 obtained by applying a depth-first traversal starting at node A. Visit neighbors in alphabetical order, and number the edges in the order they are added to the tree.
  2. [12 points] Draw the spanning tree for G 1 obtained by applying a breadth-first traversal starting at node A. Visit neighbors in alphabetical order, and number the edges in the order they are added to the tree.

A B

C

D

E

F

A B

C

D

E

F

A B

C

D

E

F

  1. Recall the two types of locality, spatial and temporal , that were discussed in class. Suppose a program accesses a largecollection of records stored in a file, and that the program exhibits both kinds of locality. Suppose also that the program incorporates a buffer pool.

a) [12 points] Carefully describe how the program should use the buffer pool in order to take advantage of spatial locality.

Spatial locality is exhibited if, when a particular record is accessed, there is a high probability that a nearby record will be accessed in the near future. Taking advantage of spatial locality requires that the program pre- fetch some nearby records when a record is retrieved from the file to satisfy a query. Naturally, the fetched records would be stored in the buffer pool. The replacement policy might seem to be a concern. However, nothing special needs to be done; if the client tells the buffer pool to load a collection of records, rather than just one, then each of those "extra" records will naturally reflect either a proper recent-use time (LRU) or a proper frequency count (LFU).

b) [12 points] Carefully describe how the program should use the buffer pool in order to take advantage of temporal locality.

Temporal locality is exhibited, if when a particular record is accessed, there is a high probability that same record will be accessed again in the near future. Simply using the buffer pool in the usual manner will take advantage of temporal locality so long as the replacement policy is reasonable.

  1. [12 points] Aside from improving the selection of the pivot value, describe one technique for improving the performance of the quicksort algorithm and explain why it would make a difference.

A number of suggestions were discussed in class, including:

  • switch to a nonrecursive sorting algorithm on short sublists (say less than 16 elements); this will eliminate most of the recursive calls that would otherwise occur, and improve runtime efficiency
  • switch to an iterative implementation of the quicksort algorithm
  • find a more efficient algorithm for partitioning sublists; it is possible to reduce the number of copy operations to about 1/3 of those taken by the partitioning algorithm in the course notes
  1. [12 points] Suppose that you need to sort a list of N non-negative integer values. One objection to using binsort would be that it would require too many bins if the range of the data values was large. Describe a scenario in which, even if the number of bins is feasible, binsort would not really be Θ(N).

Note: the question is about binsort, not radix sort.

Let B be the number of bins; in any case B must be at least as large as N and typically B is much larger than N.

The pass of binsort through the data elements will leave then in sorted order, but scattered throughout the B bins. It is still necessary to collect the elements into a single list, and that will require a linear pass through the bins, which would be Θ(B).

That makes the total cost of binsort Θ(N + B). But this will only be Θ(N) if B is bounded by kN for some constant k. This, unfortunately, isn't guaranteed, even if the number of bins is feasible.

For a specific example, suppose that we are sorting a collection of N = 1000 integers in the range 0 to 1,000,000. We would need 10^6 bins, which might well be feasible, but the cost of the collecting pass would be on the order of N^2.

The size of the integers is of absolutely no concern here; binsort makes only one pass to sort the values and a second pass to collect them; and, if the integers are large but confined to a small range (e.g., 1000 integers between 900,000 and 1,000,000) then any intelligent use of binsort will take that into account and use a suitable number of bins (e.g., 100,000 instead of 1,000,000).

The occurrence of duplicate values is also irrelevant. Duplicates would simply go to the same slot; that's easily accommodated by making the slot a simple linked stack structure so insertion is still Θ(1). And, as far as binsort is concerned, it doesn't matter what order the duplicate elements occur in the slot. In the specific scenario described above, since the values being sorted are simple integers, all that's needed in each slot is actually just a counter.