Download CS 1501 Fall 2006 Midterm Exam 2 and more Exams Computer Science in PDF only on Docsity!
CS 1501 Fall 2006 Midterm Exam 2 Name____________________________________ Section (2PM or 6PM) ______________________ You are not allowed to use calculators. You have 50 minutes.
- (20 points).. a) Define “Open addressed” and “closed addressed” hashing. Closed address means that the hash table entry contains a point to all items hashed to this hash table entry. Open addressed means that the hash table entries are the actual records. b) Assume that you are implementing double hashing. Let h 1 (x) be the primary hash function, and h 2 (x) be the secondary hash function. What is the probe sequence for an element x? The ith probe, i = 1, 2, …, n is (h 1 (x) + (i-1) h 2 (x)) mod n where n is the table size c) Define the Kolmogorov complexity of a string/file F. The Kolmogorov complexity of a string F is the length of the shortest program that writes out F given no input. Strictly speaking the exact value of this length depends on the programming language, but it is easy to show that the choice of programming language only changes the length by an additive constant. d) Assume that you are implementing a Bloom filter with the k hash functions h 1 , h 2 , …, hk, and a hash table/array H. Explain how to implement a SEARCH/FIND operation. A
word. To compute the number of codewords k, we need to solve Σi=1k^ I = 106 /2 for k. This sum is about k^2 /2. Thus k is about 10^3. So the number of bits in the output is 10^3 times the number of bits in a codeword, which is not specified in the problem. But a reasonable assumption is say about 16 bits for a codeword. This would about 16000 bits in the output.
- (15 points) Recall that the subset sum (or change making) problem takes as input positive integers x[1] … x[n] and L. Think of the x[i]’s as being the values of n coins. The goal is to determine whether there is a subset of the x[i]’s (coins) that sums to L. Give “iterative, array based, bottom-up” pseudo-code to solve this problem. Do not forget to initialize your array, and do not forgot to output your answer once you have filled in the array. Your answer need only be yes/no, you do not need to find the actual subset of coins. For i = 0 to L do A[i]= A[0]=A[x[1]]= For i = 2 to n do For k = 0 to L do A[i, k] = A[i-1, k] or A[i-1, k-x[i]] Return A[n,L]
- (10 points) Consider running Dijkstra’s (called Priority Search in the book) shortest path algorithm on the following graph starting from vertex S. State the order that the edges are added to the shortest path tree. Specify an edge by giving the two endpoints. For example, to get you started the first edge added is (S, A).
(S, A) (S, B) (A, D) (B, E) (E, C) (E, F)
- (10 points) Consider running Prim’s (called Priority Search in the book) minimum spanning tree algorithm on the following graph starting from vertex S. State the order that the edges are added to the minimum spanning tree. Specify an edge by giving the two endpoints. For example, to get you started the first edge added is (S, A).
S
A
C
B
E
D
F
Logical integer output: 3 2 1 0 1 3 Letter: D D D D A C Actual ouput file 000111 000110 00001 00000 00001 000111
- (15 points) Write pseudo-code to delete the minimum key in a binary heap H. Assume that the minimum key is at the root of H. Assume for simplicity that H stores the actual keys (so this is simpler than the implantation that you would need for priority first search of a graph). Assume you have a variable n that tells you the number of keys in the heap. So for example if H was 12 14 15 17 19 34 44 1 2 3 4 5 6 7 8 Then your code should remove the key 12 from H , and restructure H so that it is still a heap.
Your code should run in O(log n) time. Code from the text: itemType PQ::remove() {itemType v = a[1]; a[1] = a[N--]; downheap(1); return v; } void PQ::downheap(int k) { int j; itemType v; v = a[k]; while (k <= N/2) { j = k+k; if (j<N && a[j]<a[j+1]) j++; if (v >= a[j]) break; a[k] = a[j]; k = j; } a[k] = v; }