Top Banner
12

Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Jan 19, 2015

Download

Education

klirantga

THE GATE ACADEMY's GATE Correspondence Materials consist of complete GATE syllabus in the form of booklets with theory, solved examples, model tests, formulae and questions in various levels of difficulty in all the topics of the syllabus. The material is designed in such a way that it has proven to be an ideal material in-terms of an accurate and efficient preparation for GATE.

Quick Refresher Guide : is especially developed for the students, for their quick revision of concepts preparing for GATE examination. Also get 1 All India Mock Tests with results including Rank,Percentile,detailed performance analysis and with video solutions

GATE QUESTION BANK : is a topic-wise and subject wise collection of previous year GATE questions ( 2001 – 2013). Also get 1 All India Mock Tests with results including Rank,Percentile,detailed performance analysis and with video solutions

Bangalore Head Office:
THE GATE ACADEMY
# 74, Keshava Krupa(Third floor), 30th Cross,
10th Main, Jayanagar 4th block, Bangalore- 560011
E-Mail: [email protected]
Ph: 080-61766222
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.
Transcript
Page 1: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY
Page 2: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

DATA STRUCTURE

&

ALGORITHM

For

Computer Science

&

Information Technology

By

www.thegateacademy.com

Page 3: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Syllabus DSA

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

Syllabus for Data Structures and Algorithms

Programming in C; Functions, Recursion, Parameter passing, Scope, Binding; Abstract data types,

Arrays, Stacks, Queues, Linked Lists, Trees, Binary search trees, Binary heaps.

Analysis, Asymptotic notation, Notions of space and time complexity, Worst and average case

analysis; Design: Greedy approach, Dynamic programming, Divide-and-conquer; Tree and graph

traversals, Connected components, Spanning trees, Shortest paths; Hashing, Sorting, Searching.

Analysis of GATE Papers

(Data Structures and Algorithms)

Year Percentage of marks Overall Percentage

2013 18.00

11.33%

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

Page 4: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Contents DSA

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

CC OO NN TT EE NN TT SS

Chapters Page No.

#1. Data Structure and Algorithm Analysis 1 – 32 Assymptotic Notation 1 – 5

Algorithm Analysis 5 – 10

Notation of Abstract Data Types 10 – 14

Recurrence 14 – 17

Assignment 1 18 – 23

Assignment 2 24 – 27

Answer keys 28

Explanations 28 – 32

#2. Stacks and Queues 33 – 55 Stacks 33

Stack ADT Implementations 34 – 36

The Stack Purmutation 36 – 40

Running Time Analysis 40 – 41

Binary Expression Tree 41 – 45

Queue 45

Different Type of Queue Implementations 46 – 48

Assignment 1 49 – 51

Assignment 2 51 – 52

Answer keys 53

Explanations 53 – 55

#3. Trees 56 – 84 Extended Binary Tree 56

Binary Tree 56 – 58

Height Analysis 59 – 60

Binary Tree Construction Using Inorder 60 – 70

Assignment 1 71 – 75

Assignment 2 76 – 78

Answer keys 79

Explanations 79 - 84

#4. Height Balanced Trees (AVL Trees, B and B+) 85 – 113

AVL Trees 85 – 94

B – Tree 94 – 96

Maximizing B-Tree Degree 96 – 102

B+Tree 102 – 103

Page 5: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Contents DSA

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

Maximizing B+ Tree Degree 103 – 104

Assignment 1 105 – 107

Assignment 2 107 – 108

Answer keys 109

Explanations 109 – 113

#5. Priority Queues (Heaps) 114 – 135 Introduction 114

Binary Heap 114 – 118

Array Representation of Binary Heap 118 – 119

MinHeap Vs MaxHeap 119

Basic Heap Operation 119 – 121

Building a Heap by Inserting Items One at the Time 121 – 125

Sum of the Height of All Nodes of a Perfect Binary Tree 125 – 126

Assignment 1 127 – 129

Assignment 2 130 – 131

Answer keys 132

Explanations 132 – 135

#6. Sorting Algorithms 136 – 149 Bubble Sort 136 – 137

Insertion Sort 137 – 139

Selection Sort 139 – 140

Merge Sort 140 – 141

Heap Sort 141

Quick Sort 141 – 142

Assignment 1 143 – 144

Assignment 2 145 – 146

Answer keys 147

Explanations 147 – 149

#7. Graph Algorithms 150 – 170 Important Definitions 150 – 151

Representation of Graphs 151

Single Source Shortest Path Algorithm 151 – 154

Minimum Spanning Tree 154 – 159

Assignment 1 160 – 163

Assignment 2 163 – 166

Answer keys 167

Explanations 167 – 170

#8. Dynamic Programming 171 – 194 Introduction 171

Idea of Dynamic Programming 171 – 172

Page 6: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Contents DSA

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

Matrix Chain Multiplication Algorithm 172 – 175

Greedy Algorithm 175 – 183

NP-Completeness 183 – 186

Other NP- Complete Problems 186 – 189

Hashing 189 – 194

Module Test 195 – 209

Test Questions 195 – 205

Answer Keys 206

Explanations 206 – 209

Reference Books 210

Page 7: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Chapter-1 DSA

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

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.

Big–Oh

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 0,n n n where 0,c n 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

3 2f n n

3 2 4n n for all 2n

3 2n O n Here 04, 2c n

Example 2

2 3 5f n n n

2 23 5 2n n n for 3n

Page 8: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Chapter-1 DSA

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

2 23 5n n O n Here 02, 3c n

Example 3

23.4nf n n

23.4 5.4n nn

23.4 4n nn O for 1n

Example 4

23 2 4n n O n

Because here doesn’t exist any positive 0n and cso that Big-Oh equation gets satisfied.

Remarks:

For the function 4n+3,

4n+3 is O n

4n+3 is also 2O n and 3O n

Even though 4n+3 is 2O n and 3O n but the best answer for , 4n+3 is O n only, as

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. If f n is O g n and h n is O p n then max ,f n h n O g n p n

Example 5

2f n =n , h n =logn

2 2n logn O n

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

Page 9: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Chapter-1 DSA

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

4. If f n is O g n and g n is O h n then f n is also O h n

5. log kn is logO n

6. If f n is any polynomial of degree m , 11 1 0. ....m m

m mf n a n a n a n a ,

then f n is mO n

In general one should remember order of the following functions which will help while solving the relative growth rate of more complicated functions.

2 3 k nO 1 ,O logn ,O n ,O nlogn ,O n ,O n ....O n ,O 2

All the functions are arranged in increasing order of growth rate.

If an algorithm has the time complexity 1O , then the time complexity is said to be constant,

that means running time is independent of input size.

Big Omega

Big Omega represents lower 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 not be less than “n” for all inputs of size n for a given algorithm. However, it may be greater than this.

More formally Big-Omega is defined as follows:

If f x and g x are any two functions and f x is ,g x

If .f x c g x for x k where c and k are any two positive constants.

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 2 2n y n for 1n

2 4n is Ω n here 2, 1c k

we can also say that 2 4n n for 1n then 1, 1c k

Page 10: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Chapter-1 DSA

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

Remarks:

If f n is O g n , then g n is f n .

Example: 7

2n is 3O n

3n is 2n

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 0x x , then

f x g x here c and x0 are two positive constants.

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

Page 11: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY

Chapter-1 DSA

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

Then, after simplifying the above equation

logn = logen/ loge2 = logn.

Remarks

If f x g x then g x is also f x

If f x g x we can say that f x is O g x and f x is

g x and also g x is O f x and g x is Ω 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:

An algorithm may have some input.

An algorithm should produce at least one output.

Each statement should be clear without any ambiguity.

An algorithm in contrast to a program should terminate in a finite amount of time.

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.

Running time complexity

The time required for running an algorithm.

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.

Therefore, h(n) = n + n2.

Page 12: Computer Science Engineering : Data structure & algorithm, THE GATE ACADEMY