0% found this document useful (0 votes)
130 views

Workbook Workbook Workbook Workbook Workbook: Try Yourself Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

Workbook Workbook Workbook Workbook Workbook: Try Yourself Questions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

2016

WORKBOOK
Detailed Explanations of
Try Yourself Questions

Computer Science & IT


Algorithms, Data
Structures & Programming
1 Divide and Conquer

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 3

if (A[P] < A[m])


{
Swap (A[P], A[m]);
P=m
}
else
return;
}
}

T3 : Solution

(a)
find (int n)
{
if (n < 2) then return;
else
{
sum = 0;
for (i = 1; i 4; i++) find(n/2); O(log n)
for (i = 1; i n* n; i++) O(n2)
sum = sum +1;
}
}
Since first for loop run 4log n times and second for loop run n2 times.
So total time complexity = O(4log n + n2) = O(n2).

T4 : Solution

(c)
We knwo finding kth smallest by build heap method klog k time i.e. O(n) time to build then kth element find
at kth level in worst case.
So O(n) + O(klog k) = O(klog k)
Here in this questions worst case i = n 1 assume.
So to find n1th smallest element it will take n1(log n1) time which is asspmtotically = O(nlog n)

T5 : Solution

(b)

2n 2n 2n
T(n) = T + T + T + 0(1)
3 3 3

Low t2 t1 High 2n I f st atement s


0
3 and other simple
(0) (2n/3) n/3 n statements

Copyright www.madeeasypublications.org
4 Computer Science & IT Algo

2n
T(n) = 3.T + 0(1)
3

Apply master theorem


T(n) = (n log33/2) = (n2.7)

T6 : Solution

List1 List2 List3 Listk


Sorted Sorted Sorted ... Sorted
n/k elements n/k elements n/k elements n/k elements

Sorted list of n-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 5

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
2 Greedy Technique and
Dynamic Programming

T1 : Solution

Kr uskal
Kruskal s algorithm: AE, AG, AB, CE, FI, FH, CD, CF
uskals
Prims algorithm: AE, AG, AB, CE, CD, CF, FI, FH
Prims

max (epi )prim's (epi )kruskal's = 5 7 = 2

T2 : Solution

Kr uskal
Kruskal s algorithm:
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 7

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
3 Hashing, Stack, Queue and Array

T1 : Solution

Implementation of stack using single linklist:


Inserting sequence: 1, 2, 3, 4, 5, 6 6
5
Insertion take 0(1) time
4
Link list representation: 3
2
1. 1 / 1

2. 2 1 /

3. 3 2 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

Enque operation takes O(1) time


Deque operation takes O(n) time [visits last node]

www.madeeasypublications.org Copyright
Workbook 9

T3 : Solution

(d)
PUSH (S, P, Q, Ti, x)
{
P
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)
Number of pop operations = n + m
But there are 2m delete operations which are less than no. of pop operations, hence
2m n + m ...(2)
From (1) and (2): n + m x 2n and 2m n + m

T5 : Solution

Formula to find location of a[20] [20] [30] = 10 + {[(20 1) (30 1) (40 1)] + (20 1) (30 1) + (30 1)}
= [10 + (19 29 39) + (19 29) + (29)]
= 10 + 21489 + 551 + 29
= 10 + 22069
= 22079

Copyright www.madeeasypublications.org
10 Computer Science & IT Algo

T6 : Solution

+ +
+ ( (
( * * * *
( ( ( / ( (
( ( ( ( ( + + +
Height Height Height Height Height Height Height Height
4 5 3 1 2 5 3 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
4 Linked List, Tree, Graph and P, NP

T1 : Solution

(d)

Lets take a undirected graph

1 2

3 4

6
(G) Graph

and 2 is source after perform BFs ON graph.


2

1 5 4

3 6

(T) Tree
Now missing edges are: 1 to 5, 4 to 5, 4 to 6, 3 to 6, 3 to 5, 3 to 4 for 1 to 5 = d(u) d(v)
(Distance from 2 to 1) (Distance from 2 to 5)
= 11=0
for 4 to 5 = d(u) d(v)
= 11=0
for 4 to 6 = d(u) d(v)

Copyright www.madeeasypublications.org
12 Computer Science & IT Algo

= 12
= 1 or 1
for 3 to 6 = d(u) d(v)
= 22=0 So 2 is not possible
for 3 to 5 = d(u) d(v) So ans is (d)
= 21
= 1 or 1
for 3 to 4 = d(u) d(v)
= 21
= 1 or 1

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 2

6 1 6

2 2 2

1 6 1 6 1 5

5 5 3 6

www.madeeasypublications.org Copyright
Workbook 13

2 3

1 5 2 5

3 6 1 4 6

2 5


1 4 6

Level order = BFS= 3251467

T6 : Solution

(a)

3 Level 1

2 5 Level 2

1 4 6 Level 3

7 Level 4

Number of element in last level = 1 to 7

Copyright www.madeeasypublications.org
5 Programming

T1 : Solution

A(n)
{ for (i = 1 to n)

{ if (n mod i = = 0)
= O(n/2) = O(n)
{ for (j = 1 to n) = O(n)
printf(j)

}
}
}
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 1 2 3 n
2nd log n log n log n ...log n
3rd log log n log log n log log n ...log log n
Time complexity = O(n)(log n + log log n) = O(n log n).

Copyright www.madeeasypublications.org

You might also like