COL106 Problems PDF
COL106 Problems PDF
COL106 Problems PDF
1. Binary Search Let T (n) be time to perform binary search on a sorted integer array of size n.
Prove that T (n) = T (n/2) + Θ(1). Use this recurrence relation to argue T (n) = O(log n).
2. Insertion Sort Write a pseudocode to perform Insertion Sort on linked-lists storing integers.
Let T (n) be time complexity of your code. Prove that T (n) = T (n − 1) + O(n). Use this recur-
rence relation to argue T (n) = O(n2 ).
3. Merge Sort Consider a variation of Merge Sort that partitions a list of size n ⩾ 3 into
three lists of size respectively +n/3,, +n/3,, n − 2+n/3,. Write pseudocode for the modified
version of the problem, and prove that the time complexity of your code follows the relation
T (n) = 3T (n/3) + O(n). Use this recurrence relation to show T (n) = O(n log n).
4. Unbalanced Merge Sort Consider a variation of Merge Sort that partitions a list of size n ⩾ 4
into two lists of size respectively +n/4,, n − +n/4,. Prove that the time complexity follows the re-
lation T (n) = T (n/4)+T (3n/4)+O(n). Use this recurrence relation to show T (n) = O(n log n).
5. Asymptotic bounds For each pair of functions f (n) and g(n) in the table below, write O,
Ω, or Θ in the appropriate space, depending on whether f (n) = O(g(n)), f (n) = Ω(g(n)),
or f (n) = Θ(g(n)). If there is more than one relation between f (n) and g(n), write only the
strongest one. The first line is a demo solution.
n n log2 n n2
n log22 n Ω
2log2 n
log2 n!
log2 nlog2 3
1
6. Radix Sort Argue that the Radix sort algorithm always take linear time if the elements to be
sorted are integers in the range {1, . . . , n8 }. How will time complexity change if the integers are
in the range {1, . . . , nd }, for some constant d?
7. Count Sort Suppose that an array contains n numbers, each of which is −1, 0, or 1. Then,
argue that the array can be sorted in O(n) time in the worst case.
8. Stacks and Queues Consider an implementation of Stacks using linked-list that takes O(1)
time for Push and Pop operations. Your task is to use two Stacks to implement a Queue.
What will be time complexity of Enqueue, Dequeue operations as a function of queue length (n)?
9. Quick Sort Consider a variation of Quick Sort, wherein, we recursively compute a random
pivot until we come across an element having rank in range [n/4, 3n/4]. After finding such a pivot
we split input list into child lists L1 and L2 , sort these child lists recursively, and finally concatenate
them to obtain sorted L.
Algorithm 1: QUICKSORT(L)
1 Let n = SIZE (L);
2 if n ⩽ 10 then
3 Sort L using Insertion-sort and return sorted L;
4 else
5 Let L1 , L2 be two empty lists and let RANK = −1;
6 while RANK ∈ / [n/4, 3n/4] do
7 PIVOT = element of list L;
8 Compute RANK = Number of elements in L whose value is at most PIVOT;
9 foreach (x ∈ L \ {PIVOT}) do
10 if (x ⩽ PIVOT) then
11 L1 .append( x )
12 else
13 L2 .append( x )
14 Return QUICKSORT(L1 ) ·PIVOT· QUICKSORT(L2 )
Prove that the expected time complexity of above algorithm is T (n) = O(n log n).
2
COL 106 : Data Structures and Algorithms
Semester II, 2022-23, IIT Delhi
Practice Sheet - 2
(a) Consider a stack S initially contain 0 elements. What will the stack contain after perform-
ing following operations: Push(10), Push(20), Pop(), Push(30), Push(40), Pop(), Push(50),
Push(60), Pop().
(b) Consider a queue Q initially contain 0 elements. What will the queue contain after per-
forming following operations: Enqueue(10), Enqueue(20), Dequeue(), Enqueue(30), En-
queue(40), Dequeue(), Enqueue(50), Enqueue(60), Dequeue().
3. String Operations
Write a pseudo-code to perform following operations on strings using Stacks and Queues.
(b) Odd-Even Splitting: If input is s = axbycz, then the output must be two strings abc, xyz.
1
4. Balanced parenthesis
Recall the algorithm for balanced parenthesis that uses stack. What will be the maximum stack
size during the execution of the algorithm when the input is the following string:
s = “{ [ { [ ] } ] { [ ] } } [ ]”.
Can you compute this maximum value for any input string s without using stack data structure?
6. Sorting
You are given an array containing n integers. Each integer lies in the range [0, n2 ]. Give an O(n)
time algorithm to sort them in increasing order.
7. Hash Table
Consider the hash function f (x) = ((2x ∗ (5 − x mod 5)) mod 7). Draw the hash table you
will obtain after insertion of elements 76, 93, 40, 47, 33, 75 assuming that you use chaining as
collision resolution technique.
(d) Print(): remove and return the job from the front of the wait list.
2
9. Solving Maze using Queues
Let M be a maze represented by a binary matrix of size n × n. The starting position can be any
cell from the set {(i, 0) | M [i, 0] = 0, i < n}, and the destination cell is (n − 1, n − 1).
Present an algorithm to compute shortest route using queue data-structure. Argue that the time
complexity of your algorithm is O(n2 ).
3
COL 106 : Data Structures and Algorithms
Semester II, 2022-23, IIT Delhi
Practice Sheet - 3
1. Merging BSTs
Prove or disprove the following statement: “Two BSTs of size n can be merged into a single BST
in O(n) time”.
2. Tree Traversals
Insert in a BST elements in the following order: 90, 22, 5, 65, 8, 31. What is the sequence of
elements generated by an in-order, a pre-order and a post-order traversal?
3. AVL Trees
What is minimum number of nodes in an AVL tree of height 12?
1
8. Insertion in Kd trees
What is the worst-case time complexity to perform single insertion in a Kd-tree wherein axis-
aligned splitting planes are computed by taking medians? Explain.
2
COL 106 : Data Structures and Algorithms
Semester II, 2022-23, IIT Delhi
Practice Sheet - 4
Question 1 Prove that any n vertex undirected graph contains at most n − 1 bridge-edges. Also
present examples to show that this bound is tight.
Question 2 Let G = (V, E) be a undirected graph. Design an O(m + n) time algorithm to com-
pute a partitioning P = (V1 , . . . , Vk ) of V such that for any two vertices x, y ∈ V , vertices x and
y are bi-connected if and only if they lie in same partition in P.
Question 3 A vertex x in an undirected graph G is said to be a cut vertex if the number of con-
nected components of G − {x} is greater than the number of connected components of G.
Complete the pseudo-code below to obtain an O(|V | + |E|) time algorithm for computing
cut-vertices of an n-vertex undirected (possibly unconnected) graph G = (V, E).
1
1 Let (v1 , . . . , vn ) be any ordering of vertices of G;
2 for i = 1 to n do
3 VISITED [vi ] ← False and I S - CUT- VERTEX [vi ] ← False ;
4 end
5 for i = 1 to n do
6 if (VISITED[vi ] = False) then
7 LEVEL [vi ] ← 0;
8 Invoke DFS(vi );
9 if (vi has one child) then I S - CUT- VERTEX[vi ] ← False ;
10 end
11 end
Procedure Compute-Cut-vertices(G)
Question 4 Provide a pseudocode for computing topological ordering of a DAG on m edges and
n vertices in O(m + n) time that uses DFS traversal.
Question 5 Let y, z be any two vertices in a directed acyclic graph G. If there is a path from y to
z, then prove that FINISH - TIME(z) < FINISH - TIME(y) for each DFS traversal carried out in G.
2
Solution to Question 3