DATA STRUCTURE& ALGORITHM For Computer Science & Information Technology By www.thegateacademy.com Syllabus DSA 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 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. Data Structure and Algorithm Analysis #2. Stacks and Queues #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 Extended Binary Tree Binary Tree Height Analysis Binary Tree Construction Using Inorder Assignment 1 Assignment 2 Answer keys Explanations Height Balanced Trees (AVL Trees, B and B+) AVL Trees B – Tree Maximizing B-Tree Degree B+Tree Page No. 1 – 32 1–5 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. Priority Queues (Heaps) #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. Maximizing B+ Tree Degree Assignment 1 Assignment 2 Answer keys Explanations 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 Matrix Chain Multiplication Algorithm Greedy Algorithm NP-Completeness Other NP- Complete Problems Hashing 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. 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 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 doesn’t 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. If f n is O g n and h n is O p n then f n h n O max g n , p n 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. If f n is O g n and g n is O h n then f n is also O h n 5. log n k is O log n 6. f n am.nm am1nm1 ....a1n a0 , If f n is any polynomial of degree m , m then f n is O n 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 2 3 k n All the functions are arranged in increasing order of growth rate. If an algorithm has the time complexity O 1 , 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 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 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 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 Then, after simplifying the above equation logn = logen/ loge2 = logn. Therefore, h(n) = n + n2. 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. 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