cs502_final_term_2
cs502_final_term_2
www.vu786.com
CS502 Fundamentals of Algorithms
Final Term Examination - August 2004
Time Allowed: 150 Minutes
Instructions
Please read the following instructions carefully before attempting any of the
questions:
1. Attempt all questions. Marks of every question are shown adjacent to it.
2. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions, attempt
it to the best of your understanding.
b. If you believe that some essential piece of information is missing, make an
appropriate assumption and use it to solve the problem.
**WARNING: Please note that Virtual University takes serious note of unfair
means. Anyone found involved in cheating will get an `F` grade in this course.
Very Important:
Some results you may need:
n n(n 1) n
2n3 3n 2 n n x( n1) 1
i 2 xi
2 i 1 i x 1
i 1 ,
6 , i 1
It is impossible to design a sorting algorithm based on comparison of keys worst-case run time is in
O(n).
o True
o False
Consider the following problem: You are given a sequence of n integers that contains log2 n different
integers.For example, n = 8, log(8) = 2: {3,5,3,5,3,5,5,5}; the list of 8 numbers is made up of only two
distinct integers 3 and 5. Design an algorithm to sort this sequence using O(nlog logn) element
comparisons (which is better than O(nlogn)). Prove that your algorithm is indeed O(nlog logn)
[Hint: use variation of heap sort.]
In the following graph, edges are labeled with upper case letters. Edge weights are given
as numbers next to edges.
Recall that Kruskal's algorithm greedily adds edges in a way that avoids cycles. For the graph
shown above, list the edges in the order chosen by Kruskal's algorithm.
Greedy algorithms are called "greedy" because they often take a lot of time.
o True
o False
Kruskal's algorithm (choose best non-cycle edge) is better than Prim's (choose best tree edge) when
the graph has relatively few edges.
o True
o False
Quick sort has a good average run time and a poor worst-case run time.
o True
o False
Page 2 of 2
Question No. 7 Marks : 15
In the following algorithm, ". . ." stands for some simple calculations that take constant
time, i.e.,(1).
procedure(n)
for k from 1 to n do
... /* produces a number j */
if k divides j, then mergesort an n-long list
...
end for
...
end
Note: Think of j as a random integer, so the probability that "k divides j" is "1/k".
(a) Suppose the sorting were free (which it is not). What is the complexity class for the
average running time of this algorithm. You MUST give a reason for your answer. (The class
should be of the form( f (n)) where f (n) is a simple function.)
(b) Suppose that the basic operation is a comparison in merge sort. What is the complexity
class for the average running time of this algorithm. (You may give your answer in the form
( f (k)) where f (k) is a simple function and the sum runs from 1 to n.) You MUST give a
reason for your answer.
(c) Use (a) and (b) to find the complexity class for the average running time of this algorithm.
You MUST give a reason for your answer.
o True
o False
Consider the following two problems. In P1 we are given as input a set of n squares (specified by
their corner points), and a number k. The problem is to determine whether there is any point in the
plane that is covered by k or more squares.
In P2 we are given as input an n–vertex graph, and a number k; the problem is to determine whether
there is a set of k mutually adjacent vertices. (E.g. for k = 3 we are just looking for a triangle in the
graph.).
Obviously, the problems are both in NP. There exists a simple translation from P1 to P2: just make a
graph vertex for each square, and add an edge between a pair of vertices if the corresponding two
squares overlap.
o True
o False
(a) Run DFS on this graph and write down the values of d[v] and f [v] for all vertices v. Assume
that in outer for-loop of the DFS, vertices of G are processed in numeric order of their labels.
For example, DFS will choose v1 initially. Also assume that in DFS-Visit, vertices adjacent to
each vertex u are processed in increasing numeric label. For example, if v10, v5, v8 are
adjacent to some vertex, they will be processed in the order v5, v8, v10.
(b) Identify tree edges, back edges, forward edges and cross edges for the DFS.