











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Professor: Earls; Class: Data Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2009;
Typology: Exams
1 / 19
This page cannot be seen from the preview
Don't miss anything!












7-10p, Tuesday, December 15
Suppose your goal is determine whether or not a graph contains a vertex that is connected to no other vertices. How long does the best possible algorithm take if the graph is implemented using adjacency lists? an adjacency matrix? (As always, assume there are n vertices and m edges.)
(a) O(1) for lists and O(n) for the matrix. (b) O(deg(v)) for lists and O(n) for the matrix. (c) O(n) for lists and O(n^2 ) for the matrix. (d) O(m) for lists and O(n^2 ) for the matrix. (e) None of these answers is correct.
When should a pointer parameter p be a reference parameter? (That is, when would it be more appropriate for a parameter list to be (myType * & p) rather than (myType * p)?)
(a) When the function changes p, and you want the change to affect the actual pointer argument. (b) When the function changes p, and you do NOT want the change to affect the actual pointer argument. (c) When the function changes *p, and you want the change to affect the object that is pointed at. (d) When the function changes *p, and you do NOT want the change to affect the object that is pointed at. (e) When the pointer points to a large object.
Suppose we have implemented the Stack ADT as a singly-linked-list with head and tail pointers and no sentinels. Which of the following best describe the running times for the functions push and pop, assuming there are O(n) items in the list, and that the bottom of the stack is at the head of the list (all pushing and popping occurs at the tail)?
(a) O(1) for both functions. (b) O(n) for both functions. (c) O(1) for push and O(n) for pop. (d) O(n) for push and O(1) for pop. (e) None of these is the correct choice.
Depth first search, when run on the following graph, beginning at node A, classifies edge (c, f ) as. Assume that the edge iterators are set up to process adjacent vertices in the (left to right) order given in this adjacency list representation of the graph.
(a) “back” (b) “cross” (c) “discovery” (d) “predecessor” (e) None of these is the correct choice.
Suppose we implement Kruskal’s algorithm using a sorted array as our priority queue and an adjacency matrix graph. What is the tightest worst case running time of the algorithm? (As usual, |V | = n, |E| = m, and you may assume disjoint set functions run in constant time.)
(a) O(n^2 ) (b) O(n + m) (c) O(m log n) (d) O(n^2 + m log n) (e) None of the above accurately describes the running time of Kruskal’s algorithm.
Suppose we implement an algorithm to determine whether or not a simple undirected graph is connected using a modification of a graph algorithm we saw in class. What is the tightest worst case asymptotic running time of the best algorithm to do this?
(a) O(n^2 ) (b) O(n + m) (c) O(n log n + m log n) (d) O(n)
Which of the following array-based representations of a forest of up-trees could not possibly result from a sequence of union and find operations if union-by-size and path compression are employed?
(a)
(b)
(c)
(d)
(e) All of these are valid forests of up-trees.
In this problem you will describe a C++ class that supports nearest-neighbor search among 1-dimensional points consisting of a single real value. The abstract data type corresponding to the class definition of OneDSearch:
Implementation notes: Your data structure should perform each operation in worst-case O(log n) time. Your answers will be graded on correctness, efficiency, clarity, and succinctness.
(a) (7 points) Write the entire public portion of the class definition for OneDSearch, pay- ing particular attention to const correctness and, depending on your implementation, responsible memory management.
class OneDSearch { public:
private: // you don’t have to write this };
(b) (3 points) The best implementation of this class most nearly resembles which of the following data structures from our course? BST AVL Tree Hash Table SkipList Heap KD-Tree DFS BFS (c) (7 points) Write the C++ code for public member function query(double y). Be sure to comment your code so we know what you’re doing. You may write a private helper function, if you’d like.
(d) (3 points) Suppose you want to add a function rangeQuery(int a, int b) that returns a list of all the values in the structure between a and b (inclusive). The tightest, worst- case running time of rangeQuery in terms of n, the number of values in the structure is...
O(1) O(log n) O(n) O(n log n) O(n^2 )
(a) (5 points) In class we examined a particular scheme for creating the “towers” of a skip list using coin flips. What is the probability we see a sequence of coin flips corresponding to a tower containing exactly 3 pointers?
(b) (5 points) Suppose we want to insert the key 35 into the structure below, and we’ve flipped coins to determine that the tower containing 35 should have exactly 3 point- ers. In the diagram, neatly mark the nodes that are compared when the function call insert(35) is made. Please mark each node in every level at which a comparison takes place (so that some nodes will have multiple comparisons). In addition, place a small x on the pointers in the original structure that have to be reassigned in order to insert the new node.
(c) (3 points) Suppose you are flipping coins to create a tower and that each flip is the last one with probability p, and with probability (1-p) you keep going. Then what is the probability a tower contains exactly k pointers?
(d) (3 points) Let random variable X denote the number of pointers in a single tower. Com- pute E[X], the expected number of pointers in a tower if the probabilities are as described in part c). Show your work!
(e) (2 points) Define a skip list to have height (H) equal to k if the maximum level of any tower is level k − 1, and suppose towers are created by flipping fair coins. We have derived an upper bound on the height of an n item skip list:
P (H > k) ≤
Circle the correct bound: 2 nk 1 − ( n 2 )k^ ( (^1) k )n^ (1 − (^) kn^1 ) ( 12 )nk
(f) (2 points) Use the result from part e) to give a bound on the probability that a skip list containing 2^20 keys (approx. 1m) has height greater than 60?
i. (7 points) Write function changeKey. Be sure to comment your code! void Heap
ii. (3 points) What is the tightest bound on the worst case running time of changeKey?
O(1) O(log n) O(n) O(n log n) O(n^2 )
iii. (2 points) Is changeKey a public or private member function of the Heap class?
Consider the following code fragment:
minPQ
Assume that a[] is an array containing n integers, n ≥ k≥ 1, and minPQ is implemented using a binary heap.
(a) (5 points) What does the code output?
(b) (5 points) What is the tightest bound on its worst case running time?
O(k log k) O(k log n) O(n log k) O(n log n) O(nk) O(n^2 )
Now suppose we remove the line marked <3 <3 <3 <3 <3, and we implement minPQ as an unsorted array.
(c) (5 points) What does the code output?
(d) (5 points) What is the tightest bound on its worst case running time?
O(k log k) O(k log n) O(n log k) O(n log n) O(nk) O(n^2 )
Below is partial pseudocode from an algorithm we discussed in class. Assume it is a member function of a graph class consisting of weighted, undirected edges. Also assume that any instance of the graph class contains a collection of vertices V with |V | = n and a collection of edges E with |E| = m.
graph & graph::_____________________( vertex s ) {
for each vertex v in V { d[v] = INFINITY; labelled [v] = false; predecessor[v] = empty; }
d[s] = 0;
for n iterations { vertex v = minimum distance vertex among unlabelled vertices; // (12) labelled[v] = true;
for each vertex w adjacent to v if (!labelled[w]) if (weight(v,w) < d[w]) { d[w] = weight(v,w); // (18) predecessor[w] = v; } }
graph returnGraph; returnGraph.V = V; for each vertex v in V if (v != s) returnGraph.addEdge(v,predecessor[v]);
return returnGraph; }
(a) (4 points) What is the name of the function described above, and what does it return?
(b) (3 points) In the worst case, how many times does is the assignment statement d[w] = weight(v,w); made over the execution of the entire algorithm?
(c) Suppose that the degree of every vertex in the graph is no more than 10.
i. (5 points) Which graph implementation gives the most efficient running time?
ii. (5 points) Which implementation of a priority queue should be used in lines 12 and 18?
iii. (3 points) What is the running time if you implement the function this way? (Your answer should be in terms of n and/or m, and should be as accurate as possible.)
(scratch paper, page 2)