Minor 1 Solution PDF
Minor 1 Solution PDF
Maximum marks: 35
Time: 1 PM - 2PM
Important Guidelines:
Name:
Entry number:
√
Part 2: The number of iterations of outer while loop is Θ( n/ log n) because in any two
√ difference in value of i is exactly ⌊log2 n⌋, and
consecutive iterations of while loop the
the loop breaks when i goes beyond n.
√
Alternate proof of√Part 1: The time complexity of the algorithm is Θ( n) as the outer
while loop runs Θ( n/ log n) times and the inner loop runs Θ(log n) times.
1
(b) You have a queue containing integers and an empty stack, and the only allowed operations
are: push to stack() and pop stack().
public class MyClass
{
private Stack S;
private Queue Q;
public void push_to_stack()
{
int value = Q.dequeue();
S.push(value);
}
public void pop_stack()
{
System.Out.print(S.pop() + ",");
}
}
Assume that the queue contains 1,2,3,4,5 in that order initially. Write down a sequence of
calls to push to stack() and pop stack() such that the output is “2,1,3,5,4,”.
(c) Prove or disprove the following: Let m be the size of a hash table and n be the number
of entries, if n > km there must exist a chain of size > k under any hash function H.
The average chain length is n/m which is is strictly greater than k (because n > km).
Since the average chain length is strictly greater than k, there must exist a chain of size
larger than k under any hash function H.
2
(d) A circular linked list is a linked list where the tail points to its head instead of a null
pointer. The diagram below shows a circular linked list with 4 at its head and 3 at its tail.
4 2 3
Implement the method remove for a circular linked list, that takes an integer value x as
an argument and deletes all entries in the list that are equal to x.
You may assume that class Node has two attributes: data and next.
public class CircularLinkedList
{
private Node head;
public void remove(int x)
{
Node tail=head;
while (tail.next!=head) \\find tail
{tail=tail.next;}
if(head.data==x)
head=Null;
else
for(Node n=head; n!=head; n=n.next;)
while (n.next.data==x)
{n.next=n.next.next;}
}
}
(e) Consider the hash function H(x) = ((2x ∗ (6 + x mod 3)) mod 5). Draw the hash
table you will obtain after insertion of elements 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 assuming that
you use chaining as collision resolution technique.
• T [0] : 5 → 10 → N ull
• T [1] : 3 → 4 → N ull
• T [2] : 2 → 6 → N ull
• T [3] : 7 → 8 → 9 → N ull
• T [4] : 1 → N ull
3
3 Stack with Maxima [6 marks]
You need to maintain an integer stack that along with operations push(), pop(), top() has an
additional operation called ReportMaximum(). This operation returns the maximum among all
elements currently present in the stack.
Describe an implementation of the modified stack that achieves O(1) time for all four op-
erations using a single linked-list data-structure. (You may assume all integers inserted are
non-negative).
Part 1: We first provide an implementation using two standard stacks S1 and S2. Let D be
the new data structure, then the four operations on D can be performed as below.
• D.push(int i):
First perform S1.push(i). Next check if i >= S2.top(). If yes, then perform
S2.push(i).
Note: An element is pushed into S2 as long as the sequence in S2 remains non-decreasing.
This will guarantee that S2.top() returns the maximum element in D so far.
• D.pop():
First assign i=S1.pop(). This removes the top element from S1 and stores it in i.
Next check if i==S2.top(), if yes, then perform S2.pop(). Finally return i.
• D.top():
Simply return S1.top().
• D.max():
Simply return S2.top().
Part 2: Next observe that two standard stacks S1 and S2 storing non-negative integers can
be implemented using a single linked list, say L.
• We can appropriately modify the functions to ensure that element −1 is never modi-
fied/returned.
4
4 Skip Lists [8 marks]
Consider a 2-level skip list data structure having lists L0 , L1 . The list L0 contains all√elements
in sorted order. Each element of L0 is present in the list L1 with probability p = 1/ n. (You
may assume that the number of elements in skip list remains Θ(n)).
Provide an implementation of search function, and analyse its expected time complexity.
√
Search time in L1 : The time to search x √in L1 is O(|L1 |). Since E[|L1 |] = p|L0 | = O( n),
the expected time to search x in L1 is O( n). [2 marks]
P rob(t = 1) = p
P rob(t = 2) = p · (1 − p)
P rob(t = 3) = p · (1 − p)2
..
.
P rob(t = r) = p · (1 − p)r−1
X r ∞
X
P rob(t > r) = 1 − P rob(t = i) = p · p · (1 − p)i .
i=1 i=r
So,
r
X
E[Time to Search x in L0 ] = E[t] = i · P rob(t = i) + r · P rob(t > r)
i=1
∞
X
⩽ i · p · (1 − p)i−1
i=1
√
= O(p−1 ) = O( n).
[5 marks]
5
5 Sorting [5 marks]
Consider a variant of Merge Sort where an input list of size n is divided into 3 sub-lists of size
respectively n/2, n/4, and n/4.
(i) Write down the recurrence relation for time complexity analysis.
(ii) Solve the recurrence relation to obtain a bound on the the time complexity.
Analysis:
The value T (n) is bounded above by expressions in the nodes of the following tree structures.
cn
cn
..
.
We have: