Exam #1 Solution - Computer Science

advertisement
Computer Science II (COP 3503)
Fall 2010 Exam #1
9/29/10
Lecturer: Sarah Buchanan
Name: ____________________
1) (5 pts) What is the minimum number of comparisons necessary to sort 14 values,
using a comparison sort? Utilize the following information in your solution: 14! ~
8.72x1010, log28.72 ~ 3.12, and log210 ~ 3.32. You will only get full credit if you utilize
the information given and arrive at the correct answer.
n! <= 2k, we know this to be true for k comparisons, solve for k.
log2(n!) <= k
Since we know n = 14, we get:
log2(14!) = log2(8.72x1010 ) <= k
log2(8.72x1010 ) = log2(8.72) + log21010 = log2(8.72) + 10*log2(10) <= k
3.12 + 10*3.32 = 3.12 + 33.2 = 36.32 <= k
Minimum number of comparisons is 36.32.
Grading – Only give full credit for correct answer. -1 for each arithmetic error.
2) (10 pts) Determine whether the following statements are true or false. Circle the
correct answer.
a) n2 /5 = ( n2 logn /50)
True
False
b) nlogn = O(200n)
True
False
c) n2 = (n2 logn)
True
False
d) 2n =  (2.1n)
True
False
e) log 2 n = (log 200 n)
True
False
Grading – 2pts each, no partial credit.
3) (5 pts) What is the most likely theta run-time in terms of n, the input size, of an
algorithm that has been measured with the following run-times:
n (input size)
1000
2000
4000
8000
16000
T(n) (measured run-time in ms.)
1
7
32
128
512
θ(n2),
Grading – 5pts for this answer, no partial credit.
4) (4 pts) Put a box around the sequence of numbers corresponding to the MCSP of the
following sequence of numbers.
-8, -2, 7, -5, -2, 5, 0, 11, -3, 10, -8, 5, -4, 7, -1, 5, -4, -5, 3, -6
Grading – 4pts for this answer, no partial credit.
5) (6 pts) Consider implementing a hash table of size 13 that stores integers with the hash
function f(x) = (2x + 5) mod 13 using the quadratic probing strategy. Place the following
values in the hash table in the following order: 3, 6, 7, 19, and 32.
index 0
1
2
3
4
5
6
7
8
9
10
11
12
value
6
19
7
32
3
Grading – 6pts for this answer, -2pts for each wrong answer.
6) For the following AVL tree operations, show the final AVL tree obtained and all
intermediate steps.
(a) (5 pts) Determine the AVL tree obtained when the following keys are inserted in the
order given: 1,3,2,5,4.
1
\
3
/
2
/\
1 3
\
5
/
2
2
/\
1 4
/\
3 5
4
Grading – 5 pts for this answer. If final answer is wrong, give 2 pts for having 1-2-3
correct. And 1 more point for having inserted 3-5-4 in correct order.
6(b) (5 pts) Delete 12 from the following AVL tree and redraw the tree if it needs
rebalancing. Use the inorder predecessor method (replace the deleted node with the
largest in its left subtree).
10
/
8
6
/ \
9
\
7
10
\
12
/ \
11 14
/
13
/
8
/ \
6
9
\
7
10
\
11
\
14
/
13
/
8
/ \
6
9
\
7
\
13
/ \
11 14
Grading – 5pts for this final answer, give 2pts partial credit if they have the second
step.
7)(a) (6 pts) Draw the result of inserting 4 into the 2-4 tree shown below. Put a box
around your final answer and show all steps.
10, 20, 30
/
/
\ \
2, 5, 7 11, 14 27 32, 40
10, 20, 30
/
/
\ \
2, 4, 5, 7 11, 14 27 32, 40
5, 10, 20, 30
/ | /
\
\
2, 4, 7 11, 14 27 32, 40
20
/
5, 10
/ | \
2, 4, 7 11, 14
\
30
/
\
27 32, 40
Grading – 6pts for this final answer, give 2pts partial credit each for the 2 nd and 3rd
steps.
7)(b) (5 points) Show the intermediate steps and final result of deleteMin from the binary
heap below.
5
/
\
10
25
/ \
/ \
20 15 30 35
35
/
\
10
25
/ \
/
20 15 30
10
/
\
35
25
/ \
/
20 15 30
10
/
\
15
25
/ \
/
20 35 30
Grading – 5pts for this final answer, give 2pts partial credit for replacing with 35.
8) (5 pts) Draw the Disjoint Set that corresponds to the array representation shown
below:
index
value
1
1
2
1
3
6
4
8
5
8
6
1
7
8
1
8
/ \
/ | \
2 6
4 5 7
|
3
Grading – 5pts for this final answer, -2pts for each misplaced element.
8
8
9) (5 pts) Show the first steps in using Counting Sort to sort a list of integers in the range
from 0 to 3 inclusive. Given the unsorted array A, show the array C right before you
insert the first value into the output array. Show the first value you would insert into the
output array.
Array A:
index
value
0
1
1
1
2
2
3
3
Array C:
index
value
0
0
1
3
2
5
3
7
Output:
index
value
0
1
2
3
4
0
5
1
6
3
7
2
4
5
2
6
7
Grading – 5pts for this final answer if C and Output are correct. -2pts for wrong
output. -2 pts for each error in C.
10) For the following code trace problems, write a theta bounds in terms of n for the
following pseudocode. Provide proper justification for your answer.
(a) (5 points)
int i,j;
for (i=0,j=0; i<n; i++) {
while (i < n) {
count++;
i++;
while (j < n) {
count++;
j++;
}
}
}
Solution: θ(n), while(j < n) runs n times, while(i < n) runs n times for a total of 2n
times.
Grading – 5pts for this final answer. -2pts if missing explanation.
(b) (5 points)
while (n > 0) {
for (int i=1; i<=n; i++)
count++;
n = n / 3;
}
Solution: θ(n),
S = N*(1/3)0 + N*(1/3)1 + N*(1/3)2 + N*(1/3)3 + …
An approximation of this finite summation is the infinite geometric sum
S ≈ N*(1/3)0 + N*(1/3)1 + N*(1/3)2 + N*(1/3)3 + … + N*(1/3)∞
Using the formula for a geometric sum we calculate S ≈ N * a1 / (1 – r) = N / (1 – (1/3) )
= 3N/2
Thus the code segment takes O(N) time.
Grading – 5pts for this final answer. -2pts if missing explanation.
(c) (5 points)
for (i = 1; i <= n*n; i++)
for (j = 1; j <= 5 * n; j++)
count++;
for (i = 1; i <= 3 * n; i++)
count++;
Solution: θ(n3), count++ gets called 5n3 + 3n times.
Grading – 5pts for this final answer. -2pts if missing explanation.
11) (20 points) Suppose you have 2 text files listing a series of unsorted integers, and you
want to output only the integers the two files have in common into a new file.
a) Describe an efficient algorithm to solve this problem using a Binary Heap. Give the
run time of this algorithm along with proper justification.
Create a heap for each file, this will take O(n lg n) for each file.
Then compare the minimums of each heap using peekMin(), if they are equal
deleteMin() of both trees and output this value to each file. If they are not equal,
deleteMin() of the smaller value. This will take O(n) time.
Continue until each heap is empty at this point you are finished.
An upper bound for this algorithm is therefore O(n lg n).
(An alternate solution would be put each list in a separate heap, then to heapsort each list
separately by using deleteMin() and outputting the min to an array until the heap is
empty. O(n log n). Then once this is done use a counter in each sorted array to compare
values as we did with the sorted list matching problem, O(n). Then the total run time is
O(n lg n).
Grading – 10pts for this answer. 3 points for putting each file in a separate heap, 3
points for comparing the minimums, 4 points for analysis – 2 pts for analysis of
makeHeap() and 2pts for analysis of output.
b) Describe an efficient algorithm to solve this problem using a Hash Table. Give the
run time of this algorithm along with proper justification.
Use a Hash Table with separate chaining. Hash each integer in the first file, O(n).
Hash each integer in the second file, O(n), if there is a collision, and the length of the
LinkedList at that index is >= 1, you know there is a duplicate, output it to the file.
An upper bound for this algorithm is O(n).
Grading – 10pts for this answer. 5 points for hashing each integer and keeping
track of duplicates. 5 points for correct analysis of hashing and output.
12)(4pts) What’s your favorite sorting algorithm and why? (There’s no wrong answer
for this question, except a blank answer!)
Grading – 4pts for any answer.
Scratch Page – Please clearly mark any work on this page you would like graded.
Download