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

© All Rights Reserved

1.9K views

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

© All Rights Reserved

- Data Structures and Algorithms Made Easy-Narasimha Karumanchi
- Computer Networks GATE CSE MADE EASY
- GATE MATERIAL CSE
- GATE CSE
- A Handbook on CSIT Engineering
- TOC MCQ
- GATE (CSE)_ADA & DSA.pdf
- MCQ for IES GATE PSUs Practice Test Workbook Booklet
- Data Structures and Algorithms Made Easy - Narasimha Karumanchi
- Operating System MCQS (GATE)
- Gate Book- Cse
- GateGenie cs
- All Dbms Mcqs
- Previous GATE paper with answer keys and solutions - Computer Science cs/it
- GQB_CS.pdf
- QRG_CS.pdf
- UGC-NET Computer Science
- GATE Computer Organization & Architecture Book
- Gate Forum Sectional Test Booklet
- GATE Solved Question Papers for Computer Science and Information Technology [CS] by AglaSem.Com

You are on page 1of 15

WORKBOOK

Detailed Explanations of

Computer Science & IT

Algorithms, Data

Structures & Programming

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

{

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

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

Dynamic Programming

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.

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

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

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)

But there are 2m delete operations which are less than no. of pop operations, hence

2m n + m

...(2)

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

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

2

6

(T) Tree

(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

=

=

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

T6 : Solution

(a)

Level 1

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

- Data Structures and Algorithms Made Easy-Narasimha KarumanchiUploaded byPadmalatha Ragu
- Computer Networks GATE CSE MADE EASYUploaded by7apes
- GATE MATERIAL CSEUploaded bysaikatdey1810
- GATE CSEUploaded byshashankniec
- A Handbook on CSIT EngineeringUploaded byVijaya Kumari
- TOC MCQUploaded bySurya Kameswari
- GATE (CSE)_ADA & DSA.pdfUploaded byAkshat Sinha
- MCQ for IES GATE PSUs Practice Test Workbook BookletUploaded byPravendraSingh
- Data Structures and Algorithms Made Easy - Narasimha KarumanchiUploaded bySherry Srivastava
- Operating System MCQS (GATE)Uploaded byJitender Sharma
- Gate Book- CseUploaded byadityabaid4
- GateGenie csUploaded byKumar Gaurav
- All Dbms McqsUploaded byShuseel Baral
- Previous GATE paper with answer keys and solutions - Computer Science cs/itUploaded byPravendraSingh
- GQB_CS.pdfUploaded byckvirtualize
- QRG_CS.pdfUploaded byckvirtualize
- UGC-NET Computer ScienceUploaded byswastik1
- GATE Computer Organization & Architecture BookUploaded byMims12
- Gate Forum Sectional Test BookletUploaded byAbhi Rockzz
- GATE Solved Question Papers for Computer Science and Information Technology [CS] by AglaSem.ComUploaded byaglasem
- GATE Operating System BookUploaded byMims12
- Gate Forum 09 CSE Operating System Practice SetUploaded bysinghmanish1997
- Gate 2007 Cse SolutionsUploaded byAkhil Singh
- Theory of Computation MCQsUploaded byJitender Sharma
- Discrete Mathematics - C. Liu.pdfUploaded byAdhitya Kamakshidasan
- MADE EASY MANUAL POST GATEUploaded byShivanee Prusty
- Gate Forum 09 CSE Digital Logic Practice SetUploaded bysinghmanish1997
- GATE Overflow Book.pdfUploaded byadeshct
- Gate Cs Solved Paper by Nodia 2Uploaded byKrishnendu Rarhi
- GATE Study Material for CSE and ITUploaded byPankaj Vanis

- Leave Form for DD & M.tech StudentsUploaded by7apes
- Dil Ka Kya Rang KaroonUploaded by7apes
- Slotwise-AD16Uploaded by7apes
- Sumero-Indo-European Language Contacts_by a. SahalaUploaded byHansSmith
- Roma PepperUploaded by7apes
- BluetoothUploaded by7apes
- LicenseUploaded byDidzis Briedis
- a1 Report Group 43Uploaded by7apes
- POM - Ass.2 IITMUploaded by7apes
- ReadmeUploaded byjiraman_womanly
- 02-dtwUploaded by7apes
- Assignement No. 3 , POM, NPTEL, IITMUploaded by7apes
- Group 22 Principles of Management,Uploaded by7apes
- Quiz2 2012Uploaded by7apes
- POE link_3Uploaded by7apes
- Gantt Chart - POM Assgn 2Uploaded by7apes
- POM Group Assignment IITMUploaded by7apes
- Acad_FeeUploaded by7apes
- Acad_FeeUploaded by7apes
- MBR LecturesUploaded by7apes
- Root (Linguistics) - Wikipedia, The Free EncyclopediaUploaded by7apes
- Soltisiak - Phisycal Anthropology and the Summerian ProblemUploaded byslozt
- A Bright Future for Indias Defense IndustryUploaded byKiran Chokshi
- ila0601Uploaded byFlorence Cheang
- Human Performance on Hard Non-Euclidean Graph ProblemsUploaded by7apes
- Ukessays.com-swot Analysis of Bajaj Two Wheeler IndustryUploaded by7apes
- How 2 Use Torrents in IITM via PuttyUploaded byyourjana

- Op Tim MatlabUploaded byBiguest Bags
- multi.pdfUploaded byHarman Singh
- B-TreeUploaded bydrizztkuo
- PepUploaded byseetkash
- DEQUEUploaded byrobinpt
- COMP3121 1 IntroductionUploaded byriders29
- Problem Solving and PythonUploaded bykalimuthu
- DDT: Distributed Decision TreeUploaded byAnonymous CyOVDbr1
- Analysis of Algorithms IIUploaded byKamakhya Gupta
- DDA Line Drawing AlgorithmUploaded byAbhishekBhardwaj
- jeas_0715_2273.pdfUploaded bypugal4
- Algorithms Hw 2Uploaded byavocetave
- Rdbms (Unit 2)Uploaded byshivamtailor
- BFS DFSUploaded bymadhurimapatra1987
- Advances Data StructuresUploaded byDan Heisenberg
- csc190 project 3 - engineering science class of 1t7 wikiUploaded byapi-242586715
- 2numUploaded byJuan Carlos De Los Santos Santos
- Genetic Algorithms for the 0/1 Knapsack ProblemUploaded bybrownka5
- Hash TablesUploaded byPriyank Paliwal
- DIT radix-2Uploaded bySuha Nori
- DS 2MarksUploaded byManideep Paduchuri
- Graphical Method and Simplex MethodUploaded byKaushik Reddy
- Ch8 Storage Indexing OverviewUploaded byKamlesh Kumar
- 6-LP Simplex (Two Phase Method)Uploaded byRizal Ahmad
- SearchingUploaded byRaffi Sk
- Sir's on Dynamic ProgrammingUploaded byapi-3844034
- H20-solution[www.alirezaweb.com].pdfUploaded byVahid Esmaeilzadeh
- Algorithms Project ReportUploaded byapi-3722999
- solpexa2_f06Uploaded byMargarita Vasco
- Pattern Recognition and Clustering TechniquesUploaded bySrknt Rckz