This action might not be possible to undo. Are you sure you want to continue?

# DATA STRUCTURES

AND

ALGORITHMS MADE EASY

-To All My Readers

No part of this book may be reproduced in any form or by any electronic or mechanical means. All rights reserved.Copyright© by All rights reserved. Designed by Copyright © CareerMonk Publications. without written permission from the publisher or author . including information storage and retrieval systems. All rights reserved.

. ]. He is the first person who taught me and its by giving good books on them. . . . of ]. ] for . [ . . proofreading and design. of ]. I thank all of you Last but not least. and for your help and suggestions. allowed me to quote their remarks and assisted in the editing. my family during our studies. [ . ]. ]. for encouraging me when I was at . [ . . [ . for approaching me for teaching Data Structures and Algorithms at their training centers. . . ] and ] for spending time in reviewing. ]& [ . testing code and for providing me the ]. talked things over. to all those who provided support. I would like to thank the following individuals. for helping me and ]. for his encouragement during my stay at the importance of & [Founders of ]. [ [ [ valuable suggestions. [ ]. offered comments. have contributed greatly to the quality of this book. [ spending their valuable time in reviewing the book. I would like to thank & [ [ [ ]. They are the primary reason for initiation of this book. . Founder of . In particular. wrote. M-Tech.Acknowledgements I would like to express my gratitude to many people who saw me through this book. read. suggestions and encouragement.

and know that you are the creator of your own destiny. -Stand up. -- . This does not mean that first stroke was useless.A stone is broken by the last stroke. be strong. Take the whole responsibility on your own shoulders. Success is a result of continuous daily effort. be bold.

it‟s an enumeration of possible solutions. first you will see the basic required theory and then followed by problems. even if we get a new question it gives us a way to think about all possible solutions. If you read as an . at least one complete reading of this book is required to get full understanding of all the topics. If any such typos found.Preface Dear Reader. due to human tendency there could be some minor typos in the book. I request you to constantly monitor this site for any corrections. you observe multiple solutions with different improved complexities). We start with solution and slowly move towards the possible for that problem. As a if you read complete book with good understanding. campus preparations. Basically. With this approach. There a total of approximately algorithmic puzzles and all of them are with solutions. But I would like to strongly recommend reading preface of this book at least. In the subsequent readings. Please Hold on! I know many people do not read preface. This book is very much useful for interview preparation. Have a nice reading. For each chapter. you will give better lectures with easy go approach and as a result your students will feel proud for selecting Computer Science / Information Technology as their degree. This preface has from regular prefaces. While writing the book. Founder of . they will be updated at . This book is very much useful for the of and during their academic preparations. solutions are provided with different complexities. competitive exams preparation. Also. new problems and solutions. All the chapters of this book contain theory and their related problems as many as possible. In all the chapters you will see more importance given to problems and analyzing them instead of concentrating more on theory. M-Tech. I have followed a pattern of improving the problem solutions with different complexities (for each problem. the content of this book covers the topics in full details. enough readings were given for correcting the errors. For many of the problems. an intense care has been taken to help students who are preparing for these kinds of exams. you can directly go to any chapter and refer. It is that. Main objective of the book is not to give you the theorems and proofs about and . Wish you all the best. Even though. If you read as a preparing for competition exams for Computer Science/Information Technology]. For each problem we will try to understand how much time the algorithm is taking and how much memory the algorithm is taking. please provide your valuable suggestions at: . I am sure you will challenge the interviewers and that is the objective of this book.

Other Titles by Narasimha Karumanchi Success keys for Big Job Hunters Data Structures and Algorithms for GATE Data Structures and Algorithms Made Easy in Java Coding Interview Questions Peeling Design Patterns .

Hashing ------------------------------------------------------------------------. Linked Lists -----------------------------------------------------------------------38 4. Miscellaneous Concepts -------------------------------------------------------.344 17.9 2. Sorting--------------------------------------------------------------------------. Selection Algorithms [Medians]-----------------------------------------------. Greedy Algorithms ------------------------------------------------------------.305 15. String Algorithms --------------------------------------------------------------.106 7. Graph Algorithms -------------------------------------------------------------. Algorithms Design Techniques -----------------------------------------------. Recursion and Backtracking ------------------------------------------------------33 3.415 .294 13.193 9. Trees ----------------------------------------------------------------------------.246 11.347 18. Complexity Classes ------------------------------------------------------------.Table of Contents 1. Divide and Conquer Algorithms ----------------------------------------------.303 14.357 19.175 8. Priority Queue and Heaps -----------------------------------------------------.320 16. Stacks ------------------------------------------------------------------------------74 5. Queues ----------------------------------------------------------------------------96 6.202 10.408 21. Introduction ----------------------------------------------------------------------.269 12.371 20. Disjoint Sets ADT --------------------------------------------------------------. Searching -----------------------------------------------------------------------. Symbol Tables------------------------------------------------------------------. Dynamic Programming --------------------------------------------------------.

.

We first concentrate on understanding the basic elements of algorithms.. If we have a problem and wanted to code it.1 Variables Before going to the definition of variables.2 Data types In the above equation. programming languages and compilers are providing the facility of data types. Examples of data types are: integer. it‟s very difficult to provide the solution in terms of zeros and ones. combining bytes ( bits) and calling it as . That means.Data Structures and Algorithms Made Easy Introduction INTRODUCTION Chapter-1 The objective of this chapter is to explain the importance of analysis of algorithms. A in a programming language is a set of data with values having predefined characteristics. Similarly. All of us have solved many mathematical equations since childhood.. double. the equation has some names ( and ) which hold values (data). real numbers ( etc…) or just and . at the top level. Similarly. As an example. in memory we are combining bytes ( bits) and calling it as . char. 1. character.). their notations. bool. To solve the equation. importance of analysis and then slowly move towards analyzing the algorithms with different notations and finally the problems. floating point unit number. we need to relate them to kind of values they can take and is the name being used in computer science for this purpose. To help users. consider the below equation: We don‟t have to worry about the use of above equation. 1. After completion of this chapter you should be able to find the complexity of any given algorithm (especially recursive functions).. etc. takes bytes (actual value depends on compiler). in computer science we need something for holding data and are the facility for doing that. the variables and can take any values like integral numbers ( . string etc. relationships and solving as many problems as possible. Basically. there are two types of data types: System defined data types (also called data types) User defined data types System defined data types (Primitive data types) Data types which are defined by system are called data types. Computer memory is all filled with zeros and ones. takes bytes etc… This says that. The important thing that we need to understand is. the ( and ) are the place holders for representing data. float.1 Variables 9 .. The primitive data types which are provided by many programming languages are: int. For example. etc… 1. A data type reduces the coding effort. let us relate them to old mathematical equations.

queues. trees. float. in general user defined data types are defined along with their operations. Good example of user defined data types are: structures in and classes in . files. To simplify the process of solving the problems. Stacks and Queues. That means. For example.) supports basic operations like addition.3 Data Structure Based on the above discussion.Data Structures and Algorithms Made Easy Introduction The number of bits allocated for each primitive data type depends on the programming languages. et. struct newType { int data1.. }. data structures are classified into two types: 1) Elements are accessed in a sequential order but it is not compulsory to store all elements sequentially (say. stacks. User defined data types If the system defined data types are not enough then most programming languages allows the users to define their own data types called as user defined data types. Declaration of data 2. bytes ( bits). Linked Lists). General data structure types include arrays. If it takes. : Trees and graphs. different languages may use different sizes. we generally combine the data structures along with their operations and are called (ADTs). float data 2. An ADT consists of parts: 1. This gives more flexibility and comfort in dealing with computer memory. 1. graphs and so on. For the same primitive data type. compiler and operating system. : Linked Lists. Declaration of operations 1. If it takes bytes ( bits) then the total possible values are to + (-1). in the below case. Same is the case with remaining data types too. once we have data in variables. For example. we are combining many system defined data types and called it as user defined data type with name “ ”. then the possible values are between to (-1). 2) : Elements of this data structure are stored/accessed in a non-linear order. is a particular way of storing and organizing data in a computer so that it can be used efficiently. … char data. The implementation for these operations can be done when we want to actually use them. we need some mechanism for manipulating that data to solve problems. subtraction etc… The system is providing the implementations for the primitive data types. let us consider the different view of system defined data types. all primitive data types (int. Depending on the size of the data types the total available values (domain) will also changes. by default. linked lists. We all know that. 1.4 Abstract Data Types (ADTs) Before defining abstract data types. Depending on the organization of the elements.3 Data Structure 10 . “ ” may take bytes or bytes. a is a specialized format for organizing and storing data. That means. For user defined data types also we need to define operations.

Note: we do not have to prove each step of the algorithm.g. by train and also by cycle. and some are highly specialized to specific tasks. stack uses LIFO (Last-In-First-Out) mechanism while storing the data in data structures. 1. Hash Tables. By the end of this book. selection sort..Data Structures and Algorithms Made Easy Introduction Commonly used ADTs : Linked Lists. 1. 1. Disjoint Sets (Union and Find). Do we have oil? i. etc.. pushing an element onto the stack. If no. popping an element from stack. The last element inserted into the stack is the first element that gets deleted. 1. a. Stacks. Priority Queues.. by bus. 2) Get the oil. giving step by step procedure for solving it. we encounter the following types of inputs. 2. finding number of elements in the stack etc.8 What is Running Time Analysis? It is the process of determining how processing time increases as the size of the problem (input size) increases.6 Why Analysis of Algorithms? To go from city to city there can be many ways of accomplishing this: by flight. we will go through many of them and you will be in a position to relate the data structures to the kind of problems they solve. developers effort etc. finding the current top of the stack. Graphs. sorting problem has many algorithms like insertion sort. ii. For example. general steps we follow are: 1) Get the frying pan. Binary Trees. Input size is number of elements in the input and depending on the problem type the input may be of different types. memory. In general.5 What is an Algorithm? Let us consider the problem of preparing an omelet. If yes.. then go out and buy. for a given problem (preparing an omelet). While defining the ADTs do not care about implementation details. Similarly. Different kinds of ADTs are suited to different kinds of applications. Algorithm analysis helps us determining which of them is efficient in terms of time and space consumed.5 What is an Algorithm? 11 . quick sort and many more). Queues. do we want to buy oil? 1. put it in the pan. Depending on the availability and convenience we choose the one which suits us. in computer science there can be multiple algorithms exist for solving the same problem (for example. What we are doing is. Formal definition of an algorithm can be given as: An algorithm is the step-by-step instructions to solve a given problem.7 Goal of Analysis of Algorithms The goal of is to compare algorithms (or solutions) mainly in terms of running time but also in terms of other factors (e.) 1. If yes. 3) Turn on the stove. They come in to picture only when we want to use them.. Dictionaries. For preparing omelet. we can terminate. If no. Common operations of it are: creating the stack. and many other.

cost of car is too big compared to cost of cycle (approximating the cost of cycle to cost of car). Time complexity Name Constant Logarithmic Linear Linear Logarithmic Quadratic Cubic Exponential Example Adding an element to the front of a linked list Finding an element in a sorted array Finding an element in an unsorted array Sorting n items by „divide-and-conquer‟-Mergesort Shortest path between two nodes in a graph Matrix Multiplication The Towers of Hanoi problem Below diagram shows the relationship between different rates of growth. For the above example. is the highest rate of growth.e. Since. This kind of comparison is independent of machine time.10 What is Rate of Growth? The rate at which the running time increases as a function of input is called .. let us define few : as execution times are specific to a particular computer. Let us assume that you went to a shop for buying a car and a cycle. 1. we can represent the cost of car and cost of cycle in terms of function and for a given function ignore the low order terms that are relatively insignificant (for large value of input size. programming style. since the number of statements varies with the programming language as well as the style of the individual programmer. ) and compare these different functions corresponding to running times. ). As an example in the below case. 1. If your friend sees you there and asks what you are buying then in general we say This is because. . 1.9 How to Compare Algorithms? 12 . Execution times? Number of statements executed? . Ideal Solution? Let us assume that we expressed running time of given algorithm as a function of the input size (i.9 How to Compare Algorithms? To compare algorithms.. etc. and are the individual costs of some function and approximate it to .11 Commonly used Rate of Growths Below is the list of rate of growths which come across in remaining chapters.Data Structures and Algorithms Made Easy Size of an array Polynomial degree Number of elements in a matrix Number of bits in binary representation of the input Vertices and edges in a graph Introduction 1. ..

12 Types of Analysis To analyze the given algorithm we need to know on what inputs the algorithm is taking less time (performing well) and on what inputs the algorithm is taking huge time. We have already seen that an algorithm can be represented in the form of an expression.Data Structures and Algorithms Made Easy Introduction D e c r e a s i n g R a t e s O f G r o w t h √ 1.12 Types of Analysis . In general the first case is called the and second case is called the of the algorithm. There are three types of analysis: Worst case o Defines the input for which the algorithm takes huge time. To analyze an algorithm we need some kind of syntax and that forms the base for asymptotic analysis/notation. o Input is the one for which the algorithm runs the slower. That means we represent the algorithm with multiple expressions: one for case where it is taking the less time and other for case where it is taking the more time. Best case 13 1.

1. Rate of Growth Input Size.. In the below figure. o Input is the one for which the algorithm runs the fastest.13 Asymptotic Notation Having the expressions for best. . if is the given algorithm. 1. Average case o Provides a prediction about the running time of the algorithm o Assumes that the input is random Introduction For a given algorithm.13 Asymptotic Notation . at larger values of . O notation defined as O there exist positive constants and such that for all . Let us assume that the given algorithm is represented in the form of function . Below the rate of growths could be different. For example. That means the rate of growth at lower values of is not important. Our objective is to give smallest rate of growth which is greater than or equal to given algorithms rate of growth . is the point from which we need to consider the rate of growths for a given algorithm. it is represented as O . we can represent best. As an example. Let us see the O notation with little more detail. gives the maximum rate of growth for at larger values of . That means. In general. Big-O Visualization O O O is the set of functions with smaller or same order of growth as O etc. Generally. for worst case for best case Similarly. is an asymptotic tight upper bound for . for all the three cases we need to identify the upper and lower bounds. for average case too. worst and average cases in the form of expressions. then is . let be the function which represents the given algorithm. That means. The expression defines the inputs with which the algorithm takes the average running time (or memory). average case and worst cases. In order to represent these upper and lower bounds we need some kind syntax and that is the subject of following discussion.14 Big-O Notation This notation gives the upper bound of the given function.Data Structures and Algorithms Made Easy o Defines the input for which the algorithm takes lowest time. we discard lower values of . O includes 14 1. For example. the upper bound of is .

For this function there are multiple and values possible. Big-O Examples Example-1 Find upper bound for Solution: for all ∴ =O with c = 4 and Example-2 Find upper bound for Solution: for all ∴ =O with Example-3 Find upper bound for Solution: ∴ =O and for all with and Example-4 Find upper bound for Solution: for all ∴ =O with Example-5 Find upper bound for Solution: for all ∴ =O with and Example-6 Find upper bound for Solution: for all ∴ O with and and No Uniqueness? There are no unique set of values for and in proving the asymptotic bounds. O 1. . below Introduction we do not care for rate of O .14 Big-O Notation 15 . What this means is. Solution1: Solution2: for all for all and and is a solution. O O only. is also a solution.Data Structures and Algorithms Made Easy Note: Analyze the algorithms at larger values of growth. . O . Let us consider.