Col106 Iitd
Col106 Iitd
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Dashboard ► My courses ► 2101-COL106 ► 15 September - 21 September ► Minor Exam: Part 1
Calculate the time complexity of in big-theta notation. It is given that the time
taken by is units and that by is units. Assume that: assignments,
statements, statements, mathematical operations and comparisons take unit
of time.
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.
l.
m.
Question 2 What will be the final state of a min-heap (implemented using an array) if the following 7
Correct elements are inserted in order: 7, 0, 8, 5, 13, 17, 14. Assume that the heap was empty
before inserting these elements.
Mark 1.00 out of
1.00
Select one:
a. [0, 8, 5, 17, 14, 7, 13]
b. [0, 7, 5, 17, 8, 14, 13]
c. [0, 8, 5, 13, 14, 17, 7]
Select one:
a. 0
b. 8
c. 14
d. 16
e. 11
f. 12
g. 9
h. 10
i. 2
j. 1
k. 6
l. 4
m. 13
n. 15
o. 3
p. 7
q. 5
The correct answers are: Pre-order traversal: 1, 11, 16, 10, 12, 5, 7, 3, In-order
traversal: 16, 11, 1, 5, 12, 7, 10, 3, Post-order traversal: 16, 11, 5, 7, 12, 3, 10, 1
Question 5 In this question we look at an open addressing scheme called cuckoo hashing. The set
Correct of keys we are trying to store is a subset of the natural numbers and we have a hash
table T of size n. We are given two hash functions h1 and h2 which map all natural
Mark 1.00 out of
numbers to the set {0, 1, . . . , n−1}. In order to place a newly inserted key x into the
1.00
table we do the following:
1. Compute l1 = h1(x).
2. If T[l1] is free then store x in T[l1] and exit, else
3. Compute l2 = h2(x).
4. If T[l2] is free then store x in T[l2] and exit else if y is currently stored in T[l2]. Remove
y from T[l2] and store x there.
5. Now we need to find an alternate position for y. If l2 was h1(y) then compute l3 =
h2(y) and go to step 4 else if l2 was h2(y) then compute l3 = h1(y) and go to step 4. The
fifth time we have to go to step 4 we assume a cycle has occured and an exception is
thrown declaring the insertion unsuccessful.
b. State after inserting 56: [18, 55, 69, __, __, __, 56, __, 58, 49, 89]
c. State after inserting 55: [49, 69, 89, __, __, __, __, 55, __, 58, 18]
d. State after inserting 56: [69, 89, 18, __, __, __, __, 55, 49, 58, 56]
e. State after inserting 55: [49, 69, 89, __, __, __, __, __, 55, 58, 18]
f. State after inserting 56: [49, 69, 89, __, __, __, __, 55, 56, 58, 18]
g. State after inserting 56: [49, 69, 89, 56, __, __, __, __, 55, 58, 18]
h. State after inserting 56: [49, 69, 89, __, __, __, __, 56, 55, 58, 18]
i. State after inserting 55: [49, 69, 89, 55, __, __, __, __, __, 58, 18]
j. State after inserting 55: [49, 69, 89, __, __, __, 55, __, __, 58, 18]
The correct answers are: State after inserting 55: [49, 69, 89, __, __, __, __, __, 55, 58,
18], State after inserting 56: [49, 69, 89, __, __, __, __, 56, 55, 58, 18]
Jump to...
Dummy Minor Part 1 ►
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2
Name:
Entry number:
(a) True
(b) (1/2 point) State true or false: In any strongly connected undirected graph with at least two vertices,
there are always two vertices that have the same degree.
(b) True
(c) (1/2 point) State true or false: For any directed graph G with n 2 vertices and any two vertices
s, t in G, the number of distinct paths from s to t is O(2n )
(c) False
(d) (1/2 point) State true or false The graph given below is a bipartite graph.
(d) True
(e) (1/2 point) State true or false Any strongly connected directed graph with at least two vertices con-
tains a cycle.
(e) True
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2
(f) (1/2 point) Consider the binary search tree below. Draw the binary search tree obtained after
performing remove(88).
(g) (1 point) Consider the array A representing a min-heap below (only keys are shown) and answer
the question that follow:
Array index 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Key 3 10 5 13 17 6 11 15 16 21 18 9 8 23
Array index 1 2 3 4 5 6 7 8 9 10 11 12 13
Key
5 10 6 13 17 8 11 15 16 21 18 9 23
(h) (1 point) Show the AVL tree that results after each of the integer keys 9, 27, 50, 15, 2, 21, and 36 are
inserted, in that order, into an initially empty AVL tree. (You may want to show steps for partial
grading)
Page 2
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2
2. (4 points) You are given an array A[1...n] representing a min-heap and an integer k. You have to design
an algorithm to output all the keys in the array that are less k.
(For example if k = 6 and the array A is as shown below, then your algorithm should output the keys 3
and 5.)
Array index 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Key 3 10 5 13 17 6 11 15 16 21 18 9 8 23
Give pseudocode for your algorithm and discuss running time. Let m be the number of keys in A that
are smaller than k. The running time of your algorithm should be O(m).
Solution: Here is the pseudocode for the algorithm. The function call should be SmallerThan(A, n, k, 1).
SmallerThan(A, n, k, i)
- if(A[i] k OR i > n)return
- Print(A[i])
- SmallerThan(A, n, k, 2i)
- SmallerThan(A, n, k, 2i + 1)
Running time: Let the number of elements smaller than k in the array A be m. The above algorithm
is a recursive algorithm. There are constant number of operations performed in every recursive call
before further recursive calls are made. So, the total running time of the algorithm is proportional
to the total number of recursive calls that are made during the execution of the algorithm. For each
element that are smaller than k, at most two recursive calls will be made. Note that no recursive
calls are made with respect to elements that are at least k. So, the total number of recursive calls
will be bounded by 2m.
Page 3
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2
3. (3 points) Consider the following basic java implementation of Binary Search Tree that we have used in
the lecture. Complete the body of the method PrintKeys below that is supposed to print all the keys
in the tree in a sorted order (increasing order). Your algorithm should run in O(n) time where n is the
number of keys in the binary search tree. (The methods for insert, search, delete are not shown below)
class Node{
public int key;//This denotes the key stored at the node
public String value;//This denotes the value stored in the node.
public Node leftChild;//This is a reference to the left child
public Node rightChild;//This is a reference to the right child
public Node parent;//This is a reference to the parent of the node
public Node(){parent = leftChild = rightChild = null;}
}
public class BinarySearchTree{
public Node root;
public int size;
public BinarySearchTree(){size=0;root=null;}
Page 4
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-2
4. (3 points) You are given an unsorted array A = A[1...n] containing n distinct integers. Design an
algorithm that outputs the smallest k elements in the array A. The running time of your algorithm
should be O(n + k log n). Give pseudocode and discuss running time.
SmallElements(A, k, n)
- BottomUpHeapConstruction(A, n)
- For i = 1 to k
- Print(A[1])
- A[1] A[n]
-n n 1
- DownHeapBubble(A, n, 1)
DownHeapBubble(A, n, i)
-c i
- if(2i > n and 2i + 1 > n)return;
- elseif(2i n and 2i + 1 > n){if(A[c] > A[2i]) c 2i}
- else{
if(A[c] > A[2i])c 2i
if(A[c] > A[2i + 1])c 2i + 1
}
- if(c = i)return;
- else{swap(A, i, c); DownHeapBubble(A, n, c)}
BottomUpHeapConstruction(A, n)
-h blog nc
- For i = h 1 to 0
- For j = 2i to 2i+1 1
- DownHeapBubble(A, n, j)
We use the array based bottom-up heap construction discussed in the class to convert array A to
one representing a min-heap. After this we perform the remove minimum operation on this heap to
obtain the k smallest elements.
Running time: Constructing the heap will take time O(n) and performing k remove minimum
operations costs O(k log n) time. So, the total running time is O(n + k log n).
Page 5
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1
Name:
Entry number:
(a) True
(b) (1/2 point) State true or false: Let f (n) = 5n2n + 3n and g(n) = n3n . Then g(n) = O(f (n)).
(b) False
(c) (1/2 point) Express the running time of the algorithm below in big-O notation.
Alg2(A, n)
- for i = 1 to n
- for j = 2i to n
- A[i] A[j] + 1
(c) O(n2 )
(d) (1 1/2 points) Solve the following recurrence relation and write the exact value of T (n). Show
calculations.
T (n) = 2 · T (n/2) + n2 ; T (1) = 1 (assume n is a power of 2)
Solution:
T (n) = 2 · T (n/2) + n2
= 2 · (2 · T (n/22 ) + (n/2)2 ) + n2
= 22 · T (n/22 ) + n2 · (1 + 1/2)
..
.
= 2i · T (n/2i ) + n2 · (1 + 1/2 + ... + 1/2i 1
)
..
.
= 2log n · T (n/2log n ) + n2 · (1 + 1/2 + ... + 1/2log n 1
)
1 1/n
= n · T (1) + n2 ·
1 1/2
= n + 2n(n 1)
= 2n2 n
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1
2. (4 points) Solve the following recurrence relation and write the exact value of T (n).
8
>
<T (n 1) if n > 1 and n is odd
T (n) = 2 · T (n/2) if n > 1 and n is even
>
:
1 if n = 1
2. T (n) = 2blog nc
(Hint: Unrolling may be tricky. You may want to guess the bound and then use induction to confirm
your guess.)
Proof. We show this by induction on k. Let P (k) denote the given proposition in the claim. We
need to show that 8k, P (k) is true.
Base step: Base case is trivially true since T (1) = 1.
Inductive step: Suppose P (1), P (2), ..., P (i) are true. We will show that P (i + 1) is true. Consider
any 2i+1 n < 2i+2 . We need to consider the case when n is even and n is odd.
If n is odd, then T (n) = T (n 1) = 2 · T ( n 2 1 ). Note that 2i+1 n 1 < 2i+2 . So, we have
2i (n 1)/2 < 2i+1 . Applying induction hypothesis, we get T (n) = 2i+1 .
If n is even, then T (n) = 2 · T (n/2). Since 2i+1 n < 2i+2 , we have 2i n/2 < 2i+1 . Applying
induction hypothesis, we get that T (n) = 2i+1 .
Page 2
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1
3. (4 points) In the lecture, we saw an implementation of Dynamic Arrays using regular arrays involving
doubling the size of the array on overflows. What is the amortized running time for insert operations
for the implementation where we increase the size of the array by 42 on overflows. That is, if the size
of the array before overflow was m, then the size of the array after overflow is m + 42. Also, you may
assume that we start with an empty array of size 1.
Solution: Starting from an empty array of size 1, consider n insertions. We can break down the
running time into time for copying when an overflow occurs and time for inserting into the array.
Note that the total time for insertion is going to be n since n elements are being inserted. Let us
now focus on the time for copies.
Every time the current array overflows, we need to copy. The time for this copying is equal to the
size of the array when the overflow occurs. The first time the array overflows, size of the array is
1. The second time the array overflows, size of the array is (1 + 42). The third time the array
overflows, size of the array is (1 + 42 + 42). So, the ith time the array overflows, the size of the array
is (1 + 42 · (i 1)). Suppose k is the number of times the array overflows on n insertions. Then we
have
1 + 42(k 1) n < 1 + 42k
This gives k = b n421 c. So, the total cost of copying is
k
X k(k 1) n 1 (n 1) · (n 43)
1 + 42(i 1) = k + 42 · + = O(n2 ).
i=1
2 42 2 · 42
Page 3
COL106: Data Structures and Algorithms (IIT Delhi, Semester-II-2016-17) Minor-1
4. (4 points) Consider the following basic java implementation of linked list that we have used in the
lecture. Complete the body of the method SortList below that is supposed to sort the integers in the
linked list in non-decreasing order. It will be sufficient to give an O(n2 )-time algorithm for this problem.
You are NOT allowed to use arrays within this method.
class Node{
public int value;
public Node next;
}
class LinkedList{
public Node head;
public int size;
public LinkedList(){size=0; head = null;}
//other methods
..
.
//Implementation of Bubble Sort
public void SortList(){
Node N;
for(int i=0;i<size-1;++i){
N = head;
for(int j=0;j<size - i - 1;++j){
if(N.value > N.next.value){
int x = N.value;
N.value = N.next.value;
N.next.value = x;
}
N = N.next;
}
}
}
}
Page 4
COL106: Minor Examination
11/11/2020
Maximum Marks: 30
● Make sure that you keep your videos on for the duration of the examination
● Answer each question and its subpart on a separate piece of paper
● Write your name, entry number and page number on each page
● At the end of the examination, you have 10 minutes to send images of your answers to
your TAs using WhatApp/Telegram
● Once you have sent the pictures of your answers to your TAs, you need to convert them
into a single PDF file and upload it on Gradescope by 3:00pm.
● Answers not uploaded on gradescope will not be evaluated
● In case you are unable to upload your answers on Gradescope by 3:00pm due to
Internet or power issues, but you have sent the images of your answers to your TA by
the deadline, please send your PDF to your TA as soon as possible.
Question 1 [10 Marks]
(a) Consider the two functions f: N→N and g:N→N defined as follows:
f(n) = n when x is not a prime number
3
n when x is a prime number; and
g(n) = n2 for all x
Formally prove that neither f(n) is in O(g(n)), nor g(n) is in O(f(n)). [7]
Let us call such a pair of functions as `mutually non-dominating’.
Write pseudocode to multiply two sparse matrices A and B, using the above representation.
Note that the two matrices can have 0 values at different indices, though number of columns in
A can be assumed to be the same as the number of rows in B. [10]
Please do not allow any bag, phone or other electronic device near you. Keep your ID card next to you on the desk. Maximum
marks available for questions are listed in []. Write answers in the provided space. Justify all answers.
1. (11)
a) [8] Given the Hash function h below, list the table slots touched/probed and show the status of the Hash table after
each listed operation (in order from left to right, starting with an empty hash table). The table has 5 slots. Assume
open addressing with hi as given: (Deletion is by marking as deleted.)
h = h0 = (∑ digits ) % 5
hi = (h0 + 3*i) % 5
Insert:873 Insert:9734 Insert:280 Delete:9734 Search 143 Insert:14 Rough space
Slots probed:
| | | | | |
b) [3] What is the termination condition for a search in such a Hash table?
2. [5] Write code (approximate syntax is OK) to restructure to balance the following configuration. Assume references
left, right, parent and value are stored for each node. Assume p.parent.left == p and n.parent.right == n. (No references
should be assumed to be null.) Restructure(Node n):
Node p = n.parent, g = p.parent; g
p
n
3. (10) Consider a binary search tree that allows keys to be repeated in multiple nodes such that keys equal to any
node's key are always in that node's left subtree. Write the pseudo-code for search and delete for this tree that each take
time O(h) for a tree with height h. The search function must return all instances and the delete function must delete all
instances of the given key.
[6] Delete(root, key): [4] Search(root, key):
4. [5] You need to implement a Red-Black tree that supports multiple threads inserting, searching or deleting in parallel.
(You may assume that each thread uses the standard algorithms to perform these operations.) What synchronization is
required to ensure that the threads do not interfere with each other's operation?
5. [5] Consider a skip-list that supports multiple threads inserting, searching or deleting in parallel. (Assume each
thread uses the standard algorithms to perform these operations.) What synchronization is required to ensure that the
threads do not interfere with each other's operation?
Please write your entry number and name on each sheet.
6. [4+3+3] Show that the height of a 2-4 tree with n keys is (a) no less than floor(0.5 log2n) and (b) no more than log2n.
(c) Also show that the number of comparisons required to find a key is no more than 2 log 2n. (5 extra marks to show the
bound to be 1.3 log2n)
7. [6] You are given two heaps with 2h keys each, where h is an integer. Provide an O(h) algorithm to merge these two
heap. (Assume all keys in the two heaps are comparable to each other.)
8. [6] In an AVL tree with with 20 nodes having numbers 1 to 20, respectively, as keys, which numbers may not appear
in the root?
Please write your entry number and name on each sheet.
9. [6] Given the keys of a Red-black tree in the pre-order traversal order, provide an algorithm to re-construct the Red-
black tree. (You do not have to determine the nodes' colors.)
10. [6] Provide non-recursive pseudo-code to compute the depth of a binary tree.
RO
U
G
H
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner