803purl Algorithms TYS
803purl Algorithms TYS
PSUs
Workbook 2023
Detailed Explanations of
Try Yourself Questions
© Copyright: Subject matter to MADE EASY Publications, New Delhi. No part of this book may be reproduced or utilised in any form without the written permission.
1
Loops Analysis, Asymptotic Notations,
Recursive Algorithm and Methods to
Solve Recurrence Relations
T1 : Solution
(a)
f(n) = Ω(n), g (n) = Ο(n), h(n) = Θ(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) = Ο(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, n 3, n4... etc.
T2 : Solution
The increasing order of given five fuctions are: f4 < f2 < f5 < f1 < f3.
www.madeeasypublications.org © Copyright
Detailed Explanations of Try Yourself Questions : GATE 2023 3
T3 : Solution
(b)
find (int n)
{
if (n < 2) then return;
else
{
sum = 0;
for (i = 1; i ≤ 4; i++) find(n /2); → Ο(log n)
for (i = 1; i ≤ n * n; i++) → Ο(n2)
sum = sum +1;
}
}
Since first for loop run 4log n times for which second for loop run n2 times for every value of (4 log n).
So total time complexity = Ο(4log n × n2) = Ο(n2 log n).
© Copyright www.madeeasypublications.org
2 Divide and Conquer
T1 : Solution
Ο (n logk)
(i) Remove the smallest element from each list and build min heap with k-elements ⇒ Ο(k).
(ii) Extract the minimum elements from this heap that will be the next smallest in the resulted list ⇒ Ο(logk).
(iii) Remove the elements from original list where we have extracted next smallest element and insert into
the heap ⇒ Ο(logk).
Repeat step2 and step3 until all elements are in the resulted list
= Ο(k) + [Ο(logk) + Ο(logk)] ∗ Ο(n)
= Ο(n logk)
T2 : Solution
www.madeeasypublications.org © Copyright
3 Binary Trees, Binary Heaps
and Greedy Algorithms
T1 : Solution
Ο (E + V)
In adjacency list representation of directed graph to find the out degree of each vertex will take Ο(E+V)
time in worst case i.e. for an element we have to search n time.
T2 : Solution
Ο (V + E)
In adjacency matrix representation of directed graph to find universal sink will take Ο(V + E) time i.e. for
every entry in adjacency matrix we have to check n time.
T3 : Solution
(b)
Heap is implemented using array. If ‘i ’ is parent element then ‘2i ’ is left child and ‘2i + 1’ is right child. So
if an element is delete from last level of the heap then it will take Ο(1) time. Since element can be deleted
from any level of heap tree in worst case root element is deleted then at every level one element is
exchange.
Example:
Minimum Ο(d ) time will take if a element is deleted in heap tree but not Ο(1).
© Copyright www.madeeasypublications.org
6 Computer Science & IT • Algorithms
T5 : Solution
Ο( n)
Sorting the array using binary search tree will take Ο(n) time i.e. inorder sequence.
Sorting the array using min heap tree will take Ο(n log n) time i.e. Ο(n) time to build and log n time to get
every minimum element. So Ο(n) + Ο(n log n) = Ο(n log n).
In the giving question binary search tree is better than min heap tree by Ο((n ) time
time.
T6 : Solution
(b)
max-heapify (int A[ ], int n, int i)
{
int P, m ;
P = i;
while (2P ≤ n) for checking whether left child is present.
{
if (2P + 1 ≤ n && A[2P + 1] > A[2P]) for checking if right child is present or not and finding between
left and right child which is greater.
n = 2P+1;
else m = 2P;
whichever is greater, swap that child with its parent
if (A[P] < A[m ])
{
Swap (A[P], A[m ]);
P = m;
}
else
return;
}
}
T7 : Solution
(7)
Kr uskal’
Kruskal’ s algorithm: AE, AG, AB, CE, FI, FH, CD, CF
uskal’s
Prim’s algorithm: CF, CE, EA, AG, AB, FI, FH, CD (since we have to find maximum difference so start with
Prim’s
edge CF)
www.madeeasypublications.org © Copyright
Detailed Explanations of Try Yourself Questions : GATE 2023 7
T8 : Solution
[Ο (m )]
Kr uskal’
Kruskal’ s algorithm:
uskal’s
(i) Sorting ⇒ Ο(m log m)
(ii) Union ⇒ Ο(n log n)
(iii) Find ⇒ Ο(m log n)
⇒ Running time = Ο(m log m)
Now edges are already sorted.
∴ Running time = Ο(m)
T9 : Solution
O(
O(⎪ ⎪V⎪)
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 = Ο(⎪V⎪+⎪E⎪) = Ο(⎪V⎪)
© Copyright www.madeeasypublications.org
4 Sorting Algorithms, Graph Traversals
and Dynamic Programming
T1 : Solution
(c)
Insertion-sort (A)
{
for j ← 2 to length (A)
{
key ← A[ j ]
i = j −1
i = i −1
}
A[i + 1] ← key;
}
}
T2 : Solution
Ο( n)
The length of array A is n which stores the integers. We need two additional arrays B[0...k] and C[0...k].
Initialize B and C with 0. It requires Ο(k). For each element of A increment B[A[i]]. It will take Ο(n) time B[ j ]
contain the number of elements of A having value j .
Do C[1] = B[1] and for each element i of array C do
www.madeeasypublications.org © Copyright
Detailed Explanations of Try Yourself Questions : GATE 2023 9
C[i] = B[i] + [i – 1]
It will take Ο(k) for getting answer compute C[b] – C[a] + B[a].
The preprocessing time takes = Ο(n)
T3 : Solution
T4 : Solution
(d)
Lets take a undirected graph
1 2
3 4
6
(G) Graph
and 2 is source, after performing BFS on graph we obtain the following tree.
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)
= 1–1=0
for 4 to 5 = d(u) – d(v )
= 1–1=0
for 4 to 6 = d(u ) – d (v)
= 1–2
= – 1 or 1
for 3 to 6 = d (u) – d (v )
© Copyright www.madeeasypublications.org
10 Computer Science & IT • Algorithms
T5 : Solution
(64)
In question restriction on BST is height should be ‘6’. So we need 7 levels (given that root at height ‘0’). In
creation of BST we have to use all element without repeatation.
At 1 level = We have 2 choice i.e., either take 1 or 7.
At 2 level = We have 2 choice for root 1 and 7 each. If 1 is root then 2 choice will be 6 and 2. If 7 is root then
2 choice will be 1 and 6.
At 3 level = If we take 1 at root, 6 at 2nd level than we have 2 choice i.e., 5 and 2 at 3rd level.
If we take 1 at root, 2 at 2nd level than we have 2 choice i.e., 6 and 3 at 3rd level. Similarly if we take 7 as root
element.
So till 6th level, we have two choice at every level and for last level we left with only 1 element. So number
of BST with height 6
= 2 × 2 × 2 × 2 × 2 × 2 × 1 = 26 = 64
T7 : Solution
(1500)
(A 1 A 2 A 3 A 4 )
(A 1 A 2 A 3 )A 4 (A 1 A 2 ) (A 3A 4 ) A 1( A 2A 3 A 4)
The minimum number of multiplication required using basic matrix multiplication method will be 1500.
www.madeeasypublications.org © Copyright