Major Examination.
COL106, Sem-I, 2024-25
Saturday, November 23rd, 8 am - 10 am
Name:
Entry number: Group:
There are 7 questions and all are compulsory. Maximum marks: 35
Note: You can assume without explanation any algorithm (or result) discussed in class. Each
algorithm must be clearly stated along with justification of correctness and running time. You must not
use examples in correctness proofs.
1. (4 marks) You are given a directed graph 𝐺 = (𝑉, 𝐸). A vertex 𝑣 is called nice if there is a
directed path from 𝑣 to each of the other vertices in 𝐺 (there could be multiple nice vertices in a
graph). Suppose we run DFS on 𝐺 and let 𝑤 be the vertex with the highest finish time (recall
that the finish time of a vertex 𝑢 in DFS is the time at which the procedure DFS(𝑢) ends, i.e., 𝑢
is popped from the stack). Prove that if 𝐺 has a nice vertex, then 𝑤 is a nice vertex.
2. (6 marks) Let 𝐺 = (𝑉, 𝐸) be a directed graph where each edge has a positive length. Given a
vertices 𝑠 and 𝑡, give an 𝑂((𝑚 + 𝑛) log 𝑛) time algorithm to check whether there is a unique
shortest path from 𝑠 to 𝑡. In other words, if 𝑑(𝑠, 𝑡) denotes the shortest distance from 𝑠 to 𝑡, the
algorithm should output TRUE if there is exactly one path from 𝑠 to 𝑡 of length 𝑑(𝑠, 𝑡), otherwise
it should output FALSE. The graph is given in the adjacency list representation. Here 𝑚
denotes the number of edges and 𝑛 denotes the number of vertices in the graph.
3. (4 marks) Suppose we are given 𝑛 numbers in an array where only 𝑡 values are “out of order”,
i.e., if we remove these 𝑡 values, then the rest of the array is sorted in ascending order. Give
an 𝑂(𝑡𝑛) time algorithm to sort the array in ascending order. Note that the algorithm does not
know the identity of these 𝑡 values.
4. (6 marks) You are given two sorted arrays of length 𝑛 and 𝑚 of respectively. Give an algorithm
𝑡ℎ
that takes as input a number 𝑘, and outputs the 𝑘 smallest number in the union of the two
arrays. Running time should be 𝑂(log 𝑛 + log 𝑚). Note that 𝑘 can be any number between 1
and 𝑛, and the proposed algorithm should have the desired time complexity independent of the
value of 𝑘. Specifically, a solution which has a linear time dependence on 𝑘 is not allowed.
5. (5 marks) Consider a binary tree where every internal node has exactly 2 children. For each
node 𝑣, let 𝑤(𝑣) denote the number of external nodes (i.e., leaves) below the subtree rooted at
𝑣. Note that if 𝑣 is an external node, then 𝑤(𝑣)=1. We call such a tree “2-balanced” if for every
internal node 𝑣 with children 𝑣𝐿 and 𝑣𝑅 , the ratio 𝑤(𝑣𝐿)/𝑤(𝑣𝑅) lies in the range [1/2, 2]. Show
that any 2-balanced tree with 𝑛 nodes has height 𝑂(log 𝑛).
6. (5 marks) You are maintaining a skip list on 𝑛 keys in the ascending order. Show how you can
modify this data-structure by adding one more field at each node such that in addition to the
originally supported operations of (i) Search for a key, (ii) Insert a key, (iii) Delete a key,
𝑡ℎ
another operation can be supported which is: given a number 𝑘, return the 𝑘 smallest key.
Each of these operations should take expected 𝑂(log 𝑛) time.
7. (a) (2 marks) Recall that in the 2-dimensional range tree data-structure on 𝑛 points, each
internal node in the range tree stores an associated tree (which is a balanced tree that uses 𝑦
coordinates of the points below the node). Now suppose that only the nodes with depth 0, 2, 4,
6, ... have an associated tree structure – the rest of the nodes do not store any associated tree
(the root is at depth 0). A range query is specified by a rectangle and the output is supposed to
be the number of input points inside the rectangle. Show how to answer a range query with
this modified range tree data-structure.
(b) (3 marks) Now suppose only the nodes at depth 0, 𝑗, 2𝑗, 3𝑗... store an associated tree
data-structure (here 𝑗 is some integer which may not be constant). What will be the space
requirement of this data-structure and how much will be the query time to answer a range
query? Express your answer using 𝑂() notation in terms of 𝑛 and 𝑗. Justify your answer.