Algorithms Workbook GATE CSE MADE EASY

15
7/21/2019 Algorithms Workbook GATE CSE MADE EASY http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 1/15 Computer Science & IT Algorithms, Data Structures & Programming WORKBOOK WORKBOOK WORKBOOK WORKBOOK WORKBOOK 2016 Detailed Explanations of Try Yourself Questions

description

This is a workbook on algorithms by MADE EASY for GATE CSE 2016.

Transcript of Algorithms Workbook GATE CSE MADE EASY

Page 1: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 1/15

Computer Science & ITAlgorithms, Data

Structures & Programming

WORKBOOKWORKBOOKWORKBOOKWORKBOOKWORKBOOK2016

Detailed Explanations of Try Yourself Questions

Page 2: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 2/15

© Copyrightwww.madeeasypublications.org

T1 : Solution

(a )(a )(a )(a )(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. 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;

Divide and Conquer1

Page 3: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 3/15

© Copyright www.madeeasypublications.org

3Workbook

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

Swap (A[P], A[m]);P = m

else

return;

T3 : Solution

(a )(a )(a )(a )(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(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→

Page 4: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 4/15

© Copyrightwww.madeeasypublications.org

4 Computer Science & IT • Algo

T(n) = +

Apply master theorem

T(n) = Θ(n log 33/2 ) = Θ(n2.7 )

T6 : Solution

List1

Sorted

n/k elements

List2

Sorted

n/k elements

List3

Sorted

n/k elements

Listk

Sorted

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

T9 : Solution

(? )(?)(? )(?)(?)

Page 5: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 5/15

© Copyright www.madeeasypublications.org

5Workbook

T10 : Solution

(c )(c )(c )(c )(c )Insertion-sort (A)

for j ← 2 to length (A)

key ← A[j]

i = − j

while ( i > 0 && A[ i] > key)

A[i+1] ← A[i]

i = −i

A[i+1] ← key;

Page 6: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 6/15

© Copyrightwww.madeeasypublications.org

T1 : Solution

KrKrKrKrKruskal’uskal’uskal’uskal’uskal’ s algorithm:s algorithm:s algorithm:s algorithm:s algorithm: AE, AG, AB, CE, FI, FH, CD, CF

Prim’Prim’Prim’Prim’Prim’s algorithm:s algorithm:s algorithm:s algorithm:s algorithm: AE, AG, AB, CE, CD, CF, FI, FH

−i i = 5 – 7 = 2

T2 : Solution

KrKrKrKrKruskal’uskal’uskal’uskal’uskal’ s algorithm:s algorithm:s algorithm:s algorithm:s algorithm:

(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 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.

Greedy Technique andDynamic Programming2

Page 7: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 7/15

© Copyright www.madeeasypublications.org

7Workbook

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

Page 8: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 8/15

© Copyrightwww.madeeasypublications.org

Hashing, Stack, Queue and Array3

T1 : Solution

Implementation of stack using single linklist:Inserting sequence: 1, 2, 3, 4, 5, 6Insertion take 0(1) time

654321

Link list representation:

1. →

2. → →

3. → → →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]

Page 9: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 9/15

© Copyright www.madeeasypublications.org

9Workbook

T3 : Solution

(d)(d)(d)(d)(d)PUSH (S, P, Q, T i, x)

if = = × + − i i

printf (“stack overflow”);exit (1);

else

Ti++;S[Ti] = x;

= = × + − i i indicate the last location of the array is already filled. So overflow occur.

T4 : Solution

(a )(a )(a )(a )(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

Page 10: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 10/15

© Copyrightwww.madeeasypublications.org

10 Computer Science & IT • Algo

T6 : Solution

(

(

(

+

(

(

*

(

( (

/

+

(

*

+(

+

(

*

+

+

(

(

*

Height4

Height5

Height3

Height1

Height2

Height5

Height3

Height1

= 1 + 2 + 3 + 4 + 5 = 15

T7 : Solution

Expected number of probes in a unsuccessful = 1/(1 – α)

− α = 3

1 = 3 (1 – α)1 = 3 – 3 α

–2 = –3 αα = 2/3

Expected number of probes in a unsuccessful = 1/ α log e 1/(1– α)

= 0.7324

Page 11: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 11/15

© Copyright www.madeeasypublications.org

Linked List, Tree, Graph and P, NP4

T1 : Solution

(d)(d)(d)(d)(d)

Lets take a undirected graph

1

3

5

2

6

4

(G) Graph

and 2 is source after perform BFs ON graph.

1

2

3

45

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 = 0for 4 to 5 = d(u) – d(v)

= 1 – 1 = 0for 4 to 6 = d(u) – d(v)

Page 12: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 12/15

© Copyrightwww.madeeasypublications.org

12 Computer Science & IT • Algo

= 1 – 2= – 1 or 1

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

elements we have to check n time.

T5 : Solution

(d)(d)(d)(d)(d)

2 2 2

16 6

2 2 2

16 51 6 1

5

3

3 65

Page 13: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 13/15

© Copyright www.madeeasypublications.org

13Workbook

2 3

2 51 5

4 63

4

6 1

3

2 5

4 61

7

Level order = BFS= 3251467

T6 : Solution

(a )(a )(a )(a )(a )

3

2 5

4 61

7

Level 1

Level 2

Level 3

Level 4

Number of element in last level = 1 to 7

Page 14: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 14/15

© Copyrightwww.madeeasypublications.org

Programming5

T1 : Solution

A(n)

for (i = 1 to n)

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

printf(j)

Time complexity = O(n) × O(n) = O(n 2).

T2 : Solution

main( ) int i = 3;

switch ( i)

default : printf(“zero”)Case 1 : printf(“one”)

breakCase 2 : printf(“two”)

breakCase 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).

Page 15: Algorithms Workbook GATE CSE MADE EASY

7/21/2019 Algorithms Workbook GATE CSE MADE EASY

http://slidepdf.com/reader/full/algorithms-workbook-gate-cse-made-easy 15/15

© Copyright www.madeeasypublications.org

15Workbook

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)(b)(b)(b)(b)

Char \ 0if (0) Printf(% S”, a) = Null = 0So condition falseSo 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.

3 rd for loop run log n times for log n.So,1 st 1 2 3 n2nd log n log n log n ...log n

3rd

log log n log log n log log n ...log log nTime complexity = O(n)(log n + log log n) = O(n log n).