SlideShare a Scribd company logo
1 of 15
DATA STRUCTURES
        AND
  ALGORITHMS
MADE EASY IN JAVA


  -To All My Readers
Copyright ©           by

                                          All rights reserved.

                             Designed by




Copyright ©2012 CareerMonk Publications. All rights reserved.

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

I would like to express my gratitude to many people who saw me through this book, to all those who provided
support, talked things over, read, wrote, offered comments, allowed me to quote their remarks and assisted in
the editing, proofreading and design. In particular, I would like to thank the following individuals.

                        , for encouraging me when I was at             . He is the first person who taught me
the importance of               and its      by giving good books on them.

                        [                                           ], for his encouragement during my stay at
             .

                    [                ,            ],                    [       ,               ], . . .
         [                       ,       ,                      ],            [          ,             ] and
                        [            ] for spending time in reviewing, testing code and for providing me the
valuable suggestions.

                                  [               ],                               [          ,             ] for
spending their valuable time in reviewing the book, suggestions and encouragement.

       &          [Founders of                         ], for approaching me for teaching Data Structures and
Algorithms at their training centers. They are the primary reason for initiation of this book.

             ,            and            have contributed greatly to the quality of this book. I thank all of you
for your help and suggestions.

Last but not least, I would like to thank               of                          ,
         &                      [                              ],               [                 of           ],
                                                [                      ,                ],
[   ],                               [              ,          ]&                            for helping me and
my family during our studies.

                                                                                        -
                                                                                           M-Tech,
                                                                                    Founder of
A stone is broken by the last stroke. This does not mean that first stroke was useless.
             Success is a result of continuous daily effort. --

Stand up, be bold, be strong. Take the whole responsibility on your own shoulders,
 and know that you are the creator of your own destiny. --
Preface
Dear Reader,

Please Hold on! I know many people do not read preface. But I would like to strongly recommend reading
preface of this book at least. This preface has               from regular prefaces.

Main objective of the book is not to give you the theorems and proofs about                   and               .
I have followed a pattern of improving the problem solutions with different complexities (for each problem,
you observe multiple solutions with different improved complexities). Basically, it‟s an enumeration of possible
solutions. With this approach, even if we get a new question it gives us a way to think about all possible
solutions. This book is very much useful for interview preparation, competitive exams preparation, campus
preparations.

As a              if you read complete book with good understanding, I am sure you will challenge the
interviewers and that is the objective of this book.

If you read as an              , 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 book is very much useful for the              of                        and          during their academic
preparations. All the chapters of this book contain theory and their related problems as many as possible. There
a total of approximately     algorithmic puzzles and all of them are with solutions.

If you read as a         preparing for competition exams for Computer Science/Information Technology], the
content of this book covers    the           topics in full details. While writing the book, an intense care has
been taken to help students who are preparing for these kinds of exams.

In all the chapters you will see more importance given to problems and analyzing them instead of concentrating
more on theory. For each chapter, first you will see the basic required theory and then followed by problems.

For many of the problems,              solutions are provided with different complexities. We start with
             solution and slowly move towards the              possible for that problem. For each problem
we will try to understand how much time the algorithm is taking and how much memory the algorithm is
taking.

It is                that, at least one complete reading of this book is required to get full understanding of all
the topics. In the subsequent readings, you can directly go to any chapter and refer. Even though, enough
readings were given for correcting the errors, due to human tendency there could be some minor typos in the
book. If any such typos found, they will be updated at                              . I request you to constantly
monitor this site for any corrections, new problems and solutions. Also, please provide your valuable
suggestions at:                           .

                                   Wish you all the best. Have a nice reading.

                                                                                      -
                                                                                          M-Tech,
                                                                                   Founder of
Other Titles by Narasimha Karumanchi
         Success keys for Big Job Hunters

  Data Structures and Algorithms for GATE

  Data Structures and Algorithms Made Easy (C/C++)
  Coding Interview Questions
  Peeling Design Patterns
Table of Contents
1. Introduction ----------------------------------------------------------------------- 9

2. Recursion and Backtracking ------------------------------------------------------32

3. Linked Lists -----------------------------------------------------------------------37

4. Stacks ------------------------------------------------------------------------------72

5. Queues ----------------------------------------------------------------------------97

6. Trees ----------------------------------------------------------------------------- 107

7. Priority Queue and Heaps ------------------------------------------------------ 175

8. Disjoint Sets ADT --------------------------------------------------------------- 193

9. Graph Algorithms -------------------------------------------------------------- 202

10. Sorting--------------------------------------------------------------------------- 247

11. Searching ------------------------------------------------------------------------ 270

12. Selection Algorithms [Medians] ------------------------------------------------ 295

13. Symbol Tables------------------------------------------------------------------- 304

14. Hashing ------------------------------------------------------------------------- 306

15. String Algorithms --------------------------------------------------------------- 322

16. Algorithms Design Techniques ------------------------------------------------ 346

17. Greedy Algorithms ------------------------------------------------------------- 349

18. Divide and Conquer Algorithms ----------------------------------------------- 359

19. Dynamic Programming --------------------------------------------------------- 373

20. Complexity Classes ------------------------------------------------------------- 407

21. Miscellaneous Concepts -------------------------------------------------------- 414
Data Structures and Algorithms Made Easy in                                                              Introduction



                                                                                              Chapter-1
                                INTRODUCTION


The objective of this chapter is to explain the importance of analysis of algorithms, their notations, relationships and
solving as many problems as possible. We first concentrate on understanding the basic elements of algorithms,
importance of analysis and then slowly move towards analyzing the algorithms with different notations and finally the
problems. After completion of this chapter you should be able to find the complexity of any given algorithm
(especially recursive functions).

1.1 Variables
Before going to the definition of variables, let us relate them to old mathematical equations. All of us have solved
many mathematical equations since childhood. As an example, consider the below equation:


We don‟t have to worry about the use of above equation. The important thing that we need to understand is, the
equation has some names ( and ) which hold values (data). That means, the         ( and ) are the place holders
for representing data. Similarly, in computer science we need something for holding data and            are the
facility for doing that.

1.2 Data types
In the above equation, the variables and can take any values like integral numbers ( ,      etc...), real numbers
(         etc…) or just and . To solve the equation, we need to relate them to kind of values they can take and
           is the name being used in computer science for this purpose.
A             in a programming language is a set of data with values having predefined characteristics. Examples of
data types are: integer, floating point unit number, character, string etc...
Computer memory is all filled with zeros and ones. If we have a problem and wanted to code it, it‟s very difficult to
provide the solution in terms of zeros and ones. To help users, programming languages and compilers are providing the
facility of data types.
For example,       takes bytes (actual value depends on compiler),           takes bytes etc… This says that, in memory
we are combining bytes ( bits) and calling it as               . Similarly, combining bytes ( bits) and calling it as
       . A data type reduces the coding effort. Basically, at the top level, 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. The primitive data types which are provided
by many programming languages are: Boolean, byte, short, int, float, double, etc…The number of bits allocated for
each primitive data type depends on the programming languages, compiler and operating system. For the same
primitive data type, different languages may use different sizes. Depending on the size of the data types the total
available values (domain) will also changes.
For example, “    ” may take bytes or bytes. If it takes bytes ( bits) then the total possible values are -          to
+       (-           -1). If it takes,    bytes (    bits), then the possible values are between                     to
                 (-         -1). Same is the case with remaining data types too.

1.1 Variables                                                                                                         9
Data Structures and Algorithms Made Easy in                                                                  Introduction


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. Good example of user defined data types are: structures in            and
classes in      . For example, in the below case, we are combining many system defined data types and called it as user
defined data type with name “             ”. This gives more flexibility and comfort in dealing with computer memory.
          public class newType {
                   public int data1;
                   public int data 2;
                   private float data3;
                   …
                   private char data;
                   //Operations
          }

1.3 Data Structure
Based on the above discussion, once we have data in variables, we need some mechanism for manipulating that data to
solve problems.                    is a particular way of storing and organizing data in a computer so that it can be used
efficiently. That means, a                       is a specialized format for organizing and storing data. General data
structure types include arrays, files, linked lists, stacks, queues, trees, graphs and so on. Depending on the organization
of the elements, 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, Linked Lists).              : Linked Lists, Stacks and Queues.
     2)                                    : Elements of this data structure are stored/accessed in a non-linear order.
                    : Trees and graphs.

1.4 Abstract Data Types (ADTs)
Before defining abstract data types, let us consider the different view of system defined data types. We all know that,
by default, all primitive data types (int, float, et..) supports basic operations like addition, subtraction etc… The system
is providing the implementations for the primitive data types. For user defined data types also we need to define
operations. The implementation for these operations can be done when we want to actually use them. That means, in
general user defined data types are defined along with their operations.
To simplify the process of solving the problems, we generally combine the data structures along with their operations
and are called                        (ADTs). An ADT consists of    parts:
         1. Declaration of data
         2. Declaration of operations
Commonly used ADTs               : Linked Lists, Stacks, Queues, Priority Queues, Binary Trees, Dictionaries, Disjoint Sets
(Union and Find), Hash Tables, Graphs, and many other. For example, stack uses LIFO (Last-In-First-Out) mechanism
while storing the data in data structures. The last element inserted into the stack is the first element that gets deleted.
Common operations of it are: creating the stack, pushing an element onto the stack, popping an element from stack,
finding the current top of the stack, finding number of elements in the stack etc...
While defining the ADTs do not care about implementation details. They come in to picture only when we want to
use them. Different kinds of ADTs are suited to different kinds of applications, and some are highly specialized to
specific tasks. By the end of this book, 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.

1.5 What is an Algorithm?
Let us consider the problem of preparing an omelet. For preparing omelet, general steps we follow are:

1.3 Data Structure                                                                                                       10
Data Structures and Algorithms Made Easy in                                                             Introduction

    1) Get the frying pan.
    2) Get the oil.
           a. Do we have oil?
                     i. If yes, put it in the pan.
                    ii. If no, do we want to buy oil?
                             1. If yes, then go out and buy.
                             2. If no, we can terminate.
    3) Turn on the stove, etc...
What we are doing is, for a given problem (preparing an omelet), giving step by step procedure for solving it. Formal
definition of an algorithm can be given as:
                         An algorithm is the step-by-step instructions to solve a given problem.
Note: we do not have to prove each step of the algorithm.

1.6 Why Analysis of Algorithms?
To go from city      to city     there can be many ways of accomplishing this: by flight, by bus, by train and also by
cycle. Depending on the availability and convenience we choose the one which suits us. Similarly, in computer science
there can be multiple algorithms exist for solving the same problem (for example, sorting problem has many
algorithms like insertion sort, selection sort, quick sort and many more). Algorithm analysis helps us determining
which of them is efficient in terms of time and space consumed.

1.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.g., memory, developers effort etc.)

1.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. Input
size is number of elements in the input and depending on the problem type the input may be of different types. In
general, we encounter the following types of inputs.
       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

1.9 How to Compare Algorithms?
To compare algorithms, let us define few                       .
Execution times?                          as execution times are specific to a particular computer.
Number of statements executed?                                 , since the number of statements varies with the
programming language as well as the style of the individual programmer.
Ideal Solution? Let us assume that we expressed running time of given algorithm as a function of the input size
(i.e.,   ) and compare these different functions corresponding to running times. This kind of comparison is
independent of machine time, programming style, etc...

1.10 What is Rate of Growth?
The rate at which the running time increases as a function of input is called                  . Let us assume that you
went to a shop for buying a car and a cycle. If your friend sees you there and asks what you are buying then in general

1.6 Why Analysis of Algorithms?                                                                                     11
Data Structures and Algorithms Made Easy in                                                              Introduction

we say                This is because, cost of car is too big compared to cost of cycle (approximating the cost of cycle
to cost of car).


For the above example, 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, ). As an example in the
below case, ,       ,    and      are the individual costs of some function and approximate it to . Since,        is the
highest rate of growth.



1.11 Commonly used Rate of Growths
Below diagram shows the relationship between different rates of growth.



                                                                                            D
                                                                                            e
                                                                                            c
                                                                                            r
                                                                                            e
                                                                                            a
                                                                                            s
                                                                                            i
                                                                                            n
                                                                                            g


                                                                                            R
                                                                                            a
                                                                                            t
                                                                                            e
                                                                                            s

                                                                                            O
                                                                                            f

                                                                                            G
                                                                                            r
                                                                                            o
                                                                                            w
                                                                                            t
                                                                                            h
                                                 √




1.11 Commonly used Rate of Growths                                                                                   12
Data Structures and Algorithms Made Easy in                                                                Introduction

Below is the list of rate of growths which come across in remaining chapters.
             Time complexity    Name                   Example
                                Constant               Adding an element to the front of a linked list
                                Logarithmic            Finding an element in a sorted array
                                Linear                 Finding an element in an unsorted array
                                Linear Logarithmic     Sorting n items by „divide-and-conquer‟-Mergesort
                                Quadratic              Shortest path between two nodes in a graph
                                Cubic                  Matrix Multiplication
                                Exponential            The Towers of Hanoi problem

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. 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. In general the first case is called the              and second case is called the                 of the
algorithm. To analyze an algorithm we need some kind of syntax and that forms the base for asymptotic
analysis/notation. There are three types of analysis:
 Worst case
         o Defines the input for which the algorithm takes huge time.
         o Input is the one for which the algorithm runs the slower.
 Best case
         o Defines the input for which the algorithm takes lowest time.
         o Input is the one for which the algorithm runs the fastest.
 Average case
         o Provides a prediction about the running time of the algorithm
         o Assumes that the input is random

For a given algorithm, we can represent best, worst and average cases in the form of expressions. As an example, let
     be the function which represents the given algorithm.
                             , for worst case
                                       for best case
Similarly, for average case too. The expression defines the inputs with which the algorithm takes the average running
time (or memory).

1.13 Asymptotic Notation
Having the expressions for best, average case and worst cases, for all the three cases we need to identify the upper and
lower bounds. In order to represent these upper and lower bounds we need some kind syntax and that is the subject of
following discussion. Let us assume that the given algorithm is represented in the form of function       .

1.14 Big-O Notation
This notation gives the       upper bound of the given function. Generally, it is represented as           O      . That
means, at larger values of , the upper bound of      is    .
For example, if                                     is the given algorithm, then    is     . That means,       gives the
maximum rate of growth for        at larger values of .

1.12 Types of Analysis                                                                                               13
Data Structures and Algorithms Made Easy in                                                              Introduction


Let us see the O notation with little more detail. O notation defined as O                       there exist positive
constants and      such that                     for all         .      is an asymptotic tight upper bound for      .
Our objective is to give smallest rate of growth         which is greater than or equal to given algorithms rate of
growth      .
In general, we discard lower values of . That means the rate of growth at lower values of is not important. In the
below figure,    is the point from which we need to consider the rate of growths for a given algorithm. Below  the
rate of growths could be different.
                        Rate of Growth




                                                                               Input Size,

Big-O Visualization
O         is the set of functions with smaller or same order of growth as               For example, O         includes
O     O      O         etc..
Note: Analyze the algorithms at larger values of     only. What this means is, below         we do not care for rate of
growth.
                            O                                                       O




                            O                                                     O

                                    ,    .                                                   ,   .



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           and
Example-3 Find upper bound for
Solution:                          for all
        ∴                 =O         with          and
Example-4 Find upper bound for
1.14 Big-O Notation                                                                                                 14
Data Structures and Algorithms Made Easy in                                                                       Introduction

Solution:                          for all
        ∴                =O             with       and
Example-5 Find upper bound for
Solution:      for all
        ∴ =O       with       and
Example-6 Find upper bound for
Solution:            for all
        ∴       O      with                  and

No Uniqueness?
There are no unique set of values for   and in proving the asymptotic bounds. Let us consider,                           O
For this function there are multiple  and values possible.
Solution1:                                               for all              and           is a solution.
Solution2:                                                 for all           and            is also a solution.

1.15 Omega-Ω Notation
Similar to O discussion, this notation gives the tighter lower bound of the given algorithm and we represent it
as                 . That means, at larger values of , the tighter lower bound of        is     For example, if
                            ,     is

                          Rate of Growth




                                                                              Input Size,

The    notation can be defined as                        there exist positive constants and         such that
      for all                is an asymptotic tight lower bound for          Our objective is to give largest rate of growth
      which is less than or equal to given algorithms rate of growth       .

Ω Examples
Example-1 Find lower bound for
Solution:       Such that:                                           and     =1
        ∴              with                  and   =1
Example-2 Prove
Solution:     Such that:

                                     –
             Since   is positive         –

1.15 Omega-Ω Notation                                                                                                        15

More Related Content

What's hot

Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsMohamed Loey
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiManishPrajapati78
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sortDistrict Administration
 
Artificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gameArtificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gamemanika kumari
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithmNeoClassical
 
Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Venkata Reddy Konasani
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structuresKuber Chandra
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data AnalysisAndrew Henshaw
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Intro to deep learning
Intro to deep learning Intro to deep learning
Intro to deep learning David Voyles
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahniHitesh Wagle
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginnersRam Sagar Mourya
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First SearchKevin Jadiya
 

What's hot (20)

Algorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching AlgorithmsAlgorithms Lecture 6: Searching Algorithms
Algorithms Lecture 6: Searching Algorithms
 
Data Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of HanoiData Structure and Algorithms The Tower of Hanoi
Data Structure and Algorithms The Tower of Hanoi
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
 
Artificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe gameArtificial Intelligence- TicTacToe game
Artificial Intelligence- TicTacToe game
 
Python programming : Strings
Python programming : StringsPython programming : Strings
Python programming : Strings
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
 
Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science Machine Learning Deep Learning AI and Data Science
Machine Learning Deep Learning AI and Data Science
 
Linear Search Presentation
Linear Search PresentationLinear Search Presentation
Linear Search Presentation
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Presentation on Elementary data structures
Presentation on Elementary data structuresPresentation on Elementary data structures
Presentation on Elementary data structures
 
pandas - Python Data Analysis
pandas - Python Data Analysispandas - Python Data Analysis
pandas - Python Data Analysis
 
List in Python
List in PythonList in Python
List in Python
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
8 queen problem
8 queen problem8 queen problem
8 queen problem
 
Intro to deep learning
Intro to deep learning Intro to deep learning
Intro to deep learning
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 

Similar to Data structures and algorithms made easy java

Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...CareerMonk Publications
 
Datastructuresandalgorithms
DatastructuresandalgorithmsDatastructuresandalgorithms
DatastructuresandalgorithmsNiharika Sahay
 
Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easyCareerMonk Publications
 
Table of contents [data structure and algorithmic thinking with python]
Table of contents [data structure and algorithmic thinking with python]Table of contents [data structure and algorithmic thinking with python]
Table of contents [data structure and algorithmic thinking with python]CareerMonk Publications
 
Sample chapters [data structure and algorithmic thinking with python]
Sample chapters [data structure and algorithmic thinking with python]Sample chapters [data structure and algorithmic thinking with python]
Sample chapters [data structure and algorithmic thinking with python]CareerMonk Publications
 
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdfData Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdfAbdurraufSharifaiGar
 
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...CareerMonk Publications
 
SaltStack For DevOps, Free Sample
SaltStack For DevOps, Free SampleSaltStack For DevOps, Free Sample
SaltStack For DevOps, Free SampleAymen EL Amri
 
Reverse Engineering
Reverse EngineeringReverse Engineering
Reverse Engineeringdswanson
 
002 One Page Essay What Is The Mla Format For Ess
002 One Page Essay What Is The Mla Format For Ess002 One Page Essay What Is The Mla Format For Ess
002 One Page Essay What Is The Mla Format For EssAngela Hays
 
avgptsmathwebquest
avgptsmathwebquestavgptsmathwebquest
avgptsmathwebquestdeniaray
 
Webquestpowerpoint 120223205354-phpapp01
Webquestpowerpoint 120223205354-phpapp01Webquestpowerpoint 120223205354-phpapp01
Webquestpowerpoint 120223205354-phpapp01Ad Meskens
 
Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)CareerMonk Publications
 
Culp webquest
Culp webquestCulp webquest
Culp webquestcc86
 
Viewing assessment through different coloured glasses: Authentic, collaborati...
Viewing assessment through different coloured glasses: Authentic, collaborati...Viewing assessment through different coloured glasses: Authentic, collaborati...
Viewing assessment through different coloured glasses: Authentic, collaborati...Charles Darwin University
 
Pretend powerpoint
Pretend powerpointPretend powerpoint
Pretend powerpointNSchultz215
 
Taxonomy: a powerful magnifier with a harsh lens
Taxonomy: a powerful magnifier with a harsh lensTaxonomy: a powerful magnifier with a harsh lens
Taxonomy: a powerful magnifier with a harsh lensJoe Pairman
 

Similar to Data structures and algorithms made easy java (20)

Coding interview questions
Coding interview questionsCoding interview questions
Coding interview questions
 
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...
Data Structures and Algorithms For GATE: Solutions to all previous GATE quest...
 
Datastructuresandalgorithms
DatastructuresandalgorithmsDatastructuresandalgorithms
Datastructuresandalgorithms
 
Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easy
 
Table of contents [data structure and algorithmic thinking with python]
Table of contents [data structure and algorithmic thinking with python]Table of contents [data structure and algorithmic thinking with python]
Table of contents [data structure and algorithmic thinking with python]
 
Sample chapters [data structure and algorithmic thinking with python]
Sample chapters [data structure and algorithmic thinking with python]Sample chapters [data structure and algorithmic thinking with python]
Sample chapters [data structure and algorithmic thinking with python]
 
Peeling design patterns
Peeling design patternsPeeling design patterns
Peeling design patterns
 
Peeling Design Patterns
Peeling Design PatternsPeeling Design Patterns
Peeling Design Patterns
 
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdfData Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
Data Structures and Algorithms Made Easy in Java ( PDFDrive ).pdf
 
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...
Elements of Computer Networking: An Integrated Approach (Concepts, Problems a...
 
SaltStack For DevOps, Free Sample
SaltStack For DevOps, Free SampleSaltStack For DevOps, Free Sample
SaltStack For DevOps, Free Sample
 
Reverse Engineering
Reverse EngineeringReverse Engineering
Reverse Engineering
 
002 One Page Essay What Is The Mla Format For Ess
002 One Page Essay What Is The Mla Format For Ess002 One Page Essay What Is The Mla Format For Ess
002 One Page Essay What Is The Mla Format For Ess
 
avgptsmathwebquest
avgptsmathwebquestavgptsmathwebquest
avgptsmathwebquest
 
Webquestpowerpoint 120223205354-phpapp01
Webquestpowerpoint 120223205354-phpapp01Webquestpowerpoint 120223205354-phpapp01
Webquestpowerpoint 120223205354-phpapp01
 
Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)Data Structures and Algorithms made Easy (Cover Page)
Data Structures and Algorithms made Easy (Cover Page)
 
Culp webquest
Culp webquestCulp webquest
Culp webquest
 
Viewing assessment through different coloured glasses: Authentic, collaborati...
Viewing assessment through different coloured glasses: Authentic, collaborati...Viewing assessment through different coloured glasses: Authentic, collaborati...
Viewing assessment through different coloured glasses: Authentic, collaborati...
 
Pretend powerpoint
Pretend powerpointPretend powerpoint
Pretend powerpoint
 
Taxonomy: a powerful magnifier with a harsh lens
Taxonomy: a powerful magnifier with a harsh lensTaxonomy: a powerful magnifier with a harsh lens
Taxonomy: a powerful magnifier with a harsh lens
 

Recently uploaded

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 

Recently uploaded (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

Data structures and algorithms made easy java

  • 1. DATA STRUCTURES AND ALGORITHMS MADE EASY IN JAVA -To All My Readers
  • 2. Copyright © by All rights reserved. Designed by Copyright ©2012 CareerMonk Publications. All rights reserved. All rights reserved. No part of this book may be reproduced in any form or by any electronic or mechanical means, including information storage and retrieval systems, without written permission from the publisher or author.
  • 3. Acknowledgements I would like to express my gratitude to many people who saw me through this book, to all those who provided support, talked things over, read, wrote, offered comments, allowed me to quote their remarks and assisted in the editing, proofreading and design. In particular, I would like to thank the following individuals. , for encouraging me when I was at . He is the first person who taught me the importance of and its by giving good books on them. [ ], for his encouragement during my stay at . [ , ], [ , ], . . . [ , , ], [ , ] and [ ] for spending time in reviewing, testing code and for providing me the valuable suggestions. [ ], [ , ] for spending their valuable time in reviewing the book, suggestions and encouragement. & [Founders of ], for approaching me for teaching Data Structures and Algorithms at their training centers. They are the primary reason for initiation of this book. , and have contributed greatly to the quality of this book. I thank all of you for your help and suggestions. Last but not least, I would like to thank of , & [ ], [ of ], [ , ], [ ], [ , ]& for helping me and my family during our studies. - M-Tech, Founder of
  • 4. A stone is broken by the last stroke. This does not mean that first stroke was useless. Success is a result of continuous daily effort. -- Stand up, be bold, be strong. Take the whole responsibility on your own shoulders, and know that you are the creator of your own destiny. --
  • 5. Preface Dear Reader, Please Hold on! I know many people do not read preface. But I would like to strongly recommend reading preface of this book at least. This preface has from regular prefaces. Main objective of the book is not to give you the theorems and proofs about and . I have followed a pattern of improving the problem solutions with different complexities (for each problem, you observe multiple solutions with different improved complexities). Basically, it‟s an enumeration of possible solutions. With this approach, even if we get a new question it gives us a way to think about all possible solutions. This book is very much useful for interview preparation, competitive exams preparation, campus preparations. As a if you read complete book with good understanding, I am sure you will challenge the interviewers and that is the objective of this book. If you read as an , 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 book is very much useful for the of and during their academic preparations. All the chapters of this book contain theory and their related problems as many as possible. There a total of approximately algorithmic puzzles and all of them are with solutions. If you read as a preparing for competition exams for Computer Science/Information Technology], the content of this book covers the topics in full details. While writing the book, an intense care has been taken to help students who are preparing for these kinds of exams. In all the chapters you will see more importance given to problems and analyzing them instead of concentrating more on theory. For each chapter, first you will see the basic required theory and then followed by problems. For many of the problems, solutions are provided with different complexities. We start with solution and slowly move towards the possible for that problem. For each problem we will try to understand how much time the algorithm is taking and how much memory the algorithm is taking. It is that, at least one complete reading of this book is required to get full understanding of all the topics. In the subsequent readings, you can directly go to any chapter and refer. Even though, enough readings were given for correcting the errors, due to human tendency there could be some minor typos in the book. If any such typos found, they will be updated at . I request you to constantly monitor this site for any corrections, new problems and solutions. Also, please provide your valuable suggestions at: . Wish you all the best. Have a nice reading. - M-Tech, Founder of
  • 6. Other Titles by Narasimha Karumanchi Success keys for Big Job Hunters Data Structures and Algorithms for GATE Data Structures and Algorithms Made Easy (C/C++) Coding Interview Questions Peeling Design Patterns
  • 7. Table of Contents 1. Introduction ----------------------------------------------------------------------- 9 2. Recursion and Backtracking ------------------------------------------------------32 3. Linked Lists -----------------------------------------------------------------------37 4. Stacks ------------------------------------------------------------------------------72 5. Queues ----------------------------------------------------------------------------97 6. Trees ----------------------------------------------------------------------------- 107 7. Priority Queue and Heaps ------------------------------------------------------ 175 8. Disjoint Sets ADT --------------------------------------------------------------- 193 9. Graph Algorithms -------------------------------------------------------------- 202 10. Sorting--------------------------------------------------------------------------- 247 11. Searching ------------------------------------------------------------------------ 270 12. Selection Algorithms [Medians] ------------------------------------------------ 295 13. Symbol Tables------------------------------------------------------------------- 304 14. Hashing ------------------------------------------------------------------------- 306 15. String Algorithms --------------------------------------------------------------- 322 16. Algorithms Design Techniques ------------------------------------------------ 346 17. Greedy Algorithms ------------------------------------------------------------- 349 18. Divide and Conquer Algorithms ----------------------------------------------- 359 19. Dynamic Programming --------------------------------------------------------- 373 20. Complexity Classes ------------------------------------------------------------- 407 21. Miscellaneous Concepts -------------------------------------------------------- 414
  • 8.
  • 9. Data Structures and Algorithms Made Easy in Introduction Chapter-1 INTRODUCTION The objective of this chapter is to explain the importance of analysis of algorithms, their notations, relationships and solving as many problems as possible. We first concentrate on understanding the basic elements of algorithms, importance of analysis and then slowly move towards analyzing the algorithms with different notations and finally the problems. After completion of this chapter you should be able to find the complexity of any given algorithm (especially recursive functions). 1.1 Variables Before going to the definition of variables, let us relate them to old mathematical equations. All of us have solved many mathematical equations since childhood. As an example, consider the below equation: We don‟t have to worry about the use of above equation. The important thing that we need to understand is, the equation has some names ( and ) which hold values (data). That means, the ( and ) are the place holders for representing data. Similarly, in computer science we need something for holding data and are the facility for doing that. 1.2 Data types In the above equation, the variables and can take any values like integral numbers ( , etc...), real numbers ( etc…) or just and . To solve the equation, we need to relate them to kind of values they can take and is the name being used in computer science for this purpose. A in a programming language is a set of data with values having predefined characteristics. Examples of data types are: integer, floating point unit number, character, string etc... Computer memory is all filled with zeros and ones. If we have a problem and wanted to code it, it‟s very difficult to provide the solution in terms of zeros and ones. To help users, programming languages and compilers are providing the facility of data types. For example, takes bytes (actual value depends on compiler), takes bytes etc… This says that, in memory we are combining bytes ( bits) and calling it as . Similarly, combining bytes ( bits) and calling it as . A data type reduces the coding effort. Basically, at the top level, 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. The primitive data types which are provided by many programming languages are: Boolean, byte, short, int, float, double, etc…The number of bits allocated for each primitive data type depends on the programming languages, compiler and operating system. For the same primitive data type, different languages may use different sizes. Depending on the size of the data types the total available values (domain) will also changes. For example, “ ” may take bytes or bytes. If it takes bytes ( bits) then the total possible values are - to + (- -1). If it takes, bytes ( bits), then the possible values are between to (- -1). Same is the case with remaining data types too. 1.1 Variables 9
  • 10. Data Structures and Algorithms Made Easy in Introduction 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. Good example of user defined data types are: structures in and classes in . For example, in the below case, we are combining many system defined data types and called it as user defined data type with name “ ”. This gives more flexibility and comfort in dealing with computer memory. public class newType { public int data1; public int data 2; private float data3; … private char data; //Operations } 1.3 Data Structure Based on the above discussion, once we have data in variables, we need some mechanism for manipulating that data to solve problems. is a particular way of storing and organizing data in a computer so that it can be used efficiently. That means, a is a specialized format for organizing and storing data. General data structure types include arrays, files, linked lists, stacks, queues, trees, graphs and so on. Depending on the organization of the elements, 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, Linked Lists). : Linked Lists, Stacks and Queues. 2) : Elements of this data structure are stored/accessed in a non-linear order. : Trees and graphs. 1.4 Abstract Data Types (ADTs) Before defining abstract data types, let us consider the different view of system defined data types. We all know that, by default, all primitive data types (int, float, et..) supports basic operations like addition, subtraction etc… The system is providing the implementations for the primitive data types. For user defined data types also we need to define operations. The implementation for these operations can be done when we want to actually use them. That means, in general user defined data types are defined along with their operations. To simplify the process of solving the problems, we generally combine the data structures along with their operations and are called (ADTs). An ADT consists of parts: 1. Declaration of data 2. Declaration of operations Commonly used ADTs : Linked Lists, Stacks, Queues, Priority Queues, Binary Trees, Dictionaries, Disjoint Sets (Union and Find), Hash Tables, Graphs, and many other. For example, stack uses LIFO (Last-In-First-Out) mechanism while storing the data in data structures. The last element inserted into the stack is the first element that gets deleted. Common operations of it are: creating the stack, pushing an element onto the stack, popping an element from stack, finding the current top of the stack, finding number of elements in the stack etc... While defining the ADTs do not care about implementation details. They come in to picture only when we want to use them. Different kinds of ADTs are suited to different kinds of applications, and some are highly specialized to specific tasks. By the end of this book, 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. 1.5 What is an Algorithm? Let us consider the problem of preparing an omelet. For preparing omelet, general steps we follow are: 1.3 Data Structure 10
  • 11. Data Structures and Algorithms Made Easy in Introduction 1) Get the frying pan. 2) Get the oil. a. Do we have oil? i. If yes, put it in the pan. ii. If no, do we want to buy oil? 1. If yes, then go out and buy. 2. If no, we can terminate. 3) Turn on the stove, etc... What we are doing is, for a given problem (preparing an omelet), giving step by step procedure for solving it. Formal definition of an algorithm can be given as: An algorithm is the step-by-step instructions to solve a given problem. Note: we do not have to prove each step of the algorithm. 1.6 Why Analysis of Algorithms? To go from city to city there can be many ways of accomplishing this: by flight, by bus, by train and also by cycle. Depending on the availability and convenience we choose the one which suits us. Similarly, in computer science there can be multiple algorithms exist for solving the same problem (for example, sorting problem has many algorithms like insertion sort, selection sort, quick sort and many more). Algorithm analysis helps us determining which of them is efficient in terms of time and space consumed. 1.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.g., memory, developers effort etc.) 1.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. Input size is number of elements in the input and depending on the problem type the input may be of different types. In general, we encounter the following types of inputs.  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 1.9 How to Compare Algorithms? To compare algorithms, let us define few . Execution times? as execution times are specific to a particular computer. Number of statements executed? , since the number of statements varies with the programming language as well as the style of the individual programmer. Ideal Solution? Let us assume that we expressed running time of given algorithm as a function of the input size (i.e., ) and compare these different functions corresponding to running times. This kind of comparison is independent of machine time, programming style, etc... 1.10 What is Rate of Growth? The rate at which the running time increases as a function of input is called . Let us assume that you went to a shop for buying a car and a cycle. If your friend sees you there and asks what you are buying then in general 1.6 Why Analysis of Algorithms? 11
  • 12. Data Structures and Algorithms Made Easy in Introduction we say This is because, cost of car is too big compared to cost of cycle (approximating the cost of cycle to cost of car). For the above example, 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, ). As an example in the below case, , , and are the individual costs of some function and approximate it to . Since, is the highest rate of growth. 1.11 Commonly used Rate of Growths Below diagram shows the relationship between different rates of growth. D e c r e a s i n g R a t e s O f G r o w t h √ 1.11 Commonly used Rate of Growths 12
  • 13. Data Structures and Algorithms Made Easy in Introduction Below is the list of rate of growths which come across in remaining chapters. Time complexity Name Example Constant Adding an element to the front of a linked list Logarithmic Finding an element in a sorted array Linear Finding an element in an unsorted array Linear Logarithmic Sorting n items by „divide-and-conquer‟-Mergesort Quadratic Shortest path between two nodes in a graph Cubic Matrix Multiplication Exponential The Towers of Hanoi problem 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. 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. In general the first case is called the and second case is called the of the algorithm. To analyze an algorithm we need some kind of syntax and that forms the base for asymptotic analysis/notation. There are three types of analysis:  Worst case o Defines the input for which the algorithm takes huge time. o Input is the one for which the algorithm runs the slower.  Best case o Defines the input for which the algorithm takes lowest time. o Input is the one for which the algorithm runs the fastest.  Average case o Provides a prediction about the running time of the algorithm o Assumes that the input is random For a given algorithm, we can represent best, worst and average cases in the form of expressions. As an example, let be the function which represents the given algorithm. , for worst case for best case Similarly, for average case too. The expression defines the inputs with which the algorithm takes the average running time (or memory). 1.13 Asymptotic Notation Having the expressions for best, average case and worst cases, for all the three cases we need to identify the upper and lower bounds. In order to represent these upper and lower bounds we need some kind syntax and that is the subject of following discussion. Let us assume that the given algorithm is represented in the form of function . 1.14 Big-O Notation This notation gives the upper bound of the given function. Generally, it is represented as O . That means, at larger values of , the upper bound of is . For example, if is the given algorithm, then is . That means, gives the maximum rate of growth for at larger values of . 1.12 Types of Analysis 13
  • 14. Data Structures and Algorithms Made Easy in Introduction Let us see the O notation with little more detail. O notation defined as O there exist positive constants and such that for all . is an asymptotic tight upper bound for . Our objective is to give smallest rate of growth which is greater than or equal to given algorithms rate of growth . In general, we discard lower values of . That means the rate of growth at lower values of is not important. In the below figure, is the point from which we need to consider the rate of growths for a given algorithm. Below the rate of growths could be different. Rate of Growth Input Size, Big-O Visualization O is the set of functions with smaller or same order of growth as For example, O includes O O O etc.. Note: Analyze the algorithms at larger values of only. What this means is, below we do not care for rate of growth. O O O O , . , . 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 and Example-3 Find upper bound for Solution: for all ∴ =O with and Example-4 Find upper bound for 1.14 Big-O Notation 14
  • 15. Data Structures and Algorithms Made Easy in Introduction Solution: for all ∴ =O with and Example-5 Find upper bound for Solution: for all ∴ =O with and Example-6 Find upper bound for Solution: for all ∴ O with and No Uniqueness? There are no unique set of values for and in proving the asymptotic bounds. Let us consider, O For this function there are multiple and values possible. Solution1: for all and is a solution. Solution2: for all and is also a solution. 1.15 Omega-Ω Notation Similar to O discussion, this notation gives the tighter lower bound of the given algorithm and we represent it as . That means, at larger values of , the tighter lower bound of is For example, if , is Rate of Growth Input Size, The notation can be defined as there exist positive constants and such that for all is an asymptotic tight lower bound for Our objective is to give largest rate of growth which is less than or equal to given algorithms rate of growth . Ω Examples Example-1 Find lower bound for Solution: Such that: and =1 ∴ with and =1 Example-2 Prove Solution: Such that: – Since is positive – 1.15 Omega-Ω Notation 15