Professional Documents
Culture Documents
WORKBOOK
Detailed Explanations of
T1 : Solution
(a)
f(n) = Q(n), g(n) = O(n), h(n) = Q(n)
Then [f(n) g(n)] + h(n)
f(n) = (n)
i.e. f(n) should be anything greater than or equal to n lets take n.
g(n) = O(n)
i.e. g(n) should be less than or equal to n lets take n.
h(n) = (n)
i.e. h(n) should be equal to n.
So [f(n) g(n)] + h(n)
[n n] + n
= n2 + n = (n)
Here we only comment about lower bound. Upper bound depend an the g(n) value i.e. n2, n3, n4... etc.
T2 : Solution
(b)
max-heapify (int A[ ], int n, int i)
{
int P, m;
P = i;
while (2P n) 11 for checking left child present or not if left child not this then no need to apply the
below produces A[2P + 1] > A[2P]
{
if ((2P+1) n) for checking right child present or not between left and right child which is greater.
n = 2P+1;
else m = 2P;
www.madeeasypublications.org
Copyright
Workbook
2n
2n
2n
T(n) = T + T + T + 0(1)
3
3
3
Copyright
Low t2
t1 High
(0) (2n/3)
n/3 n
2n
3
I f st atement s
and other simple
statements
www.madeeasypublications.org
2n
T(n) = 3.T + 0(1)
3
T6 : Solution
List1
List2
List3
Sorted
Sorted
Sorted
n/k elements
n/k elements
n/k elements
Listk
...
Sorted
n/k elements
(i) Remove the smallest element from each list and build min heap with k-elements O(k).
(ii) Extract the minimum elements from this heap that will be the next smallest in the resulted list
O(logk).
(iii) Remove the elements from original list where we have extracted next smallest element and insert into
the heap O(logk).
Repeat step2 and step3 until all elements are in the resulted list
= O(k) + [O(logk) + O(logk)] O(n)
= O(n logk)
T7 : Solution
Insertion sort takes (k2) time per k-element list in worst case. Therefore sorting n/k lists of k-element each
take (k2n / k) = (nk) time in worst case.
T8 : Solution
The increasing order of given five fuctions are: f4 < f2 < f5 < f1 < f3.
T9 : Solution
(?)
www.madeeasypublications.org
Copyright
Workbook
T10 : Solution
(c)
Insertion-sort (A)
{
for j 2 to length (A)
{
key A[j]
i = j 1
while (i > 0 && A[i] > key)
{
A[i+1] A[i]
i = i 1
}
A[i+1] key;
}
}
Copyright
www.madeeasypublications.org
T1 : Solution
Kr
uskal
s algorithm: AE, AG, AB, CE, FI, FH, CD, CF
Kruskal
uskals
Prim
s algorithm: AE, AG, AB, CE, CD, CF, FI, FH
Prims
max (epi )prim's (epi )kruskal's
= 5 7 = 2
T2 : Solution
Kr
uskal
s algorithm:
Kruskal
uskals
(i) Sorting O(e log e)
(ii) Union O(n log n)
(iii) Find O(e log n)
Running time = O(e log )
Now edges are already sorted.
Running time = O (e log e)
T3 : Solution
The given problem related to some of subset problem which is np-complete problem taking exponantial
time complexity = O(nn).
T4 : Solution
In question already given graph T is minimum cost spanning tree. By decreasing the weight of any edge in
the graph should not change the minimum cost spanning tree.
So there is no need to check again for minimum spanning tree. It will take O(1) time.
www.madeeasypublications.org
Copyright
Workbook
T5 : Solution
Let e be an edge of G but not in T
(i) Run DFS on T {e}
(ii) Find cycle
(iii) Trace back edges and find edge e thus has maximum weight.
(iv) Remove e from T {e} to get MST
In T {e} Number of eges = Number of vertices
Running time of DFS = O(V+E) = O(V)
T6 : Solution
G is the connected graph with n1 edges G dont have any cycle.
Copyright
www.madeeasypublications.org
T1 : Solution
Implementation of stack using single linklist:
Inserting sequence: 1, 2, 3, 4, 5, 6
Insertion take 0(1) time
6
5
4
3
2
1
1 /
2.
1 /
3.
1 /
4.
5.
6. Insertion takes 0(1) time.
Deletion in stack (Pop)
Remove top element every time so 0(1)
Deletion in linklist
Remove 1st node every time with making second node to head.
T2 : Solution
...
Head
www.madeeasypublications.org
Copyright
Workbook
T3 : Solution
(d)
PUSH (S, P, Q, Ti, x)
{
if Ti = = (i + 1) 1
Q
{
printf (stack overflow);
exit (1);
}
else
Ti++;
S[Ti] = x;
}
P
Ti = = (i + 1) 1 indicate the last location of the array is already filled. So overflow occur.
Q
T4 : Solution
(a)
Number of push operations = n(insert) + m(delete) = n + m
So, n + m x but there are maximum 2n insert operations so n + m x 2n
...(1)
...(2)
Copyright
www.madeeasypublications.org
10
T6 : Solution
+
+
(
Height
4
Height
5
Height
3
Height
1
Height
2
Height
5
Height
3
Height
1
= 1 + 2 + 3 + 4 + 5 = 15
T7 : Solution
Expected number of probes in a unsuccessful = 1/(1 )
1
= 3
1
1 = 3 (1 )
1 = 3 3
2 = 3
= 2/3
Expected number of probes in a unsuccessful = 1/ loge 1/(1)
3
loge 3 = 0.7324
2
www.madeeasypublications.org
Copyright
T1 : Solution
(d)
Lets take a undirected graph
1
2
5
4
6
(G) Graph
6
(T) Tree
Copyright
www.madeeasypublications.org
12
=
=
for 3 to 6 =
=
for 3 to 5 =
=
=
for 3 to 4 =
=
=
12
1 or 1
d(u) d(v)
22=0
d(u) d(v)
21
1 or 1
d(u) d(v)
21
1 or 1
So 2 is not possible
So ans is (d)
T2 : Solution
Sorting the array using binary search tree will take O(n) time i.e. inorder sequence.
Sorting the array using min heap tree will take O(nlog) time i.e. O(n) time to build and log n time to get every
minimum element. So O(n) + O(nlog n) = O(nlog n).
In the giving question binary search tree is better than min heap tree. By n log n time.
T3 : Solution
In adjancy list representation of directed graph to find the out degree of each bertax will take O(n2) time in
worst case i.e. for an element we have to search n time.
T4 : Solution
In adjancy matrix representation of directed graph to find universal sink will take O(n3) time i.e. for n2
elements we have to check n time.
T5 : Solution
(d)
2
2
6
2
6
2
6
5
3
www.madeeasypublications.org
Copyright
Workbook
13
5
3
3
2
3
2
1
Level 2
Level 3
Level 4
Copyright
www.madeeasypublications.org
Programming
T1 : Solution
A(n)
{
for (i = 1 to n)
{
if (n mod i = = 0)
{
for (j = 1 to n)
printf(j)
}
= O(n/2) = O(n)
= O(n)
}
Time complexity = O(n) O(n) = O(n2).
T2 : Solution
main( )
{
int i = 3;
switch (i)
{
default : printf(zero)
Case 1 : printf(one)
break
Case 2 : printf(two)
break
Case 3 : printf(three)
break
}
}
Since i = 3 so switch (3) will go to case 3 and run the program only one time.
So time complexity = O(1).
www.madeeasypublications.org
Copyright
Workbook
15
T3 : Solution
1. Const int *P;
declare P as pointer to const integer.
2. int * const P;
declare P as constant pointer to integer
T4 : Solution
(i) Char (*(*x ( ))[ ])();
declare x as a function returning pointer to array of pointer to function returning char.
(ii) Char (*(*x[3])( ) [5];
declare x as array 3 of pointer to function returning pointer to array 5 of char.
(iii) Void (*b*int, void (*f)(int))) (int);
Syntac error
(iv) Void (*ptr)(int (*)[2], int(*)(void));
Syntax error
T5 : Solution
(b)
Char \ 0
if (0)
Printf(% S, a) = Null = 0
So condition false
So answer is else part string is not empty.
T6 : Solution
1st for loop run 1 to n = n times.
2nd for loop run log n times from n to 1.
3rd for loop run log n times for log n.
So,
1st
2nd
log n
log n
log n
...log n
3rd
log log n
log log n
log log n
...log log n
Copyright
www.madeeasypublications.org