Name: ___________________ Roll #: ___________________ Section: _____________
National University of Computer and Emerging Sciences, Lahore Campus
Course: Design and Analysis of Algorithms Course Code: CS302
Program: BS(Computer Science) Semester: Fall 2020
Duration: 180 Minutes Total Marks: 55
Paper Date: 15-Feb-2021 Weight 50%
Section: ALL Page(s): 8
Exam: Final Exam
Instruction/Notes: Attempt the examination on the question paper and write concise answers. You can use
extra sheet for rough work. Do not attach extra sheets used for rough with the question
paper. Don’t fill the table titled Questions/Marks.
Question 1-2 3-7 8-10 11 12 Total
Marks / 12 /11 /12 /8 /12 /55
Q1) [4 Marks] Assume that in the partition method of quick sort you are guaranteed to find a pivot that
splits the data into one portion of at most 60% data and the other of at least 40%. Using this partition,
what will be the recurrence for T(n) describing the worst case running time of quick sort? Based on this
recurrence, what will be the worst case running time in terms of big-Oh.
Q2) [2+4+2 = 8 Marks] Find the tightest upper bound on the running time of the following algorithms.
Write your answer in big O notation.
(a) int sum,i,j,n;
sum = 0;
cin>>n;
for (i=1;i<n;i=i++)
for (j=1;j<n;j=j*2)
sum ++
Department of Computer Science Page1
Name: ___________________ Roll #: ___________________ Section: _____________
(b) int function(int n)
{
If(n == 0) return 1;
return function(n/2) + function(n/2);
}
(c) int sum,i,j,n;
sum = 0;
cin>>n;
for (i=1;i<n;i= i+2)
for (j=1;j<n;j=j+5)
sum ++
Q3) [3 Marks] We saw that the fastest comparison based sorting algorithms can sort data in Ω(nlgn).
Based on this fact, which among the following statements are true:
(i) Data belonging to the range of [-1000, 1000] can only be sorted in Ω(nlgn) because it contains
negative numbers.
(ii) If we cannot impose any range on the data, then the fastest worst-case running time to remove
duplicates from an array of n unsorted elements is Ω(nlgn).
(iii) The fastest way to sort a DNA sequence file, where a sequence is made up of repetitions of the
characters A, B, C and D, is Ω(nlgn), for a sequence of length n.
Q4) [2 Marks] An undirected graph G has n nodes. Its adjacency matrix is given by an n × n square
matrix whose (i) diagonal elements are 0‘s and (ii) non-diagonal elements are 1‘s. which one of the
following is TRUE?
i. Graph G has no minimum spanning tree (MST)
ii. Graph G has a unique MST of cost n-1
iii. Graph G has multiple distinct MSTs, each of cost n-1
iv. Graph G has multiple spanning trees of different costs
Q5) [2 Marks] Topological sort can be applied to which of the following graphs?
i. Undirected Cyclic Graphs
ii. Directed Cyclic Graphs
iii. Undirected Acyclic Graphs
iv. Directed Acyclic Graphs
Department of Computer Science Page2
Name: ___________________ Roll #: ___________________ Section: _____________
Q6) [2 Marks] A man wants to go different places in the world. He has listed them down all. But there
are some places where he wants to visit before some other places. What application of graph can he use to
determine that?
i. Depth First Search
ii. Breadth First Search
iii. Topological Sorting
iv. Dijkstra’s Shortest path algorithm
Q7) [2 Marks] True or False
(i) Prim’s and Kruskal’s algorithm can yield different minimum spanning trees.
(ii) Prim’s and Kruskal’s algorithm produce correct minimum spanning trees if there are
negative edge weights
Q8) [2 Marks] Let G be connected undirected graph of 100 vertices and 300 edges. The weight of a
minimum spanning tree of G is 500. What is the weight of a minimum spanning tree when the weight of
each edge of G is increased by five?
Q9) [4 Marks] Professor Henry suggests the following algorithm for finding the shortest path from node
s to node t in a directed graph with some negative edges: add a large constant to each edge weight so that
all the weights become positive, then run Dijkstra’s algorithm starting at node s, and return the shortest path
found to node t. Is this a valid method? Either prove that it works correctly, or give a counter example.
Department of Computer Science Page3
Name: ___________________ Roll #: ___________________ Section: _____________
Q10) [6 Marks] Consider two strings: X = "cacad", Y = "cdcbd".
a) Show the longest common subsequence table, L, for strings X and Y.
b) What is a longest common subsequence between these strings?
Department of Computer Science Page4
Name: ___________________ Roll #: ___________________ Section: _____________
Q11) [8 Marks] Agent Bob is living in his houseat A in the enemy territory. There are n other agents in
the area, stationed at hotels h1, h2, …, hn. They all must visit Bob’s house at A for a top secret meeting.
However, the area is constantly monitored and there is a risk of being monitored associated with every
road. You have a map, G=(V, E), of the n hotels h1, h2, …, hn and A. This map shows every road between
any two hotels or between a hotel and A and also shows its associated risk value, which is a positive
weight. The total risk of a path taken by an agent is the sum of the risk values of all the edges in that path.
You need to write an algorithm that produces paths for all agents, such that the total risk for each agent is
minimized. Your algorithm should work in O((|V|+|E|)lg|V|)
(a) Briefly explain the algorithm.
(b) Write pseudocode of the algorithm
Department of Computer Science Page5
Name: ___________________ Roll #: ___________________ Section: _____________
(c) Write time complexity of your algorithm.
Q12) [4 + 8 Marks] An equilibrium index of an array is such a index i, such that the sum of the elements
A[k] for k<i is equal to the sum of elements A[j] for j>i. For example, in the following array, index 4 is an
equilibrium index, as the sum of all the elements before A[4] is 1 and the sum of all the elements after
A[4] is also 1. (Assuming array indices start from 0).
[2, 4, -7, 2, 6, 8, -6, 3, -4, 0]
(a) Following is a proposed Divide and Conquer Algorithm to find the equilibrium index in O(nlgn).
Which of the following statements best describes this algorithm:
(i) The algorithm does not correctly find the equilibrium index.
(ii) The algorithm correctly finds the equilibrium index but not in O(nlgn)
(iii) The algorithm correctly finds the equilibrium index in O(nlgn)
Department of Computer Science Page6
Name: ___________________ Roll #: ___________________ Section: _____________
findEquilibrium(A, left, right)
{
//returns a pair of values: (i, t), i.e. an index and true/false
IF left==right
return (left, false)
mid (left+right) / 2
(i1, t1) findEquilibrium(A, left, mid-1)
(i2, t2) findEquilibrium(A, mid+1, right)
IF(t1==true)
return(i1, t1)
ELSE if(t2==true)
return(i2, t2)
ELSE
{
s10, s20
FOR k left to mid-1
s1 s1+A[k]
FOR jmid+1 to right
s2 s2+A[j]
IF (s1 == s2)
return (mid, true)
ELSE
return (mid, false)
}
}
(b) Design an algorithm that finds the equilibrium index of an array (or reports that none exists) in
O(n).
Department of Computer Science Page7
Name: ___________________ Roll #: ___________________ Section: _____________
Department of Computer Science Page8