This is a workbook on algorithms by MADE EASY for GATE CSE 2016.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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. n 2, n3, n 4... etc.
T2 : Solution
(b)(b)(b)(b)(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 thebelow 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;
sum = 0;for (i = 1; i ≤ 4; i++) find(n/2); →O(log n)for (i = 1; i ≤ n *****n; i++) →O(n 2)sum = sum +1;
Since first for loop run 4log n times and second for loop run n 2 times.So total time complexity = O(4log n + n 2) = O(n 2).
T4 : Solution
(c )(c )(c )(c )(c )
We knwo finding k th smallest by build heap method klog k time i.e. O(n) time to build then k th element findat k th 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 n–1 th smallest element it will take n–1(log n–1) time which is asspmtotically = O(nlog n)
T5 : Solution
(b)(b)(b)(b)(b)
T(n) = + + +
Low t→ 2 t1 High→0 →
2n3
If s ta tementsand other simplestatements(0) (2n/3) → n/3 n→
(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 listO(logk).
(iii ) Remove the elements from original list where we have extracted next smallest element and insert intothe 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 eachtake Θ(k2n / k) = Θ(nk) time in worst case.
T8 : Solution
The increasing order of given five fuctions are: f 4 < f 2 < f 5 < f 1 < f 3.
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 exponantialtime complexity = O(n n).
T4 : Solution
In question already given graph T is minimum cost spanning tree. By decreasing the weight of any edge inthe 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.
(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 MSTIn 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 n–1 edges G don’t have any cycle.
for 3 to 6 = d(u) – d(v)= 2 – 2 = 0 So 2 is not possible
for 3 to 5 = d(u) – d(v) So ans is (d)= 2 – 1= 1 or –1
for 3 to 4 = d(u) – d(v)= 2 – 1= –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 everyminimum 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(n 2) time inworst 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(n 3) time i.e. for n 2