You are on page 1of 1

Department of Computer Science and Engineering

End-Semester Examination
Course No: CS 213
Date: 16/11/2015

Course Name: Data Structures and Algorithms


Time: 17-30 to 20-30
Marks: 50

DO NOT ASK FOR ANY CLARIFICATIONS. WRITE PRECISELY AND BRIEFLY ARGUE
THE CORRECTNESS OF YOUR ALGORITHMS.
Q1 Let a0 , a1 , . . . , an1 be a sequence of n integers.
(a) Describe an O(n) time algorithm to find indices i, j, k such that 0 i < j < k < n
and ai < ak < aj , if they exist.
(6)
(b) Describe an O(n log n) time algorithm to find indices i, j, k, l such that 0 i < j < k < l < n
and ai < ak < aj < al , if they exist.
(8)
Q2 Let l(S) denote the length of the longest strictly increasing subsequence of a sequence of
integers S. Let S 1 = S and let S k = S k1 .S denote the sequence obtained by concatenating k
copies of S, for k 1. Let l (S) = limk l(S k ).
(a) Given a sequence of n integers S, describe an O(n log n) time algorithm to find l (S).

(6)

(b) Describe an O(n log n) time algorithm to find the smallest k such that l(S k ) = l (S).

(8)

Q3 Let T be an arbitrary rooted tree with n nodes numbered 0 to n 1. You are given a data
structure that answers queries of the form lca(i, j) in O(1) time, where lca(i, j) denotes the least
common ancestor of nodes i and j. No other information about the tree T is available. Show
how you can find the root node of T in O(n) time, using the given data structure.
(6)
Q4 A node s in a directed graph is said to be a source if there exists a path in the graph from s
to all other nodes. Given a directed graph represented by adjacency lists, describe an O(n + m)
time algorithm to find all sources in the graph.
(6)
Q5 Consider a directed graph whose nodes represent tasks to be executed. The ith task has
execution time ti . The edges in the directed graph represent dependence of one task on another.
There are two types of tasks, and and or. An and type task can start execution only after
all its successors have completed execution. An or type task can start execution as soon as
any one of its successors has been completed. A task without successors can start execution at
any time. Note that the graph may contain cycles. Describe an O((n + m) log n) time algorithm
to determine whether it is possible to complete all tasks, and if so, the minimum time required
for it, assuming that any number of tasks can be executed in parallel.
(10)

You might also like