GATE Data Structure & Algorithm Book
GATE Data Structure & Algorithm Book
&
ALGORITHM
For
Computer Science
&
Information Technology
By
www.thegateacademy.com
Syllabus
DSA
Percentage of marks
2013
18.00
2012
19.00
2011
13.0
2010
18.00
2009
4.67
2008
4.67
2007
4.67
2006
8.00
2005
7.33
2004
16.67
2003
10.67
Overall Percentage
11.33%
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30 th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Contents
DSA
CONTENTS
Chapters
#1.
#2.
#3.
Stacks
Stack ADT Implementations
The Stack Purmutation
Running Time Analysis
Binary Expression Tree
Queue
Different Type of Queue Implementations
Assignment 1
Assignment 2
Answer keys
Explanations
Trees
#4.
Assymptotic Notation
Algorithm Analysis
Notation of Abstract Data Types
Recurrence
Assignment 1
Assignment 2
Answer keys
Explanations
AVL Trees
B Tree
Maximizing B-Tree Degree
B+Tree
Page No.
1 32
15
5 10
10 14
14 17
18 23
24 27
28
28 32
33 55
33
34 36
36 40
40 41
41 45
45
46 48
49 51
51 52
53
53 55
56 84
56
56 58
59 60
60 70
71 75
76 78
79
79 - 84
85 113
85 94
94 96
96 102
102 103
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page I
Contents
#5.
#6.
Bubble Sort
Insertion Sort
Selection Sort
Merge Sort
Heap Sort
Quick Sort
Assignment 1
Assignment 2
Answer keys
Explanations
Graph Algorithms
#8.
Introduction
Binary Heap
Array Representation of Binary Heap
MinHeap Vs MaxHeap
Basic Heap Operation
Building a Heap by Inserting Items One at the Time
Sum of the Height of All Nodes of a Perfect Binary Tree
Assignment 1
Assignment 2
Answer keys
Explanations
Sorting Algorithms
#7.
Important Definitions
Representation of Graphs
Single Source Shortest Path Algorithm
Minimum Spanning Tree
Assignment 1
Assignment 2
Answer keys
Explanations
Dynamic Programming
Introduction
Idea of Dynamic Programming
DSA
103 104
105 107
107 108
109
109 113
114 135
114
114 118
118 119
119
119 121
121 125
125 126
127 129
130 131
132
132 135
136 149
136 137
137 139
139 140
140 141
141
141 142
143 144
145 146
147
147 149
150 170
150 151
151
151 154
154 159
160 163
163 166
167
167 170
171 194
171
171 172
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page II
Contents
Module Test
Test Questions
Answer Keys
Explanations
Reference Books
DSA
172 175
175 183
183 186
186 189
189 194
195 209
195 205
206
206 209
210
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page III
Chapter-1
DSA
CHAPTER 1
Data Structure and Algorithm Analysis
Once an algorithm is given for a problem and decided to be correct, then an important step is to
determine how much in the way of resources, such as time or space, the algorithm will be
required.
The analysis required to estimate use of these resources of an algorithm is generally a
theoretical issue and therefore a formal framework is required. In this framework, we shall
consider a normal computer as a model of computation that will have the standard repertoire of
simple instructions like addition, multiplication, comparison and assignment, but unlike the case
with real computer, it takes exactly one unit time unit to do anything (simple) and there are no
fancy operations such as matrix inversion or sorting, that clearly cannot be done in one unit
time. We also always assume infinite memory.
Asymptotic Notation
The asymptotic notations are used to represent the relative growth rate between functions.
BigOh
Represent upper bound on the running time and the memory being consumed by the algorithms.
O(n) essentially conveys that the growth rate of running time/memory consumption rate will
not be more than n for all inputs of size n for a given algorithm. However, it may be less than
this.
More formally Big-Oh is defined as follows:
The function f n O g (n) if and only if f n c. g n for all n, n n0 where c, n0 are
positive constants.
Thus, if f n O g (n) statement is said to be true then the growth rate of function g(n) is
surely higher than/equal to f(n).
Example 1
f n 3n 2
3n 2 4n for all n 2
3n 2 O n Here c 4, n0 2
Example 2
f n n2 3n 5
n2 3n 5 2n2 for n 3
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page 1
Chapter-1
n2 3n 5 O n2
DSA
Here c 2, n0 3
Example 3
f n 3.4n n2
3.4n n2 5.4n
3.4n n2 O 4n for n 1
Example 4
3n2 2n 4 O n
Because here doesnt exist any positive n0 and c so that Big-Oh equation gets satisfied.
Remarks:
For the function 4n+3,
4n+3 is O n
2
3
Even though 4n+3 is O n and O n but the best answer for , 4n+3 is O n only, as
2
3
4n+3 is also O n and O n
O n shows most tighter upper bound than the other in the question.
Big-Oh Properties
1.
If f n is O g n then a. f n is also O g n
2.
Example 5
f n = n2 , h n = logn
n2 logn O n2
3.
If f n is O g n and h n is O p n then f n .h n is O g n . p n
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page 2
Chapter-1
DSA
4.
5.
log n k is O log n
6.
In general one should remember order of the following functions which will help while solving
the relative growth rate of more complicated functions.
, O n ....O n , O 2
O 1 , O logn , O n , O nlogn , O n
Thus, if f x is g x statement is said to be true then the growth rate of function g(x) is
surely lower than/equal to f(x).
Example 6:
f n
n
n
n for n
2n y 2n for n 1
2n 4 is n here c 2, k 1
we can also say that 2n 4 n for n 1 then c 1, k 1
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page 3
Chapter-1
DSA
Remarks:
If f n is O g n , then g n is f n .
Example: 7
n3 is n2
n 2 is O n3
Theta Notation ( )
Theta represents tightest bound on the running time and the memory being consumed by the
algorithms. (n) essentially conveys that the growth rate of running time/memory consumption
rate will be equal to n for all inputs of size n for a given algorithm. It actually conveys that both
lower and upper bounds are equal.
More formally Theta is defined as follows:
If f x and g x are two functions, and
if f x c. g x for x x0 , then
Thus, if f x g x statement is said to be true then the growth rate of function g(x) is
surely equal to f(x) and not less or not more than f(x).
Example 8
f(n) = n2 + n + 1; g(n) = 5n2 + 1; h(n) = 2logn + n2
Then, f(n) = (g(n)) because both have same degree and hence will have same growth rate.
f(n) = (h(n)) statement is also true because both have same degree and hence will have same
growth rates.
h(n) can be simplified as follows:
2logn is n only,
Let 2logn = n ---------> 1
By taking log on both sides in equation 1.
logn*loge2 = logen
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page 4
Chapter-1
DSA
Remarks
If f x g x then g x is also f x
Algorithm Definition
An algorithm is a finite set of steps or instructions to accomplish a particular task represented in
a step by step procedure. Algorithm possesses the following basic properties:
Algorithm Analysis
The following two components need to be analyzed for determining algorithm efficiency. If we
have more than one algorithms for solving a problem then we really need to consider these two
before utilizing one of them.
Space complexity
The amount of space required at run-time by an algorithm for solving a given problem.
In general these measurements are expressed in terms of asymptotic notations, like Big-Oh,
theta, Omega etc.
THE GATE ACADEMY PVT.LTD. H.O.: #74, Keshava Krupa (third Floor), 30th Cross, 10th Main, Jayanagar 4th Block, Bangalore-11
: 080-65700750, [email protected] Copyright reserved. Web: www.thegateacademy.com
Page 5