0% found this document useful (0 votes)
82 views3 pages

Sample Midterm 1 PDF

This document contains a sample midterm exam for a course on algorithms. The midterm covers topics like asymptotic analysis, recursion trees, graph algorithms, sorting, and matrix chain multiplication. It consists of 7 questions testing understanding of big O notation, solving recurrence relations, designing graph algorithms, analyzing sorting algorithms, and solving problems related to hashing and dynamic programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views3 pages

Sample Midterm 1 PDF

This document contains a sample midterm exam for a course on algorithms. The midterm covers topics like asymptotic analysis, recursion trees, graph algorithms, sorting, and matrix chain multiplication. It consists of 7 questions testing understanding of big O notation, solving recurrence relations, designing graph algorithms, analyzing sorting algorithms, and solving problems related to hashing and dynamic programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CSCI 270: Algorithms Nazarbayev University

Profs. Demirci, Sinop, Murzabulatov Spring 2021

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

For n = 28 , log 28 = 8, 28 = 24 = 16.


...

As you can see, n grows faster than log n. Thus, f (n) = Ω(g(n)).
b) We can simplify the functions as follows.
f (n) = n log 3 and g(n) = n log 2. Observe that log 3 and log 2 are constants, which are ignored
in asymptotic analysis. Thus, f (n) = Θ(g(n)), which implies that f (n) = O(g(n)) and f (n) =
Ω(g(n)).
2. Using the recursion tree method, solve the following recurrence relation T (n) = T (n − 1) + n2 .
Make sure that the bound you obtain is tight.
Solution:
T (n) − − − − − − − − − − − − − − > cn2
|
|
T (n − 1) − − − − − − − − − − − − > c(n − 1)2
|
|
T (n − 2) − − − − − − − − − − − −− > c(n − 2)2
......
|
|
T (1)
T (n) = T (1)+c·22 +c·32 +...+c·n2 = (T (1)−c)+c(12 +22 +32 +...+n2 ) = Θ(1)+c· n(n+1)(2n+1)
6 =
Θ(n3 ).
3. Given an adjacency-list representation of a multigraph G = (V, E) (a multigraph is a graph in
which there could be self loops and more than one edge between the same two vertices) describe
an O(|V | + |E|)-time algorithm to compute the adjacency-list representation of the “equivalent”
undirected graph G0 = (V, E 0 ) where E 0 consists of the edges in E with all multiple edges between
any two vertices replaced by a single edge and with all self-loops removed.

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.

4. Choose the most accurate solution to the recurrence T (n) = 4T (n/3) + n2 :

(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.

0 8 · 3 · 4 = 96 min(8 · 3 · 1 + 12, 96 + 8 · 4 · 1) = 36 36 + 8 · 1 · 20 = 196


x 0 3 · 4 · 1 = 12 min(12 + 3 · 1 · 20, 3 · 4 · 20 + 80) = 72
x x 0 4 · 1 · 20 = 80
x x x 0

The optimal parenthesization is (A1 · (A2 · A3 )) · A4 .

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)

You might also like