Cs502 Collection of Old Papers
Cs502 Collection of Old Papers
Cs502 Collection of Old Papers
com
Instructions
Please read the following instructions carefully before attempting any of the
questions:
(a) Kruskals algorithm for minimum weight spanning trees is an example of dynamic
programming algorithm.
(b) An arbitrary graph with G (V, E), with |E| = |V|-1 edges is a tree.
(c) If problem A reduces (is polynomial-time reducible) to problem B and B is NP-
complete then A is NP-complete.
1 True
2 False
3 Unknown
1 O(n)
3
2 O(n )
2
3 O(n log n)
2
4 O(n )
Given a list of N integers, the 3-sum problem is to determine whether there exists 3 integers
in the list (not necessarily distinct) such that x + y + z = 0. Suppose that you are executing the
brute force algorithm below on a computer that executes 1 billion operations (addition,
increment, or comparison) per second.
(a) Estimate how many seconds it will take (in the worst case) to solve a problem of size N =
1,000? Full credit if you are within 1% of the exact answer.
(b) Of size N = 10,000?
1 O(logn)
2 O(n)
3 O(nlogn)
2
4 O(n )
5 O(2n)
Where (i-j, w) means that the edge that connects node i to node j has weight w. give the
parent-link representation of the shortest-paths tree from node 0 that is computed by
Dijkstra's algorithm for the graph using the standard adjacency-lists representation.
Kruskals algorithm (choose best non-cycle edge) is better than Prim's (choose best tree
edge) when the graph has relatively few edges.
1 True
2 False
3 Unknown
Page 1 of 2
www.vujannat.ning.com
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 i x ( n +1) − 1
n
2n3 + 3n 2 + n
∑ i=
i =1 2 ∑ i =
2 ∑ x =
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.
www.vujannat.ning.com
o True
o False
Question No. 2 Marks : 05
Can an adjacency matrix for a directed graph ever not be square in shape?
o Yes
o No
Question No. 3 Marks : 05
You are given the task of laying down new railway lines which will connect all n cities.
Thus for any pair of cities, you will end up with track connecting them. Note that two
routes may share the same track; track laid between Lahore and Islamabad can be used to
travel in both directions. your goal is to use the minimum amount of track. How would
you achieve the goal now? (Note : consider the scenario carefully and name only the best
suited algorithm)
o 1 Dijkstra's algorithm
o 2 Prims algorithm
o 3 Floyd Warshall algorithm
o 4 Bellman Ford algorithm.
Solve the following recurrence using iteration method ( show intermediate steps )[10 pts]
T(n) = n + 2T(n/2)
T(1) = 1
>:
The algorithm based on this recurrence takes as inputs the maximum weight W, the
number of items n, and the two sequences v = hv1,v2, . . . ,vni and w = hw1,w2, . . . ,wni.
It stores the c[i, j] values in a table c[0..n,0..W]. At the end of the computation, c[n,W]
contains the maximum value the thief can take.
In the following example the inputs are n = 6,W = 8, with values vi and weights wi:
The last part of the algorithm uses this table to determine which items the thief should
take to achieve the maximum value, 22.
(a) Describe this last part of the algorithm: how, in general, it determines the items to be
taken.(Note : in maximum three lines.)
(b) For the above example, list the items to take (i.e., list their indices).
[25 pts]
Question No. 8 Marks : 15
Run DFS sweep and topological sort on the directed graph defined by the following
adjacency matrix.
[15 pts]
www.vujannat.ning.com
Connecting VU Students
CS502-Fundamentals of Algorithms
Final Term Examination – spring 2006
Time Allowed: 150 Minutes
Compute all-pairs shortest paths using Floyd-Warshall algorithm on the following graph
Use Prim’s Algorithm starting with vertex “V1”to find the minimal spanning tree for the graph
If a graph has v vertices and e edges. Then to obtain a spanning tree we have to delete
• v edges
• v – e + 5 edges
• v + e edges
• None of these
Question No. 5 Marks : 1
• Only II
• Only IV
• Both II and IV
• None of these
Question No. 6 Marks : 1
• Yes
• No
• Unknown
You are managing 300 million dollars. From this money you can sponsor 4 projects P1, P2, P3,
and P4 with total cost no more than 300 million dollars. The project P1 has cost 60 million and
generates a profit 20 millions ,the, project P2 has cost 240 million and generates a profit 500
millions, the project P3 has cost 180 million and generates a profit 300 millions ,the project P4
has cost 120 million and generates a profit 210 millions
Find out which projects to sponsor so that you get the largest profit.
Hint: This is the classic 0-1 knapsack problem
Question No. 8 Marks : 1
1.
2.
3.
4.
5.
• Only I
• Only III
• Both I and III
• All of these
In order to move a tower of 4 rings from one peg to another, how many ring moves are required
• 15
• 7
• 12
• None of these
Which sequence is not a valid topological order of the directed graph shown below?
• ABDCEF
• ADBCEF
• ABCDFE
• ADBCFE
• ABDFEC
Suppose we have two problems A and B .Problem A is polynomial-time reducible and problem
B is NP-complete. If we reduce problem A into B then problem A becomes NP-complete
• Yes
• No
Question No. 13 Marks : 1
The total degrees of the following graph G
are
• 4
• 5
• 7
• 8
Question No. 14 Marks : 1
• Only II
• Only IV
• Both II and IV
• Both I and III
• Only (a)
• Only (c)
• Both (a) and (e)
• Both (c) and (e)
• (a) ,(c),(e)
• 10 00 010
• 011 00 010
• 10 00 110
• None of these
Which of the shortest path algorithms would be most appropriate for finding paths in the graph
with negative edge weights and cycles?
I.Dijkstra’s Algorithm
II. Bellman-Ford Algorithm
III. Floyd Warshall Algorithm
• Only II
• Only III
• Both II and III
Question No. 19 Marks : 1
Which of the following orders is not a possible order in which Depth First Search can visit
the vertices of the directed graph shown below?
• ABCEFD
• ACEBFD
• ADFEBC
• ADFBCE
• ABFECD
WWW.vujannat.ning.com
http://vujannat.ning.com
Largest Online Community of VU Students
FINALTERM EXAMINATION
SPRING 2007 Marks: 50
CS502 - FUNDAMENTALS OF ALGORITHMS (Session Time: 150min
-5)
StudentID/LoginID: ______________________________
INSTRUCTIONS
Please read the following instructions carefully before attempting
any of the questions:
1. Attempt all questions. Marks are written adjacent to each question
2. Paste the bitmap image for the tables, diagrams etc while solving your
questions
3. This examination is closed book, closed notes, closed neighbors.
4. 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.
5. Some of the examination consists of multiple-choice questions. Choose only one
choice as your answer.
6. Write all steps, missing steps may lead to deduction of marks
7. Use of cell phone during the examination is strictly prohibited, otherwise strict
disciplinary action will be taken as per university rules
► 15
► 7
► 63
► 32
► None of these
⎛ ⎞
Is 22n = O⎜2 n ⎟ ?
⎝ ⎠
► Yes it is possible
► No it is not possible
► None of these
If, in a DFS forest of digraph G = (V, E), f[u] ≤ f[v] for an edge (u, v) Є E then the edge is called
► Back edge
► Forward edge
► Cross Edge
► Tree Edge
► None of these
► None of these.
Complete the following instance of the optimal matrix multiplication ordering problem.
Also give the optimal multiplication order.
1 K 2 K 3 K 4 K 5 k
1 0 0 60 1 120 2 195 2 ? ?
2 0 0 48 2 120 2 222 2
3 0 0 60 3 150 4
4 0 0 120 4
5 0 0
For the following given comparison, what are the tradeoffs between adjacency lists and
adjacency matrices?
Comparison Winner
Faster to test if (x, y) exists. ?
Faster to find vertex degree? ?
Less memory on small graphs? ?
Less memory on big graphs? ?
Edge insertion or deletion? ?
Faster to traverse the graph? ?
Better for most problems? ?
Apply Bellman-Ford Algorithm on the following graph. Show values after each iteration
in a tabular form. S is the starting node.
Name
PVC Name/Code
Date
Please read the following instructions carefully before attempting any of the
questions:
1. Attempt all questions. All Questions carry Equal Marks.
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.
Indicate whether true or false. Beware of guessing: correct answer +5pts, no answer 0pts
(a) In Prim’s algorithm, the additional information maintained by the algorithm is the length of
the shortest edge from vertex v to points already in the tree.
A) TRUE
B) FALSE
C) UNKNOWN
(b) Although it requires more complicated data structures, Prim's algorithm for a
minimum spanning tree is better than Kruskal's when the graph has a large number of
vertices.
A) TRUE.
B) FALSE
C: UNKNOWN
T(n) =
Consider the following 21 character message that consists of 3 a’s, 7 c’s, 6 t’s, and 5 g’s:
a a c c c c a c t t g g g t t t t c c g g
Are the following 43 bits a possible Huffman encoding of the message above?
0000001111000101010010010010101010111001001
In the following graph, edges are labeled with upper case letters. Edge weights are given as numbers next
to edges.
Recall that Dijkstra’s algorithm finds the shortest path from v1 to all other vertices by adding edges that
make shortest paths. For the graph shown above, each edge is bidirectional; that is, you can travel on
either direction on it for the same cost. List the edges in the order chosen by Dijkstra’s algorithm.
www.vujannat.ning.com
Instructions
Please read the following instructions carefully before attempting any of the
questions:
9
A PC like computer can perform 10 operations per second. Consider that we have five
different algorithms for a specific problem. For each algorithm i, we know the number of
operations Ti(n) it will perform on a problem of size n:
T1(n) = 6000000 ·n
T2(n) = 60000 ·nlogn
2
T3(n) = 0.003 ·n
−6 3
T4(n) = 10 ·n
−18 n
T5(n) = 10 ·2
For each algorithm compute the size nmax of the largest problem the respective algorithm
can solve within 1 second, 1 minute and 1 hour. Enter the maximal problem sizes into the
following
6
Describe an efficient algorithm to find the median of a set of 10 integers; it is known that
there are fewer than 100 distinct integers in the set.
1: for i = 1 to k
2: C[i] = 0
3: for i = 1 to n
4: C[A[i]] = C[A[i]] + 1
5: for i = 2 to k
6: C[i] = C[i] + C[i-1]
7: for i = n downto 1
8: B[C[A[i]]] = A[i]
9: C[A[i]] = C[A[i]] - 1
(a) For the input array [6,3,7,5,4,3,3,2,8,9,1,6], and k = 9, specify the contents of
the arrays B and C
• (4 points) after the second for-loop (line 3); C =
• (2 points) after the third for-loop (line 5); C =
• After each of the first three iterations of the fourth loop (line 7);
1. (3 points)
B=
C=
2. (3 points)
B=
C=
3. (3 points)
B=
C=
(b) (5 points) Suppose that the for-loop on line 7 above, is modified as follows:
7: for i = 1 to n
Will the algorithm still sort the input? If no, explain why not. If yes, what is the effect of this
Modification to the algorithm and the output?
When using merge sort to sort an array, do the recursive calls to merge sort depend on the
number of elements in the array, the values of the elements or both? Briefly explain.
Insertion sort can be expressed as a recursive procedure as follows: In order to sort array
A[1..n], we recursively sort array A[1..n-1] and then insert A[n] into the sorted array A[1..n-1].
Give an equation that describes the overall running time of this algorithm on an input array of
size n, in terms of the running time on smaller input.
T(n) =
Compute the edit distance and edit scripts for the strings "HEAP" and "CHEAT". Recall that
the edit distance recurrence is
⎛ E (i − 1, j ) + 1 ⎞
⎜ ⎟
⎜ E (i, j − 1) + 1 ⎟
E(i, j) = min
⎜ E (i − 1, j − 1) + 1 ifA[i ] ≠ B[ j ] ⎟
⎜ ⎟
⎝ E (i − 1, j − 1) ifA[i ] = B[ j ] ⎠
Using big-oh notation, give the running time of the following piece of code. Briefly
justify your answer.
CS502-ALGORITHMS
MID –FALL2007
Q#2
Solve the recurrence using iteration method and also find time complexity (Θ notation)
Q#3
Suggest the criteria for measuring algorithms. Also discuss the issues need to be
discussed in the algorithm design.
Q#4
If an algorithm has a complexity of log 2 n + nlog 2 n + n. we could say that it has
complexity
O(n)
O( n log2 n)
O(3)
O( log2 ( log2 n ))
O ( log2 n)
Q#5
Let the set P = {(1, 13), (2, 9), (3, 15), (4, 12), (5, 14), (6, 6), (7, 3), (8, 10), (9, 2), (10, 8),
(11, 9), (13, 6), (15, 3), (18, 5)}. You are required to give the final state of stack after the
execution of sweep line algorithm for 2d-maxima. No intermediate steps or graphics to be
shown.
Q#6
Suppose we have hardware capable of executing 106 instructions per second. How long
would it take to execute an algorithm whose complexity function is T (n) = 2n2 on an
input of size n = 108?
Q#7
In RAM model instructions are executed
One after another
Parallel
Concurrent
Random
Q#8
In selection algorithm, because we eliminate a constant fraction of the array with each
phase, we get the
Link list
Structure
Array
None of above
Page 1 of 3
www.vujannat.ning.com
Instructions
Please read the following instructions carefully before attempting any of the
questions:
Develop an O(nlogn)-time algorithm that solves the following problem: Given an unsorted
array of n real numbers and a real number x, decide whether there are two numbers y and
z in this array such that x = y+z. The output of your algorithm should be "No" if no such
numbers y and z exist; otherwise, it should report y and z. (Note that there may be more
than one such pair of numbers. It is sufficient to report one of them.) Argue briefly why
your algorithm takes O(nlogn) time.
Compute the edit distance and edit scripts for the strings "THROUGH" and
"PLOUGH". Recall that the edit distance recurrence is
E(i-1, j)+1
E(i, j-1)+1
E(i, j) = min E(i-1, j-1)+1 if A[i] ≠ B[ j]
E(i-1, j-1) if A[i] = B[ j]
Solve the recurrence using Iteration method and also find time complexity (Theta
notation)
o O(n)
o O(n3)
o O(n2 logn)
o O(n2)
o O(nlogn)
www.vujannat.ning.com
The following function calculate kn , for integer k and n. Determine closed expression T(n) for
running time. Assuming that arithmetic operations take unit time.
function exp1(k, n)
{
power := 1;
for i = 1 to n do{
newpower := 0;
for j := 1 to k do{
newpower := newpower + power;
}
power := newpower
}
return(power)
}
T(n) = ?
Only I
Only II
Both I and III
None of the above
int Idx;
for (Idx = 0; Idx < N; Idx++)
{
cout << A[Idx] << endl;
}
O(N)
O(N2)
O(log N)
O(N log N)
If algorithm A has running time 7n2 + 2n + 3 and algorithm B has running time 2n2, then
Only i
Only ii
Both i and ii
Both iii and iv
Determine the complexity of an algorithm that measures the number of print statements in an
algorithm that takes a positive integer n and prints 1 one time, 2 two times, 3 three times , ... ,
n n times.
That is
2 2
3 3 3
……………
……………
n n n n ……..n (n times)
Note:There is no need to write Algorithm(Pseudo code)
Compute the edit distance and edit scripts for the strings “PROPERTY” and “POCKET”.
Then T(2) is
36
72
12
None of the above
The appropriate big θ classification of the given function. f(n) = 4n2 + 97n + 1000 is
θ(n)
θ(2n)
θ(n2)
θ(n2 log n)
Question No. 11 Marks : 1
Assume that a given algorithm has a runtime C that depends on the size N of its input
according to the following two formulas:
⎧0 if N = 1
C(N ) = ⎨
⎩C ( N − 1) + 2 if N > 2
Which of the following functions C(N) describes the runtime of the algorithm?
C(N) = N – 1
C(N) = (N - 1)2
C(N) = log2 N
C(N) = 2(N - 1)
Let us say we have an algorithm that carries out N2 operations for an input of size N. Let us
say that a computer takes 1 microsecond (1/1000000 second) to carry out one operation.
How long does the algorithm run for an input of size 3000?
90 seconds
9 seconds
0.9 seconds
0.09 seconds
O(kn)
O(nk)
O(nk+1)
None of the above
www.vujannat.ning.com
n.
n log n .
log n .
n2.
⎧n +1 if m= 0
⎪
a (m, n) = ⎨a (m-1, 1) if m ≠ 0, n= 0
⎪a (m-1, a(m, n-1)) if m ≠ 0, n ≠ 0
⎩
Write a single recursive function in C++ that computes Ackerman’s function. Do not use
other functions or loops. Assume parameters m and n passed are always greater or equal to
zero
II f(x) = 22 g(x) = 2x
III f(x) = 2x + 3 g(x) = 2x + 7
IV f(x) = log(x2 + 1) g(x) = log( x)
Only I
Only III
Both I and II
Both III and IV
True
False
At most four
At least four
Exact four
Then T(5) is
25
75
79
None of the above
sort( A[1..n] )
{
for i = 2 to n do
for j = n downto i do
if( A[j-1] > A[j] )
swap(A[j-1], A[j])
}
O(N)
O(N2)
O(log N)
O(N log N)
True
False
Question No. 12 Marks : 1
Then T(8) is
61
29
13
None of the above
(i),(ii),(iii)
(ii),(iii),(i)
(iii),(ii),(i)
(iii),(i),(ii)
Let X[1..n] and Y[1..n] be two arrays, each containing n numbers already in sorted order.
Give an O(logn)-time algorithm to find the median of all 2n elements in arrays X and Y.