Name:
Entry No.: 2023 MB1235
INDIAN INSTITUTE OF
CS201-DataTECHNOLOGY
Structures ROPAR.
First Semester of Acadermic Year
2024-
Mid Semester Examination 2025
Instructor: Anil Shukla
Duration: 2 Hours Max. Marks: 25
Date: September 21, 2024
Note: Without proper
further clarifications will be explanation, marks will not be awarded. No
provided during the exam time.
1. Answer the
following with proper explanation: (1 + 2 +3 marks)
(la) What is the running time of
pletely sorted? What about reverseheapsort when the input is com
sorted input?
(1b) You are given a sorted array of n
larly shifted. For example, {35,42,elements which has been circu
5, 12, 23,26} is a sorted array
that has been circularly shifted by 2
positions. Give an
time algorithm to find the largest element in a circularlyO(log n)
shifted
array. T'he number of positions through which it
is unknown to you. has been shifted
(1c) Given an array A|0..n-1] of size
Consider it as an input stream andcontaining n distinct integers.
solve the median problem.
That is, assume that we have n
we only see A|O].
iterations. In the first iteration
Similarly, in the " iteration, we see integers
A[o],..., A<i - 1. After each iteration, output the median of
integers seen so far. Your algorithm should the
run iu time O(n log n).
Note: The median-of-median approach will take
(n²) time.
2. An array A|1..n] is said to have a
its entries are the same. Given anmajority element if more than half of
(i.e., O(n)) time algorithm to tell array, the task is to design a linear
elenent., and if so, find that element. whether the array has a majority
The clements of the array are not
necessarily from some ordered domain like
can be no comparisons of the form "is Ai >the integers, and so there
answer questions of the form: "is Ai] =A?" Aj]?". However you can
marks) in constant time. (3
1
3. Let S(u, v) denotes the minim1um number of cdges in any path from
vertex uto vertex v. Adiameter of a tree T' = (V, E) is defined as
maxy,rev o(u, v), that is, the largest of allshortest-path distances in
the troe. Give an O(|V +|E) algorithm to compute the diameter of a
tree, and also prove the correctness of your algorithm. (2 +2 marks)
Note: An undirected connccted graph without cycles is called a tree.
4. A directed acyclic graph is a directed graph with no directed cycles.
Answer the following: (4 + 4marks)
(4a) Given a directed acyclic graph G and two vertices s and t, give
an O(V|+ |E|)time algorithm to determine the number of paths
from stot in G. (Note that you need not list the paths).
(4b) A Hamiltonian path in a graph is a simple path that passes
through each vertex exactly once. Give an O(|V|+|E) time algo
rithm todecide whether there is a Hamiltonian path in a directed
acyclic graph.
5. Recall that the median-of-median algorithm on distinct input elements
runs in linear time when divided the inputs into groups of size 5 each.
Analyze the algorithm if we divide the elements inio groups of size
s>3 each. Clearly write (with explanation) and solve the recurrence
reiation for any s >3. For what values of s>3 the algorithm works
in lincar time? (4 marks)