Sample Midterm 1 PDF
Sample Midterm 1 PDF
Sample Midterm
1. For each of the following functions given in parts (a) and (b), determine whether f (n) =
O(g(n)), f (n) = o(g(n)), f (n) = Θ(g(n)) or f (n) = Ω(g(n)). For some parts, more than one
relation could be true and you are expected to identify all of them.
a) f (n) = n3/2 , g(n) = n log n
b) f (n) = log(3n ), g(n) = log(2n )
√
Solution: a) Note that f (n) = n n and g(n) = n log n
√
Since n appears as a factor in both functions, it is enough to compare n with log n.
Let’s compare the functions
√ for different values of n.
For n = 2 , log 2 = 6, √2 = 23 = 8.
6 6 6
This study source was downloaded by 100000881082175 from CourseHero.com on 02-18-2024 14:02:46 GMT -06:00
https://www.coursehero.com/file/83494782/Sample-Midterm-1pdf/
Solution: We examine all edges in the multigraph using linked list representation and add an
edge to G0 if it has not been added yet. Also if the edge is between a vertex and itself, we’ll not
add it to G0 . Here is the pseudocode.
ALGORIHTM Create array A[1...|V |] and initialize all its elements to 0
for each vertex u ∈ V
for each v ∈ Adj[u]
if (u! = v) && (u! = A[v])
A[v] = u
INSERT(Adj 0 [u], v)
The first condition in the if statement is for removing selfloops and the second is for removing
multiple edges between two vertices.
Running time: Creation of the array A[1...|V |] and its initialization takes O(V ).
Outer for loop takes O(V ).
Inner for loop takes O(degree(u)) for each vertex. Thus, for all vertices, it will be O(E) (since
the sum of all degrees is equal to the number of edges multiplied by 2).
So, the total running time is O(|E| + |V |), as needed.
(a) Θ(n)
(b) Θ(n2 )
(c) O(n3 )
(d) O(n2 )
Solution: In the recurrence relation, a = 4, b = 3, and log3 4 < 2. So using the Master method
we find that this looks like Case 3. We need to check the regularity condition. If we can find a
constant c < 1 s.t. 4(n/3)2 ≤ cn2 , we are done. Note that for c = 4/9 the inequality holds and
thus, we conclude that T (n) = Θ(n2 ). So the correct answer is (b). Observe that (d) is also
correct, but it is not the most accurate bound.
5. Suppose you inserted the keys 2, 4, 7, 8, 12, 13, 19, 20, 25, 28 and 30 into a hash table of size 11
using the hash function h(x) = (2x + 5) mod 11 with chaining. What is the average number of
probes per key?
Solution: The hash value for each key is as follows:
2 4 7 8 12 13 19 20 25 28 30
9 2 8 10 7 9 10 1 0 6 10
Hence the hash table is [{25}, {20}, {4}, {}, {}, {}, {28}, {12}, {7}, {13, 2}, {30, 19, 8}]. Note that
the individual ordering inside the cells do not matter because it does not affect the average
number of probes per key. So the number of probes per each key is: 25 => 1; 20 => 1; 4 => 1;
28 => 1; 12 => 1; 7 => 1; 13 => 1; 2 => 2; 30 => 1; 19 => 2; 8 => 3. The sum is
1+1+1+1+1+1+1+2+1+2+3=15. The average is 15/11 = 1.3636...
This study source was downloaded by 100000881082175 from CourseHero.com on 02-18-2024 14:02:46 GMT -06:00
https://www.coursehero.com/file/83494782/Sample-Midterm-1pdf/
6. Consider matrix chain multiplication with the size array being p = [8, 3, 4, 1, 20] so that the
matrices have dimensions 8×3, 3×4, 4×1 and 1×20 respectively. Build the table corresponding
to the minimum number of multiplications for multiplying these matrices. What is the optimal
parenthesizing?
Solution: Recall that the (i, j)-th entry in the following table is the minimum cost of multiplying
Ai · Ai+1 · . . . · Aj . Also, the entries below the diagonal are not used.
7. Suppose you are given an almost sorted sequence of n numbers, A, such that each number is at
most one place away from where it should be (for example, A = [2, 1, 3, 5, 4]). Which algorithm
will you choose to sort A and what will be its running time in this case?
Solution: The maximum number of inversions in A is n. Therefore, if we use Insertion Sort,
the running time will be O(n + I) = O(n) (where I is the number of inversions), which is the
best possible running time (as sorting n numbers can not be done in less time).
This study source was downloaded by 100000881082175 from CourseHero.com on 02-18-2024 14:02:46 GMT -06:00
https://www.coursehero.com/file/83494782/Sample-Midterm-1pdf/
Powered by TCPDF (www.tcpdf.org)