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

CSE 331 Mid-Term I Solutions Guide

Uploaded by

zyl178377
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)
67 views3 pages

CSE 331 Mid-Term I Solutions Guide

Uploaded by

zyl178377
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

S OLUTIONS TO CSE 331 S AMPLE M ID - TERM -I

Please do not read anything into the kind of problems in the sample mid-term. Overall, the
mid-term will be harder than this sample mid-term (but still easier than the homeworks). The
main purpose of this sample mid-term was to give you an idea of the format of questions. Also
you can get an idea of how much detail is expected from your answers in the exam from the
solutions below.

1. (8 × 5 = 40 points) Each of the questions below have two parts. For the first part, you need
to give a justification for the statement and is worth 2 points. For the second part, answer
True or False and briefly JUSTIFY your answer. A correct answer with no or totally incor-
rect justification will get you 1 out of the total 4 points. An incorrect answer irrespective
of the justification will get you 0 out of 4 points. (Recall that a statement is true only if it
is logically true in all cases while it is is false if it is not true in some case).

(a) Consider an arbitrary instance of the stable marriage problem with n men and n
women.
(Part 1) Argue why the following statement is TRUE.
There are n! = n × (n − 1) × . . . × 1 many possible perfect matchings.
Solution. See Problem 1 in HW0.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
There are at most n! stable matchings for the instance.
True. Since every stable matching is a perfect matching (but not necessarily the
other way around), this part follows from part 1.
10 10
10 10
(b) Let f (n) = log log n and g (n) = 1010 .
(Part 1) Argue why the following statement is TRUE.
f (n) is Ω(g (n)).
Solution. f (n) grows with n while g (n) does not increase with n at all.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
f (n) is O(g (n)).
False. g (n) is a constant (albeit a big one) and does not increase with n. However,
f (n) does increase with n (though slowly). Thus, for some large enough n, g (n) ≤
f (n) and thus, g (n) is O( f (n)) (and not the other way round).
(c) Let f (n) = n n and g (n) = 2400n .
(Part 1) Argue why the following statement is TRUE.

f (n) = 2n·log2 n .
¢n
Solution. This follows since n = 2log2 n , which implies that n n = 2log2 n = 2n·log2 n .
¡

1
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
f (n) is Ω(g (n)).
True. From part 1, f (n) = 2n log2 n and since n log2 n ≥ 400n for n ≥ 2400 , f (n) ≥ g (n)
for every n ≥ 2400 , which by definition implies that f (n) is Ω(g (n)).
(d) Let a 1 , . . . , a n be n integers.
(Part 1) Argue why the following statement is TRUE.
Let a i ∈ {0, 1} for each i ∈ [n]. Then the n numbers can be sorted in O(n) time.
Solution. See T/F #1 justification on piazza.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
p p
Let − n ≤ a i ≤ n for every i ∈ [n]. Then the n numbers can be sorted in O(n) time.
True. Consider the following algorithm (which is a variant of the solution to Q1 on
HW 0). Do a scan through a 1 , . . . , a n and count the number of i ∈ [n] such that a i =
p p
j for every − n ≤ j ≤ n. Let this number be n j . Then finally, output n j j ’s in
p p
increasing order of j = − n, . . . , n. (This part is not needed to give full credit but
here is an additional observation for your benefit: Since this needs to maintain an
p
array of pointers of size O( n) = O(n), this is overall O(n) time.)
(e) Consider the BFS algorithm with its input graph G in adjacency list format.
(Part 1) Argue why the following statement is TRUE.
The input size for BFS is Θ(n + m).
Solution. This is because the adjacency list representation takes Θ(m + n) space (as
we saw in class when we compared the adjacency matrix and adjacency list repre-
sentations).
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
BFS is a linear time algorithm. (Recall that an algorithm is a linear time algorithm if
it runs in time O(N ) on inputs of size N .)
True. Recall that we have shown that BFS runs in time O(m + n) when the graph is
represented in the adjacency list format. Further, from part 1, N = Θ(n + m), which
implies the run time is O(N ).
(f) For any graph, recall that running the BFS algorithm implicitly computes a BFS tree.
(Note: BFS tree is not rooted.)
(Part 1) Argue why the following statement is TRUE.
A BFS tree can be computed (explicitly) in O(m + n) time.
Solution. See the BFS pseudocode in the textbook for the algorithm.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
Every graph has a a unique BFS tree for it.
False. Consider the cycle on four vertices: v 1 , v 2 , v 3 , v 4 such that (v i , v i +1 ) for 1 ≤ i ≤
3 and (v 4 , v 1 ) are the edges. Consider a BFS run that starts from v 1 . Note that v 3 can
be discovered from either v 2 or v 4 and each choice leads to a different BFS tree.

2
(g) Recall that a directed graph is strongly connected if and only if every pair of vertices
have directed paths from one to the other.
(Part 1) Argue why the following statement is TRUE.
A directed graph has at most n 2 edges in it.
Solution. Since there are n 2 pairs of vertices, any directed graph has at most n 2
edges.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
Any directed graph on n vertices with at least n − 1 edges is strongly connected.
False. Consider the following DAG on three vertices A, B,C with directed edges (A, B ), (B,C ), (A,C ).
In this graph C is not connected to A but it has n = 3 edges.
(h) Recall that any graph can be represented in adjacency matrix format.
(Part 1) Argue why the following statement is TRUE.
Adjacency matrix takes Θ(n 2 ) space.
Solution. This was stated in the lecture were we compared adjacency matrix and
adjacency list representation of graphs.
(Part 2) Is the following statement true or false? Also remember to briefly JUSTIFY
your answer.
There is an O(n 2 ) time algorithm that for any graph on n vertices given in its adja-
cency matrix, converts it into its adjacency list representations.
True. In short here is the algorithm: go through the matrix row by row and for the
vertex u corresponding to the current row, add a list of vertices w such that the entry
for (u, w) is a 1. Each row takes O(n) time and there are n rows, which makes for a
total running time of O(n 2 ).

You might also like