SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
D A TA S T R U C T U R E S
                           AND
                 ALGORITHMS
                 MADE EASY
                 Success Key for:
                   • Campus Preparation
                   • Degree/Masters Course Preparation
                   • Instructor’s
                   • GATE Preparation
                   • Big Job hunters: Microsoft, Google, Amazon, Yahoo,
                     Facebook, Adobe, IBM Labs and many more
                   • Reference Manual for Working People




                                                     ROOM NUMBER

                                                         M-Tech, IIT Bombay
                                                                     .




CareerMonk Publications
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




     2 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy           Narasimha Karumanchi




                          To My Parents
                              -Laxmi and Modaiah

                          To My Family Members

                          To My Friends

                          To All Hard Workers




     3 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




     4 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                    Narasimha Karumanchi




                        Copyright ©2010 by CareerMonk.com

                                 All rights reserved.

                         Designed by Narasimha Karumanchi

                                  Printed in India




     5 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




     6 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                         Narasimha Karumanchi




                              Acknowledgements

I would like to thank Mohan Mullapudi for encouraging me when I was at IIT Bombay. He is
the first person who taught me the importance of Algorithms and its Design. From that day I
keep on updating myself.

I would like to thank Vamshi [Mentor Graphics] and Kalyani [Xilinx] for spending time in
reviewing this book and providing me the suggestions.

I would like to thank Sobhan [Professor IIT, Hyderabad] for spending his valuable time in
reviewing the book and suggestions. Being a PhD holder in algorithms his review provided
us the confidence in the quality of the book.

I would like to thank Kiran and Laxmi [Founder’s of TheGATEMATE.com] for approaching
me for teaching of Algorithms and Data Structures at their GATE center. They are the
primary reason for initiation of this book.

I would like to thank Timma [Founder of Algorithmica.co.in] for providing me the valuable
suggestions in writing, reviewing and for providing coding solutions to some of the problems.

I would like to thank Director’s of Guntur Vikas College, Director’s of ACE Engineering
Academy, TRC Boss [Former Director of APTransco] and Venkateswara Rao [VNR
Vignanajyothi Engineering College, Hyderabad] for helping me and my family during our
studies.

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

Special thanks should go to my wife, Sailaja for her encouragement and help during writing
of this book.



                                                               - Narasimha Karumanchi




      7 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




     8 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                          Narasimha Karumanchi




                                        Preface


Dear Reader,

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

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

If you read as an instructor, 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 students of Engineering and Masters 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 700 algorithmic problems and
all of them are with solutions.

And finally if you read as a student preparing for competition exams [like Graduate Aptitude
Test for Engineering (GATE), DRDO or any other exam for Computer Science/Information
Technology], then the content of this book covers all the required topics in full details.
While writing the book, an intense care has been taken to ensure that the content should
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, multiple solutions are provided with different complexities. First,
we start with brute force solution and slowly move towards the best solution 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.




      9 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                           Narasimha Karumanchi

It is recommended that, at least one complete reading of this book is required to get full
understanding of all the topics. In the subsequent readings, readers 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 readers to constantly monitor this site for any
corrections, new problems and solutions. Also, please provide your valuable suggestions at:
Info@CareerMonk.com.

Always remember the following quotes by Vivekananda which will give you the full energy
and confidence.

                     Wish you all the best. Have a nice reading.

                                                                   -Narasimha Karumanchi




All power is within you. You can do anything and everything. Believe in that.

                                                                        -Swami Vivekananda

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.

                                                                        -Swami Vivekananda




    10 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                               Narasimha Karumanchi


                                                   Table of Contents


Chapter 1             Introduction .................................................................................................. 33
  Introduction ............................................................................................................................. 33
  Variables ................................................................................................................................... 33
  Data types ................................................................................................................................. 33
     System defined data types (Primitive data types) ............................................................... 34
     User defined data types ........................................................................................................ 34
  Data structure ........................................................................................................................... 35
  Abstract Data Types (ADT’s) ................................................................................................... 35
  Memory and variables.............................................................................................................. 36
     Size of a variable ................................................................................................................... 37
     Address of a variable ............................................................................................................ 37
  Pointers ..................................................................................................................................... 38
     Pointer declaration ............................................................................................................... 38
     Using pointers ....................................................................................................................... 38
     Pointer arithmetic ................................................................................................................ 40
     Arrays and pointers .............................................................................................................. 41
     Dynamic memory allocation................................................................................................ 41
     Function Pointers ................................................................................................................. 42
  Parameter Passing Techniques ................................................................................................ 42
     Actual and Formal Parameters ............................................................................................ 42
     Semantics of Parameter Passing........................................................................................... 43
     Language Support for Parameter Passing Techniques........................................................ 43
     Pass by Value ........................................................................................................................ 44
     Pass by Result........................................................................................................................ 45
     Pass by Value-Result ............................................................................................................ 46
     Pass by reference (aliasing) .................................................................................................. 47
     Pass by name ......................................................................................................................... 48


      11 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                                Narasimha Karumanchi

  Binding...................................................................................................................................... 48
     Binding Times ....................................................................................................................... 49
     Static binding (Early binding).............................................................................................. 49
     Dynamic binding (Late binding) ......................................................................................... 49
  Scope ......................................................................................................................................... 50
     Static scope ............................................................................................................................ 50
     Dynamic scope ...................................................................................................................... 51
  Storage classes ........................................................................................................................... 52
     Auto storage class ................................................................................................................. 52
     Extern storage class............................................................................................................... 53
     Register storage class ............................................................................................................ 59
     Static storage class................................................................................................................. 59
  Storage Organization................................................................................................................ 60
     Static Segment....................................................................................................................... 61
     Stack Segment ....................................................................................................................... 61
     Heap Segment ....................................................................................................................... 64
  Shallow copy versus Deep copy .............................................................................................. 65
Chapter 2              Analysis of Algorithms.................................................................................. 66
  Introduction ............................................................................................................................. 66
  What is algorithm? ................................................................................................................... 66
  Why analysis of algorithms?.................................................................................................... 67
  Goal of analysis of algorithms? ................................................................................................ 67
  What do we mean by running time analysis? ........................................................................ 67
  How do we compare algorithms? ............................................................................................ 67
  What is Rate of Growth? ......................................................................................................... 68
  Commonly used Rate of Growths ........................................................................................... 68
  Types of Analysis...................................................................................................................... 70
  Asymptotic notation? ............................................................................................................... 70
  Big-O notation .......................................................................................................................... 71
     Big-O Visualization .............................................................................................................. 71
     Big-O examples ..................................................................................................................... 72

      12 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

     No uniqueness? ..................................................................................................................... 73
  Omega- notation.................................................................................................................... 73
        Examples............................................................................................................................ 74
  Theta-θ notation....................................................................................................................... 74
     θ Examples ............................................................................................................................ 75
     Important Notes .................................................................................................................... 76
  Why we call Asymptotic Analysis?......................................................................................... 76
  Guidelines for asymptotic analysis? ........................................................................................ 76
  Properties of notations ............................................................................................................. 79
  Commonly used logarithms and summations ........................................................................ 79
     Arithmetic series................................................................................................................... 80
  Master Theorem for Divide and Conquer .............................................................................. 80
     Variant of divide and conquer master theorem .................................................................. 81
  Problems on Master Theorem for Divide and Conquer......................................................... 81
  Master Theorem for subtract and conquer recurrences......................................................... 84
     Variant of subtraction and conquer master theorem ......................................................... 84
  Problems on Algorithms Analysis ........................................................................................... 84
Chapter 3             Recursion and Backtracking ....................................................................... 103
  Introduction ........................................................................................................................... 103
  What is recursion? ................................................................................................................. 103
  Why recursion? ...................................................................................................................... 103
  Format of a recursive function .............................................................................................. 103
  Recursion and Memory (Visualization) ................................................................................ 105
  Recursion versus Iteration ..................................................................................................... 106
     Recursion ............................................................................................................................ 106
     Iteration............................................................................................................................... 106
  Important notes on recursion ................................................................................................ 106
  Example Algorithms of Recursion ........................................................................................ 107
  Problems on recursion ........................................................................................................... 107
  What is backtracking?............................................................................................................ 107
  Important notes on backtracking .......................................................................................... 107

      13 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                          Narasimha Karumanchi

  Example algorithms on backtracking .................................................................................... 108
  Problems on backtracking ..................................................................................................... 108
Chapter 4            Linked Lists.................................................................................................. 113
  What is a Linked List?............................................................................................................ 113
  Linked Lists ADT ................................................................................................................... 113
     Main Linked Lists Operations ............................................................................................ 113
     Auxiliary Linked Lists Operations ..................................................................................... 113
  Why Linked Lists?.................................................................................................................. 113
     Arrays Overview................................................................................................................. 114
     Why constant time for accessing array elements?............................................................ 114
     Advantages of arrays .......................................................................................................... 114
     Disadvantages of arrays ...................................................................................................... 114
     Dynamic Arrays .................................................................................................................. 115
     Advantages of Linked Lists ................................................................................................ 115
     Issues with Linked Lists (Disadvantages) .......................................................................... 115
  Comparison of Linked Lists with Arrays and Dynamic Arrays........................................... 116
  Singly Linked Lists ................................................................................................................. 116
     Basic Operations on a List .................................................................................................. 117
     Traversing the Linked List ................................................................................................. 117
  Singly Linked List Insertion .................................................................................................. 118
     Inserting a node in Singly Linked List at the beginning .................................................. 118
     Inserting a node in Singly Linked List at the ending ....................................................... 118
     Inserting a node in Singly Linked List in the middle ....................................................... 119
  Singly Linked List Deletion ................................................................................................... 121
     Deleting the first node in Singly Linked List .................................................................... 121
     Deleting the last node in Singly Linked List ..................................................................... 122
     Deleting an intermediate node in Singly Linked List....................................................... 122
  Deleting Singly Linked List ................................................................................................... 124
  Doubly Linked Lists ............................................................................................................... 125
  Doubly Linked List Insertion ................................................................................................ 125
     Inserting a node in Doubly Linked List at the beginning ................................................ 126

      14 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

     Inserting a node in Doubly Linked List at the ending ..................................................... 126
     Inserting a node in Doubly Linked List in the middle ..................................................... 127
  Doubly Linked List Deletion ................................................................................................. 129
     Deleting the first node in Doubly Linked List .................................................................. 129
     Deleting the last node in Doubly Linked List ................................................................... 130
     Deleting an intermediate node in Doubly Linked List..................................................... 130
  Circular Linked Lists .............................................................................................................. 132
     Counting nodes in a circular list ........................................................................................ 133
     Printing the contents of a circular list............................................................................... 134
     Inserting a node at the end of a Circular Linked List ....................................................... 134
     Inserting a node at front of a Circular Linked List ........................................................... 136
     Deleting the last node in a circular list ............................................................................. 138
     Deleting the first node in a circular list ............................................................................ 139
     Applications of circular list ................................................................................................ 141
  A Memory-Efficient Doubly Linked List.............................................................................. 141
  Problems on Linked Lists....................................................................................................... 143
Chapter 5             Stacks ........................................................................................................... 168
  What is a stack? ...................................................................................................................... 168
  How are stacks used?.............................................................................................................. 168
  Stack ADT ............................................................................................................................... 169
     Main stack operations......................................................................................................... 169
     Auxiliary stack operations.................................................................................................. 169
     Exceptions ........................................................................................................................... 169
  Applications ............................................................................................................................ 169
  Implementation ...................................................................................................................... 170
     Simple Array Implementation ........................................................................................... 171
     Dynamic Array Implementation ....................................................................................... 173
     Performance ........................................................................................................................ 177
     Linked List Implementation .............................................................................................. 177
     Performance ........................................................................................................................ 179
  Comparison of Implementations ........................................................................................... 179

      15 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                              Narasimha Karumanchi

     Comparing Incremental Strategy and Doubling Strategy ................................................ 179
     Comparing Array Implementation and Linked List Implementation ............................. 180
  Problems on Stacks ................................................................................................................ 180
Chapter 6             Queues ......................................................................................................... 205
  What is a queue? .................................................................................................................... 205
  How are queues used? ............................................................................................................ 205
  Queue ADT ............................................................................................................................. 206
     Main queue operations ....................................................................................................... 206
     Auxiliary queue operations ................................................................................................ 206
  Exceptions............................................................................................................................... 206
  Applications ............................................................................................................................ 206
     Direct applications .............................................................................................................. 206
     Indirect applications ........................................................................................................... 207
  Implementation ...................................................................................................................... 207
     Why Circular Arrays? ........................................................................................................ 207
     Simple Circular Array Implementation ............................................................................ 208
     Performance & Limitations ................................................................................................ 210
     Dynamic Circular Array Implementation......................................................................... 211
     Performance ........................................................................................................................ 213
     Linked List Implementation .............................................................................................. 214
     Performance ........................................................................................................................ 216
     Comparison of Implementations ....................................................................................... 216
  Problems on Queues .............................................................................................................. 216
Chapter 7             Trees ............................................................................................................. 221
  What is a tree? ........................................................................................................................ 221
  Glossary................................................................................................................................... 221
  Binary Trees............................................................................................................................ 223
     Types of Binary Trees ......................................................................................................... 223
     Properties of Binary Trees.................................................................................................. 224
     Structure of Binary Trees ................................................................................................... 225
     Operations on Binary Trees ............................................................................................... 226

      16 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                            Narasimha Karumanchi

    Applications of Binary Trees.............................................................................................. 226
 Binary Tree Traversals ........................................................................................................... 227
    Traversal Possibilities ......................................................................................................... 227
    Classifying the Traversals ................................................................................................... 228
    Preorder Traversal .............................................................................................................. 228
    Inorder Traversal ................................................................................................................ 230
    Postorder Traversal............................................................................................................. 231
    Level order Traversal .......................................................................................................... 233
    Problems on Binary Trees .................................................................................................. 234
 Generic Trees (N-ary Trees) .................................................................................................. 259
    Representation of generic trees ......................................................................................... 260
    Problems on Generic Trees ................................................................................................ 261
 Threaded Binary Tree Traversals [Stack or Queue less Traversals] .................................... 268
    Issues with regular Binary Trees........................................................................................ 268
    Motivation for threaded binary trees ................................................................................ 269
    Classifying threaded binary trees ...................................................................................... 269
    Types of threaded binary trees .......................................................................................... 269
    Threaded binary tree structure .......................................................................................... 270
    Difference between Binary Tree and Threaded Binary Tree Structures......................... 270
    Finding Inorder Successor in Inorder Threaded Binary Tree .......................................... 271
    Inorder Traversal in Inorder Threaded Binary Tree ........................................................ 272
    Finding Preorder Successor in Inorder Threaded Binary Tree ........................................ 273
    Preorder Traversal of Inorder Threaded Binary Tree ...................................................... 274
    Insertion of Nodes in Inorder Threaded Binary Trees ..................................................... 274
    Problems on Threaded binary Trees ................................................................................. 276
 Expression Trees ..................................................................................................................... 278
    Algorithm for Building Expression Tree from Postfix Expression .................................. 278
    Example ............................................................................................................................... 279
 XOR Trees............................................................................................................................... 281
 Binary Search Trees (BSTs) .................................................................................................... 283
    Why Binary Search Trees? ................................................................................................. 283

     17 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

     Binary Search Tree Property.............................................................................................. 283
     Binary Search Tree Declaration ......................................................................................... 284
     Operations on Binary Search Trees ................................................................................... 284
     Important Notes on Binary Search Trees .......................................................................... 285
     Finding an Element in Binary Search Trees ..................................................................... 285
     Finding an Minimum Element in Binary Search Trees.................................................... 286
     Finding an Maximum Element in Binary Search Trees ................................................... 287
     Where is Predecessor and Successor?................................................................................ 288
     Inserting an Element from Binary Search Tree ................................................................ 289
     Deleting an Element from Binary Search Tree ................................................................. 290
     Problems on Binary Search Trees ...................................................................................... 293
  Balanced Binary Search Trees ............................................................................................... 304
     Complete balanced binary search trees ............................................................................. 304
  AVL (Adelson-Velskii and Landis) trees .............................................................................. 304
     Properties of AVL Trees ..................................................................................................... 304
     Minimum/Maximum Number of Nodes in AVL Tree...................................................... 305
     AVL Tree declaration ......................................................................................................... 306
     Finding height of an AVL tree ........................................................................................... 306
     Rotations ............................................................................................................................. 307
     Observation ......................................................................................................................... 307
     Types of violations .............................................................................................................. 307
     Single rotations ................................................................................................................... 308
     Double rotations ................................................................................................................. 310
     Insertion into an AVL tree ................................................................................................. 312
     Problems on AVL Trees ..................................................................................................... 313
  Other Variations in Trees ...................................................................................................... 318
     Red-Black Trees .................................................................................................................. 318
     Splay Trees .......................................................................................................................... 319
     Augmented Trees................................................................................................................ 319
     Interval Trees ...................................................................................................................... 321
Chapter 8             Priority Queue and Heaps .......................................................................... 323

     18 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

  What is a Priority Queue? ..................................................................................................... 323
  Priority Queue ADT............................................................................................................... 323
     Main Priority Queues Operations ..................................................................................... 323
     Auxiliary Priority Queues Operations .............................................................................. 324
  Priority Queue Applications.................................................................................................. 324
  Priority Queue Implementations .......................................................................................... 324
     Unordered Array Implementation .................................................................................... 324
     Unordered List Implementation ........................................................................................ 324
     Ordered Array Implementation ........................................................................................ 324
     Ordered List Implementation ............................................................................................ 325
     Binary Search Trees Implementation ................................................................................ 325
     Balanced Binary Trees Implementation ............................................................................ 325
     Binary Heap Implementation ............................................................................................ 325
     Comparing Implementations ............................................................................................. 325
  Heaps and Binary Heap.......................................................................................................... 326
     What is a heap? ................................................................................................................... 326
     Types of heaps? ................................................................................................................... 326
  Binary Heaps .......................................................................................................................... 327
     Representing Heaps ............................................................................................................ 327
     Parent of a Node ................................................................................................................. 328
     Children of a Node ............................................................................................................. 328
     Getting the Maximum Element ......................................................................................... 328
     Heapifying an Element....................................................................................................... 329
     Deleting an Element ........................................................................................................... 331
     Inserting an Element .......................................................................................................... 332
     Heapifying the Given Data (Array) ................................................................................... 333
     Heapsort .............................................................................................................................. 334
  Problems on Priority Queues [Heaps] .................................................................................. 335
Chapter 9             Disjoint Sets ADT ........................................................................................ 348
  Introduction ........................................................................................................................... 348
  Equivalence Relations and Equivalence Classes ................................................................... 348

      19 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

  Disjoint Sets ADT ................................................................................................................... 349
  Applications ............................................................................................................................ 349
  Tradeoffs in implementing Disjoint Sets ADT operations ................................................... 349
     Faster FIND Implementation (Quick FIND)..................................................................... 350
     Faster UNION Implementation (Quick UNION) ............................................................. 350
  Faster UNION implementation (Slow FIND) ....................................................................... 351
  Faster UNION implementations (Faster FIND).................................................................... 354
     UNION by Size ................................................................................................................... 354
     UNION by Height (UNION by Rank) ............................................................................... 356
     Comparing UNION by Size and UNION by Height ......................................................... 357
     Further Improving Faster UNION Implementations (Path Compression) ..................... 357
  Summary ................................................................................................................................. 358
  Problems on Disjoint Sets ...................................................................................................... 358
Chapter 10            Graph Algorithms ....................................................................................... 360
  Introduction ........................................................................................................................... 360
  Glossary................................................................................................................................... 360
  Applications of graphs ........................................................................................................... 364
  Graph Representation ............................................................................................................ 364
     Adjacency Matrix ............................................................................................................... 364
     Adjacency List..................................................................................................................... 366
     Adjacency Set...................................................................................................................... 369
     Comparison of Graph Representations ............................................................................. 369
  Graph Traversals .................................................................................................................... 369
     Depth First Search [DFS] ................................................................................................... 369
     Breadth First Search [BFS] ................................................................................................. 375
     Comparing DFS and BFS .................................................................................................... 378
  Topological Sort...................................................................................................................... 379
     Applications of Topological Sorting .................................................................................. 380
  Shortest Path Algorithms....................................................................................................... 381
     Shortest path in unweighted graph ................................................................................... 381
     Shortest path in weighted graph [Dijkstra’s] .................................................................... 383

      20 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

     Bellman-Ford Algorithm.................................................................................................... 388
     Overview of Shortest Path Algorithms ............................................................................. 389
  Minimal Spanning Tree ......................................................................................................... 389
     Prim's Algorithm ................................................................................................................ 390
     Kruskal’s Algorithms .......................................................................................................... 391
  Problems on Graph Algorithms ............................................................................................ 394
Chapter 11            Sorting .......................................................................................................... 430
  What is sorting?...................................................................................................................... 430
  Why Sorting?.......................................................................................................................... 430
  Classification........................................................................................................................... 430
     By number of comparisons ................................................................................................ 430
     By number of swaps ........................................................................................................... 431
     By Memory Usage............................................................................................................... 431
     By Recursion ....................................................................................................................... 431
     By Stability .......................................................................................................................... 431
     By Adaptability ................................................................................................................... 431
  Other classifications ............................................................................................................... 431
     Internal Sort ........................................................................................................................ 431
     External Sort ....................................................................................................................... 432
  Bubble sort .............................................................................................................................. 432
     Performance ........................................................................................................................ 432
     Implementation .................................................................................................................. 432
  Selection Sort .......................................................................................................................... 433
     Performance ........................................................................................................................ 434
     Algorithm ............................................................................................................................ 434
     Implementation .................................................................................................................. 434
  Insertion sort .......................................................................................................................... 435
     Insertion advantages ........................................................................................................... 435
     Performance ........................................................................................................................ 435
     Algorithm ............................................................................................................................ 435
     Implementation .................................................................................................................. 436

      21 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                              Narasimha Karumanchi

     Example ............................................................................................................................... 436
     Analysis ............................................................................................................................... 437
     Comparisons to other sorting algorithms .......................................................................... 437
  Shell sort ................................................................................................................................. 438
     Performance ........................................................................................................................ 438
     Implementation .................................................................................................................. 438
     Analysis ............................................................................................................................... 439
  Merge sort ............................................................................................................................... 439
     Performance ........................................................................................................................ 440
     Important Notes .................................................................................................................. 440
     Implementation .................................................................................................................. 440
     Analysis ............................................................................................................................... 442
  Heapsort .................................................................................................................................. 442
     Performance ........................................................................................................................ 442
  Quicksort ................................................................................................................................ 442
     Performance ........................................................................................................................ 442
     Algorithm ............................................................................................................................ 443
     Implementation .................................................................................................................. 443
     Analysis ............................................................................................................................... 444
  Comparison of sorting algorithms ......................................................................................... 446
  Linear Sorting Algorithms ..................................................................................................... 446
  Counting Sort ......................................................................................................................... 447
  Bucket sort [or Bin Sort] ........................................................................................................ 448
  Radix sort ................................................................................................................................ 449
  Topological Sort...................................................................................................................... 450
  External Sorting...................................................................................................................... 450
  Problems on Sorting ............................................................................................................... 451
Chapter 12            Searching ..................................................................................................... 467
  What is searching? ................................................................................................................. 467
  Why Searching? ..................................................................................................................... 467
  Types of Searching ................................................................................................................. 467

      22 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                            Narasimha Karumanchi

  Unsorted/Unordered Linear Search ...................................................................................... 467
  Sorted/Ordered Linear Search ............................................................................................... 468
  Binary Search.......................................................................................................................... 469
  Comparing Basic Searching Algorithms ............................................................................... 470
  Symbol Tables and Hashing................................................................................................... 470
  String Searching Algorithms ................................................................................................. 470
  Problems on Searching .......................................................................................................... 471
Chapter 13            Selection Algorithms [Medians] ................................................................. 507
  What are Selection Algorithms? ........................................................................................... 507
  Linear minimum/maximum algorithms................................................................................ 507
  Selection by sorting ................................................................................................................ 507
  Partition-based general selection algorithm [Quick Select] ................................................ 508
  Linear general selection algorithm - Median of Medians algorithm .................................. 508
  Finding the k smallest elements in sorted order .................................................................. 508
  Problems on Selection Algorithms........................................................................................ 508
Chapter 14            Symbol Tables.............................................................................................. 522
  What are Symbol Tables? ...................................................................................................... 522
  Symbol Table Applications .................................................................................................... 522
  Symbol Table implementations ............................................................................................. 523
     Unordered [Unsorted] array implementation................................................................... 523
     Ordered [Sorted] array implementation ........................................................................... 523
     Unordered linked list implementation .............................................................................. 523
     Ordered linked list implementation .................................................................................. 523
     Binary search implementation........................................................................................... 523
     Ternary search implementation ........................................................................................ 523
     Hashing implementation.................................................................................................... 524
  Comparing symbol table implementations ........................................................................... 524
Chapter 15            Hashing ........................................................................................................ 525
  What is Hashing? ................................................................................................................... 525
  Why Hashing? ........................................................................................................................ 525
  HashTable ADT ...................................................................................................................... 525

      23 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                             Narasimha Karumanchi

  Understanding Hashing ......................................................................................................... 525
     If Arrays Are There Why Hashing? .................................................................................. 526
  Components in Hashing ........................................................................................................ 528
  Hash Table .............................................................................................................................. 528
  Hash Function ........................................................................................................................ 528
  How to Choose Hash Function? ............................................................................................ 528
     Hash Functions in Practice ................................................................................................ 529
     Characteristics of Good Hash Functions ........................................................................... 529
  Load Factor ............................................................................................................................. 530
  Collisions ................................................................................................................................ 530
  Collision Resolution Techniques ........................................................................................... 530
     Separate Chaining ............................................................................................................... 530
     Open Addressing ................................................................................................................ 531
  Comparing collision resolution techniques .......................................................................... 532
     Comparisons between Linear probing and double hashing ............................................. 532
     Comparisons: Open Addressing vs. Separate Chaining .................................................... 532
     Comparisons: Table of Open Addressing methods .......................................................... 532
  How Hashing gets O(1) Complexity?.................................................................................... 532
  Hashing Techniques ............................................................................................................... 533
     Static Hashing ..................................................................................................................... 533
     Dynamic Hashing ............................................................................................................... 533
  Problems for which Hash Tables are not suitable ................................................................ 533
  Problems on Hashing ............................................................................................................. 534
Chapter 16            String Algorithms ........................................................................................ 556
  Introduction ........................................................................................................................... 556
  String Matching Algorithms .................................................................................................. 556
  Brute Force Method ............................................................................................................... 557
  Robin-Karp String Matching Algorithm .............................................................................. 557
     Selecting Hash Function .................................................................................................... 558
     Step by Step explanation .................................................................................................... 559
     Generalization..................................................................................................................... 559

      24 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                               Narasimha Karumanchi

 String Matching with Finite Automata ................................................................................ 560
    Finite Automata .................................................................................................................. 560
    How a Finite Automaton Works?...................................................................................... 560
    Important Notes for Constructing the Finite Automata .................................................. 561
    Matching Algorithm........................................................................................................... 561
 KMP Algorithm ...................................................................................................................... 562
    Filling Prefix Table ............................................................................................................. 562
    Matching Algorithm........................................................................................................... 563
 Boyce-Moore Algorithm ........................................................................................................ 566
 Data structures for Storing Strings ........................................................................................ 567
 Hash Tables for Strings .......................................................................................................... 567
 Binary Search Trees for Strings ............................................................................................. 568
    Issues with Binary Search Tree Representation ............................................................... 568
 Tries ........................................................................................................................................ 568
    What is a Trie? .................................................................................................................... 568
    Why Tries? .......................................................................................................................... 569
    Inserting a String in Trie .................................................................................................... 569
    Searching a String in Trie................................................................................................... 570
    Issues with Tries Representation ....................................................................................... 571
 Ternary Search Trees ............................................................................................................. 571
    Ternary Search Trees Declaration ..................................................................................... 571
    Inserting strings in TST ...................................................................................................... 572
    Searching in Ternary Search Trees .................................................................................... 574
    Displaying All Words of Ternary Search Tree ................................................................. 575
    Finding Maximum Length of Words in TST .................................................................... 576
    Partial Searching in Ternary Search Trees ........................................................................ 576
 Comparing Binary Search Trees, Tries and Ternary Search Trees ...................................... 577
 Suffix Trees ............................................................................................................................. 577
    Prefix and Suffix ................................................................................................................. 577
    Observation ......................................................................................................................... 578
    What is a Suffix Tree? ........................................................................................................ 578

     25 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                            Narasimha Karumanchi

     The Construction of Suffix Trees ....................................................................................... 578
     Applications of Suffix Trees ............................................................................................... 581
  Problems on Strings ............................................................................................................... 582
Chapter 17            Algorithms Design Techniques .................................................................. 584
  Introduction ........................................................................................................................... 584
  Classification........................................................................................................................... 584
  Classification by implementation .......................................................................................... 584
     Recursion or iteration ........................................................................................................ 584
     Logical or procedural .......................................................................................................... 585
     Serial or parallel or distributed .......................................................................................... 585
     Deterministic or non-deterministic .................................................................................. 585
     Exact or approximate .......................................................................................................... 585
     Quantum or Turing ............................................................................................................ 585
  Classification by design methods .......................................................................................... 586
     Brute-force or exhaustive search ....................................................................................... 586
     Divide and conquer ............................................................................................................ 586
     The greedy method............................................................................................................. 586
     Dynamic programming ...................................................................................................... 586
     Linear programming ........................................................................................................... 587
     Reduction [Transform and conquer] ................................................................................. 587
  Classification by field of study............................................................................................... 587
  Classification by complexity .................................................................................................. 587
  Other Classifications .............................................................................................................. 587
     Randomized algorithms ..................................................................................................... 587
     Branch and bound enumeration and backtracking .......................................................... 587
Chapter 18            Greedy Algorithms ...................................................................................... 589
  Introduction ........................................................................................................................... 589
  Greedy strategy ...................................................................................................................... 589
  Elements of Greedy Algorithms ............................................................................................ 589
     Greedy choice property...................................................................................................... 589
     Optimal substructure .......................................................................................................... 590

      26 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                           Narasimha Karumanchi

  Does greedy works always? ................................................................................................... 590
  Advantages and disadvantages of greedy technique ............................................................ 590
  Examples of greedy algorithms.............................................................................................. 590
  Understanding Greedy Technique ........................................................................................ 591
     Huffman coding algorithm ................................................................................................ 591
  Problems on greedy algorithms ............................................................................................. 595
Chapter 19            Divide and Conquer Algorithms ................................................................ 609
  Introduction ........................................................................................................................... 609
  What is Divide and Conquer Strategy? ................................................................................. 609
  Does Divide and Conquer works always? ............................................................................. 609
  Divide and Conquer Visualization ........................................................................................ 609
  Understanding Divide and Conquer ..................................................................................... 610
  Advantages of Divide and Conquer ...................................................................................... 611
  Disadvantages of Divide and Conquer .................................................................................. 612
  Master Theorem ..................................................................................................................... 612
  Examples on Divide and Conquer ......................................................................................... 612
  Problems on Divide and Conquer ......................................................................................... 613
Chapter 20            Dynamic Programming............................................................................... 634
  Introduction ........................................................................................................................... 634
  What is Dynamic Programming Strategy? ........................................................................... 634
  Can Dynamic Programming Solves any problem? ............................................................... 634
  Dynamic Programming Approaches ..................................................................................... 635
     Bottom-up dynamic programming .................................................................................... 635
     Top-down dynamic programming..................................................................................... 635
     Bottom-up versus Top-down programming ..................................................................... 635
  Examples of Dynamic Programming Algorithms................................................................. 636
  Understanding Dynamic Programming ................................................................................ 636
     Factorial of a number ......................................................................................................... 636
     Fibonacci Series .................................................................................................................. 638
     Observations ....................................................................................................................... 641
  Problems on Dynamic Programming.................................................................................... 642

      27 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                              Narasimha Karumanchi

Chapter 21            Complexity Classes ...................................................................................... 699
  Introduction ........................................................................................................................... 699
  Polynomial/exponential time ................................................................................................ 700
  What is decision problem? .................................................................................................... 700
  Decision Procedure ................................................................................................................ 700
  What is a complexity class? ................................................................................................... 700
  Types of Complexity Classes.................................................................................................. 701
     P Class ................................................................................................................................. 701
     NP Class............................................................................................................................... 701
     Co-NP Class ........................................................................................................................ 701
     Relationship between P, NP and Co-NP ........................................................................... 701
     NP-hard Class ..................................................................................................................... 702
     NP-complete Class .............................................................................................................. 702
     Relationship between P, NP Co-NP, NP-Hard and NP-Complete.................................. 703
     Does P==NP? ....................................................................................................................... 703
  Reductions .............................................................................................................................. 704
     Important NP-Complete Problems (Reductions) ............................................................. 705
  Problems on Complexity Classes ........................................................................................... 707
Chapter 22            Miscellaneous Concepts .............................................................................. 711
  Introduction ........................................................................................................................... 711
  Hacks on Bitwise Programming ............................................................................................ 711
     Bitwise AND ....................................................................................................................... 711
     Bitwise OR .......................................................................................................................... 711
     Bitwise Exclusive-OR ......................................................................................................... 712
     Bitwise Left Shift ................................................................................................................ 712
     Bitwise Right Shift.............................................................................................................. 712
     Bitwise Complement .......................................................................................................... 713
     Checking whether K-th bit is set or not ........................................................................... 713
     Setting K-th bit ................................................................................................................... 713
     Clearing K-th bit ................................................................................................................. 713
     Toggling K-th bit ................................................................................................................ 714

      28 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy                                                  Narasimha Karumanchi

   Toggling Rightmost One bit .............................................................................................. 714
   Isolating Rightmost One bit ............................................................................................... 714
   Isolating Rightmost Zero bit .............................................................................................. 715
   Checking Whether Number is Power of 2 not ................................................................. 715
   Multiplying Number by Power of 2 .................................................................................. 716
   Dividing Number by Power of 2 ....................................................................................... 716
   Finding Modulo of a Given Number ................................................................................. 716
   Reversing the Binary Number ........................................................................................... 716
   Counting Number of One’s in number ............................................................................. 717
   Creating for Mask for Trailing Zero’s ................................................................................ 719




    29 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




    30 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




  DATA STRUCTURES AND ALGORITHMS MADE EASY




    31 < | ©www.CareerMonk.com
Data Structures and Algorithms Made Easy   Narasimha Karumanchi




    32 < | ©www.CareerMonk.com

Más contenido relacionado

La actualidad más candente

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfaSampath Kumar S
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problemsJyotsna Suryadevara
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 
Toc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationToc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationnarayan dudhe
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxSyed Zaid Irshad
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Hossain Md Shakhawat
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfHarshNagda5
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrencesMegha V
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methodsTarun Gehlot
 

La actualidad más candente (20)

Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Lecture26
Lecture26Lecture26
Lecture26
 
Prim's algorithm
Prim's algorithmPrim's algorithm
Prim's algorithm
 
1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa1.7. eqivalence of nfa and dfa
1.7. eqivalence of nfa and dfa
 
Dfa h11
Dfa h11Dfa h11
Dfa h11
 
9. chapter 8 np hard and np complete problems
9. chapter 8   np hard and np complete problems9. chapter 8   np hard and np complete problems
9. chapter 8 np hard and np complete problems
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Toc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computationToc 1 | gate | Theory of computation
Toc 1 | gate | Theory of computation
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)Counting sort(Non Comparison Sort)
Counting sort(Non Comparison Sort)
 
Dijkstra.ppt
Dijkstra.pptDijkstra.ppt
Dijkstra.ppt
 
Design and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdfDesign and Analysis Algorithms.pdf
Design and Analysis Algorithms.pdf
 
Beta gamma functions
Beta gamma functionsBeta gamma functions
Beta gamma functions
 
Solving recurrences
Solving recurrencesSolving recurrences
Solving recurrences
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Merge sort algorithm power point presentation
Merge sort algorithm power point presentationMerge sort algorithm power point presentation
Merge sort algorithm power point presentation
 
Applications of numerical methods
Applications of numerical methodsApplications of numerical methods
Applications of numerical methods
 

Destacado

Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easyCareerMonk Publications
 
Data structure lab 03ameer hamza
Data structure lab 03ameer hamzaData structure lab 03ameer hamza
Data structure lab 03ameer hamzaameer hamza
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project FileDeyvessh kumar
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manualSANTOSH RATH
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programsSyed Mustafa
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...vtunotesbysree
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++thesaqib
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manualSyed Mustafa
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 

Destacado (20)

Data structures and algorithms made easy
Data structures and algorithms made easyData structures and algorithms made easy
Data structures and algorithms made easy
 
Data structure lab 03ameer hamza
Data structure lab 03ameer hamzaData structure lab 03ameer hamza
Data structure lab 03ameer hamza
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
Assignment
AssignmentAssignment
Assignment
 
C program
C programC program
C program
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Data structures lab c programs
Data structures lab  c programsData structures lab  c programs
Data structures lab c programs
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...VTU 1ST SEM  PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
VTU 1ST SEM PROGRAMMING IN C & DATA STRUCTURES SOLVED PAPERS OF JUNE-2015 & ...
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
Lab manual of C++
Lab manual of C++Lab manual of C++
Lab manual of C++
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
Data structures lab manual
Data structures lab manualData structures lab manual
Data structures lab manual
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Data structures
Data structuresData structures
Data structures
 
Relational Database Management System
Relational Database Management SystemRelational Database Management System
Relational Database Management System
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 

Similar a Data Structures and Algorithms made Easy (Cover Page)

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
 
Data structure & algorithm using java
Data structure & algorithm using javaData structure & algorithm using java
Data structure & algorithm using javaNarayan Sau
 
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
 
Datastructuresandalgorithms
DatastructuresandalgorithmsDatastructuresandalgorithms
DatastructuresandalgorithmsNiharika Sahay
 
Data structures and algorithms made easy java
Data structures and algorithms made easy   javaData structures and algorithms made easy   java
Data structures and algorithms made easy javaCareerMonk Publications
 
Asp.net mvc interview questions & answers by shailendra chauhan
Asp.net mvc interview questions & answers   by shailendra chauhanAsp.net mvc interview questions & answers   by shailendra chauhan
Asp.net mvc interview questions & answers by shailendra chauhanMasterCode.vn
 
5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)harkesh singh
 
046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)Aakaash Ngl
 
5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)harkesh singh
 
046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)Suyog Ukey
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrummtoppa
 
Sym model of communication - A communication model created by Arvind
Sym model of communication - A communication model created by ArvindSym model of communication - A communication model created by Arvind
Sym model of communication - A communication model created by ArvindArvind Bhardwaj [AB]
 

Similar a Data Structures and Algorithms made Easy (Cover Page) (20)

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...
 
Data structure & algorithm using java
Data structure & algorithm using javaData structure & algorithm using java
Data structure & algorithm using java
 
Peeling design patterns
Peeling design patternsPeeling design patterns
Peeling design patterns
 
Peeling Design Patterns
Peeling Design PatternsPeeling Design Patterns
Peeling Design Patterns
 
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]
 
T M M New
T M M NewT M M New
T M M New
 
Datastructuresandalgorithms
DatastructuresandalgorithmsDatastructuresandalgorithms
Datastructuresandalgorithms
 
List of mba institutes in hyderabad
List of mba institutes in hyderabadList of mba institutes in hyderabad
List of mba institutes in hyderabad
 
Data structures and algorithms made easy java
Data structures and algorithms made easy   javaData structures and algorithms made easy   java
Data structures and algorithms made easy java
 
Asp.net mvc interview questions & answers by shailendra chauhan
Asp.net mvc interview questions & answers   by shailendra chauhanAsp.net mvc interview questions & answers   by shailendra chauhan
Asp.net mvc interview questions & answers by shailendra chauhan
 
List of mba institutes in bangalore
List of mba institutes in bangaloreList of mba institutes in bangalore
List of mba institutes in bangalore
 
Coding interview questions
Coding interview questionsCoding interview questions
Coding interview questions
 
5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)
 
046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)
 
5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)5b9b046 sample interviewquestionbook(1)
5b9b046 sample interviewquestionbook(1)
 
046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)046 sample interviewquestionbook(1)
046 sample interviewquestionbook(1)
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrum
 
Sym model of communication - A communication model created by Arvind
Sym model of communication - A communication model created by ArvindSym model of communication - A communication model created by Arvind
Sym model of communication - A communication model created by Arvind
 

Más de CareerMonk Publications

Más de CareerMonk Publications (6)

It interview questions
It interview questionsIt interview questions
It interview questions
 
Pdp cover
Pdp coverPdp cover
Pdp cover
 
Data Structures and Algorithms Made Easy
Data Structures and Algorithms Made EasyData Structures and Algorithms Made Easy
Data Structures and Algorithms Made Easy
 
Scope, binding, papameter passing techniques
Scope, binding, papameter passing techniquesScope, binding, papameter passing techniques
Scope, binding, papameter passing techniques
 
Introductionto Analysis Of Algorithms
Introductionto Analysis Of AlgorithmsIntroductionto Analysis Of Algorithms
Introductionto Analysis Of Algorithms
 
Introductionto analysis of algorithms
Introductionto analysis of algorithmsIntroductionto analysis of algorithms
Introductionto analysis of algorithms
 

Último

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 

Último (20)

The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 

Data Structures and Algorithms made Easy (Cover Page)

  • 1. D A TA S T R U C T U R E S AND ALGORITHMS MADE EASY Success Key for: • Campus Preparation • Degree/Masters Course Preparation • Instructor’s • GATE Preparation • Big Job hunters: Microsoft, Google, Amazon, Yahoo, Facebook, Adobe, IBM Labs and many more • Reference Manual for Working People ROOM NUMBER M-Tech, IIT Bombay . CareerMonk Publications
  • 2. Data Structures and Algorithms Made Easy Narasimha Karumanchi 2 < | ©www.CareerMonk.com
  • 3. Data Structures and Algorithms Made Easy Narasimha Karumanchi To My Parents -Laxmi and Modaiah To My Family Members To My Friends To All Hard Workers 3 < | ©www.CareerMonk.com
  • 4. Data Structures and Algorithms Made Easy Narasimha Karumanchi 4 < | ©www.CareerMonk.com
  • 5. Data Structures and Algorithms Made Easy Narasimha Karumanchi Copyright ©2010 by CareerMonk.com All rights reserved. Designed by Narasimha Karumanchi Printed in India 5 < | ©www.CareerMonk.com
  • 6. Data Structures and Algorithms Made Easy Narasimha Karumanchi 6 < | ©www.CareerMonk.com
  • 7. Data Structures and Algorithms Made Easy Narasimha Karumanchi Acknowledgements I would like to thank Mohan Mullapudi for encouraging me when I was at IIT Bombay. He is the first person who taught me the importance of Algorithms and its Design. From that day I keep on updating myself. I would like to thank Vamshi [Mentor Graphics] and Kalyani [Xilinx] for spending time in reviewing this book and providing me the suggestions. I would like to thank Sobhan [Professor IIT, Hyderabad] for spending his valuable time in reviewing the book and suggestions. Being a PhD holder in algorithms his review provided us the confidence in the quality of the book. I would like to thank Kiran and Laxmi [Founder’s of TheGATEMATE.com] for approaching me for teaching of Algorithms and Data Structures at their GATE center. They are the primary reason for initiation of this book. I would like to thank Timma [Founder of Algorithmica.co.in] for providing me the valuable suggestions in writing, reviewing and for providing coding solutions to some of the problems. I would like to thank Director’s of Guntur Vikas College, Director’s of ACE Engineering Academy, TRC Boss [Former Director of APTransco] and Venkateswara Rao [VNR Vignanajyothi Engineering College, Hyderabad] for helping me and my family during our studies. Also, many friends and colleagues have contributed greatly to the quality of this book. I thank all of you for your help and suggestions. Special thanks should go to my wife, Sailaja for her encouragement and help during writing of this book. - Narasimha Karumanchi 7 < | ©www.CareerMonk.com
  • 8. Data Structures and Algorithms Made Easy Narasimha Karumanchi 8 < | ©www.CareerMonk.com
  • 9. Data Structures and Algorithms Made Easy Narasimha Karumanchi Preface Dear Reader, Please Hold on I know many people do not read preface. But I would like to strongly on! recommend reading preface of this book at least. This preface has something different from regular prefaces. As a job seeker if you read the complete book with good understanding, I am sure you will challenge the interviewer’s and that is the objective of this book. If you read as an instructor, 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 students of Engineering and Masters 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 700 algorithmic problems and all of them are with solutions. And finally if you read as a student preparing for competition exams [like Graduate Aptitude Test for Engineering (GATE), DRDO or any other exam for Computer Science/Information Technology], then the content of this book covers all the required topics in full details. While writing the book, an intense care has been taken to ensure that the content should 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, multiple solutions are provided with different complexities. First, we start with brute force solution and slowly move towards the best solution 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. 9 < | ©www.CareerMonk.com
  • 10. Data Structures and Algorithms Made Easy Narasimha Karumanchi It is recommended that, at least one complete reading of this book is required to get full understanding of all the topics. In the subsequent readings, readers 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 readers to constantly monitor this site for any corrections, new problems and solutions. Also, please provide your valuable suggestions at: Info@CareerMonk.com. Always remember the following quotes by Vivekananda which will give you the full energy and confidence. Wish you all the best. Have a nice reading. -Narasimha Karumanchi All power is within you. You can do anything and everything. Believe in that. -Swami Vivekananda 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. -Swami Vivekananda 10 < | ©www.CareerMonk.com
  • 11. Data Structures and Algorithms Made Easy Narasimha Karumanchi Table of Contents Chapter 1 Introduction .................................................................................................. 33 Introduction ............................................................................................................................. 33 Variables ................................................................................................................................... 33 Data types ................................................................................................................................. 33 System defined data types (Primitive data types) ............................................................... 34 User defined data types ........................................................................................................ 34 Data structure ........................................................................................................................... 35 Abstract Data Types (ADT’s) ................................................................................................... 35 Memory and variables.............................................................................................................. 36 Size of a variable ................................................................................................................... 37 Address of a variable ............................................................................................................ 37 Pointers ..................................................................................................................................... 38 Pointer declaration ............................................................................................................... 38 Using pointers ....................................................................................................................... 38 Pointer arithmetic ................................................................................................................ 40 Arrays and pointers .............................................................................................................. 41 Dynamic memory allocation................................................................................................ 41 Function Pointers ................................................................................................................. 42 Parameter Passing Techniques ................................................................................................ 42 Actual and Formal Parameters ............................................................................................ 42 Semantics of Parameter Passing........................................................................................... 43 Language Support for Parameter Passing Techniques........................................................ 43 Pass by Value ........................................................................................................................ 44 Pass by Result........................................................................................................................ 45 Pass by Value-Result ............................................................................................................ 46 Pass by reference (aliasing) .................................................................................................. 47 Pass by name ......................................................................................................................... 48 11 < | ©www.CareerMonk.com
  • 12. Data Structures and Algorithms Made Easy Narasimha Karumanchi Binding...................................................................................................................................... 48 Binding Times ....................................................................................................................... 49 Static binding (Early binding).............................................................................................. 49 Dynamic binding (Late binding) ......................................................................................... 49 Scope ......................................................................................................................................... 50 Static scope ............................................................................................................................ 50 Dynamic scope ...................................................................................................................... 51 Storage classes ........................................................................................................................... 52 Auto storage class ................................................................................................................. 52 Extern storage class............................................................................................................... 53 Register storage class ............................................................................................................ 59 Static storage class................................................................................................................. 59 Storage Organization................................................................................................................ 60 Static Segment....................................................................................................................... 61 Stack Segment ....................................................................................................................... 61 Heap Segment ....................................................................................................................... 64 Shallow copy versus Deep copy .............................................................................................. 65 Chapter 2 Analysis of Algorithms.................................................................................. 66 Introduction ............................................................................................................................. 66 What is algorithm? ................................................................................................................... 66 Why analysis of algorithms?.................................................................................................... 67 Goal of analysis of algorithms? ................................................................................................ 67 What do we mean by running time analysis? ........................................................................ 67 How do we compare algorithms? ............................................................................................ 67 What is Rate of Growth? ......................................................................................................... 68 Commonly used Rate of Growths ........................................................................................... 68 Types of Analysis...................................................................................................................... 70 Asymptotic notation? ............................................................................................................... 70 Big-O notation .......................................................................................................................... 71 Big-O Visualization .............................................................................................................. 71 Big-O examples ..................................................................................................................... 72 12 < | ©www.CareerMonk.com
  • 13. Data Structures and Algorithms Made Easy Narasimha Karumanchi No uniqueness? ..................................................................................................................... 73 Omega- notation.................................................................................................................... 73 Examples............................................................................................................................ 74 Theta-θ notation....................................................................................................................... 74 θ Examples ............................................................................................................................ 75 Important Notes .................................................................................................................... 76 Why we call Asymptotic Analysis?......................................................................................... 76 Guidelines for asymptotic analysis? ........................................................................................ 76 Properties of notations ............................................................................................................. 79 Commonly used logarithms and summations ........................................................................ 79 Arithmetic series................................................................................................................... 80 Master Theorem for Divide and Conquer .............................................................................. 80 Variant of divide and conquer master theorem .................................................................. 81 Problems on Master Theorem for Divide and Conquer......................................................... 81 Master Theorem for subtract and conquer recurrences......................................................... 84 Variant of subtraction and conquer master theorem ......................................................... 84 Problems on Algorithms Analysis ........................................................................................... 84 Chapter 3 Recursion and Backtracking ....................................................................... 103 Introduction ........................................................................................................................... 103 What is recursion? ................................................................................................................. 103 Why recursion? ...................................................................................................................... 103 Format of a recursive function .............................................................................................. 103 Recursion and Memory (Visualization) ................................................................................ 105 Recursion versus Iteration ..................................................................................................... 106 Recursion ............................................................................................................................ 106 Iteration............................................................................................................................... 106 Important notes on recursion ................................................................................................ 106 Example Algorithms of Recursion ........................................................................................ 107 Problems on recursion ........................................................................................................... 107 What is backtracking?............................................................................................................ 107 Important notes on backtracking .......................................................................................... 107 13 < | ©www.CareerMonk.com
  • 14. Data Structures and Algorithms Made Easy Narasimha Karumanchi Example algorithms on backtracking .................................................................................... 108 Problems on backtracking ..................................................................................................... 108 Chapter 4 Linked Lists.................................................................................................. 113 What is a Linked List?............................................................................................................ 113 Linked Lists ADT ................................................................................................................... 113 Main Linked Lists Operations ............................................................................................ 113 Auxiliary Linked Lists Operations ..................................................................................... 113 Why Linked Lists?.................................................................................................................. 113 Arrays Overview................................................................................................................. 114 Why constant time for accessing array elements?............................................................ 114 Advantages of arrays .......................................................................................................... 114 Disadvantages of arrays ...................................................................................................... 114 Dynamic Arrays .................................................................................................................. 115 Advantages of Linked Lists ................................................................................................ 115 Issues with Linked Lists (Disadvantages) .......................................................................... 115 Comparison of Linked Lists with Arrays and Dynamic Arrays........................................... 116 Singly Linked Lists ................................................................................................................. 116 Basic Operations on a List .................................................................................................. 117 Traversing the Linked List ................................................................................................. 117 Singly Linked List Insertion .................................................................................................. 118 Inserting a node in Singly Linked List at the beginning .................................................. 118 Inserting a node in Singly Linked List at the ending ....................................................... 118 Inserting a node in Singly Linked List in the middle ....................................................... 119 Singly Linked List Deletion ................................................................................................... 121 Deleting the first node in Singly Linked List .................................................................... 121 Deleting the last node in Singly Linked List ..................................................................... 122 Deleting an intermediate node in Singly Linked List....................................................... 122 Deleting Singly Linked List ................................................................................................... 124 Doubly Linked Lists ............................................................................................................... 125 Doubly Linked List Insertion ................................................................................................ 125 Inserting a node in Doubly Linked List at the beginning ................................................ 126 14 < | ©www.CareerMonk.com
  • 15. Data Structures and Algorithms Made Easy Narasimha Karumanchi Inserting a node in Doubly Linked List at the ending ..................................................... 126 Inserting a node in Doubly Linked List in the middle ..................................................... 127 Doubly Linked List Deletion ................................................................................................. 129 Deleting the first node in Doubly Linked List .................................................................. 129 Deleting the last node in Doubly Linked List ................................................................... 130 Deleting an intermediate node in Doubly Linked List..................................................... 130 Circular Linked Lists .............................................................................................................. 132 Counting nodes in a circular list ........................................................................................ 133 Printing the contents of a circular list............................................................................... 134 Inserting a node at the end of a Circular Linked List ....................................................... 134 Inserting a node at front of a Circular Linked List ........................................................... 136 Deleting the last node in a circular list ............................................................................. 138 Deleting the first node in a circular list ............................................................................ 139 Applications of circular list ................................................................................................ 141 A Memory-Efficient Doubly Linked List.............................................................................. 141 Problems on Linked Lists....................................................................................................... 143 Chapter 5 Stacks ........................................................................................................... 168 What is a stack? ...................................................................................................................... 168 How are stacks used?.............................................................................................................. 168 Stack ADT ............................................................................................................................... 169 Main stack operations......................................................................................................... 169 Auxiliary stack operations.................................................................................................. 169 Exceptions ........................................................................................................................... 169 Applications ............................................................................................................................ 169 Implementation ...................................................................................................................... 170 Simple Array Implementation ........................................................................................... 171 Dynamic Array Implementation ....................................................................................... 173 Performance ........................................................................................................................ 177 Linked List Implementation .............................................................................................. 177 Performance ........................................................................................................................ 179 Comparison of Implementations ........................................................................................... 179 15 < | ©www.CareerMonk.com
  • 16. Data Structures and Algorithms Made Easy Narasimha Karumanchi Comparing Incremental Strategy and Doubling Strategy ................................................ 179 Comparing Array Implementation and Linked List Implementation ............................. 180 Problems on Stacks ................................................................................................................ 180 Chapter 6 Queues ......................................................................................................... 205 What is a queue? .................................................................................................................... 205 How are queues used? ............................................................................................................ 205 Queue ADT ............................................................................................................................. 206 Main queue operations ....................................................................................................... 206 Auxiliary queue operations ................................................................................................ 206 Exceptions............................................................................................................................... 206 Applications ............................................................................................................................ 206 Direct applications .............................................................................................................. 206 Indirect applications ........................................................................................................... 207 Implementation ...................................................................................................................... 207 Why Circular Arrays? ........................................................................................................ 207 Simple Circular Array Implementation ............................................................................ 208 Performance & Limitations ................................................................................................ 210 Dynamic Circular Array Implementation......................................................................... 211 Performance ........................................................................................................................ 213 Linked List Implementation .............................................................................................. 214 Performance ........................................................................................................................ 216 Comparison of Implementations ....................................................................................... 216 Problems on Queues .............................................................................................................. 216 Chapter 7 Trees ............................................................................................................. 221 What is a tree? ........................................................................................................................ 221 Glossary................................................................................................................................... 221 Binary Trees............................................................................................................................ 223 Types of Binary Trees ......................................................................................................... 223 Properties of Binary Trees.................................................................................................. 224 Structure of Binary Trees ................................................................................................... 225 Operations on Binary Trees ............................................................................................... 226 16 < | ©www.CareerMonk.com
  • 17. Data Structures and Algorithms Made Easy Narasimha Karumanchi Applications of Binary Trees.............................................................................................. 226 Binary Tree Traversals ........................................................................................................... 227 Traversal Possibilities ......................................................................................................... 227 Classifying the Traversals ................................................................................................... 228 Preorder Traversal .............................................................................................................. 228 Inorder Traversal ................................................................................................................ 230 Postorder Traversal............................................................................................................. 231 Level order Traversal .......................................................................................................... 233 Problems on Binary Trees .................................................................................................. 234 Generic Trees (N-ary Trees) .................................................................................................. 259 Representation of generic trees ......................................................................................... 260 Problems on Generic Trees ................................................................................................ 261 Threaded Binary Tree Traversals [Stack or Queue less Traversals] .................................... 268 Issues with regular Binary Trees........................................................................................ 268 Motivation for threaded binary trees ................................................................................ 269 Classifying threaded binary trees ...................................................................................... 269 Types of threaded binary trees .......................................................................................... 269 Threaded binary tree structure .......................................................................................... 270 Difference between Binary Tree and Threaded Binary Tree Structures......................... 270 Finding Inorder Successor in Inorder Threaded Binary Tree .......................................... 271 Inorder Traversal in Inorder Threaded Binary Tree ........................................................ 272 Finding Preorder Successor in Inorder Threaded Binary Tree ........................................ 273 Preorder Traversal of Inorder Threaded Binary Tree ...................................................... 274 Insertion of Nodes in Inorder Threaded Binary Trees ..................................................... 274 Problems on Threaded binary Trees ................................................................................. 276 Expression Trees ..................................................................................................................... 278 Algorithm for Building Expression Tree from Postfix Expression .................................. 278 Example ............................................................................................................................... 279 XOR Trees............................................................................................................................... 281 Binary Search Trees (BSTs) .................................................................................................... 283 Why Binary Search Trees? ................................................................................................. 283 17 < | ©www.CareerMonk.com
  • 18. Data Structures and Algorithms Made Easy Narasimha Karumanchi Binary Search Tree Property.............................................................................................. 283 Binary Search Tree Declaration ......................................................................................... 284 Operations on Binary Search Trees ................................................................................... 284 Important Notes on Binary Search Trees .......................................................................... 285 Finding an Element in Binary Search Trees ..................................................................... 285 Finding an Minimum Element in Binary Search Trees.................................................... 286 Finding an Maximum Element in Binary Search Trees ................................................... 287 Where is Predecessor and Successor?................................................................................ 288 Inserting an Element from Binary Search Tree ................................................................ 289 Deleting an Element from Binary Search Tree ................................................................. 290 Problems on Binary Search Trees ...................................................................................... 293 Balanced Binary Search Trees ............................................................................................... 304 Complete balanced binary search trees ............................................................................. 304 AVL (Adelson-Velskii and Landis) trees .............................................................................. 304 Properties of AVL Trees ..................................................................................................... 304 Minimum/Maximum Number of Nodes in AVL Tree...................................................... 305 AVL Tree declaration ......................................................................................................... 306 Finding height of an AVL tree ........................................................................................... 306 Rotations ............................................................................................................................. 307 Observation ......................................................................................................................... 307 Types of violations .............................................................................................................. 307 Single rotations ................................................................................................................... 308 Double rotations ................................................................................................................. 310 Insertion into an AVL tree ................................................................................................. 312 Problems on AVL Trees ..................................................................................................... 313 Other Variations in Trees ...................................................................................................... 318 Red-Black Trees .................................................................................................................. 318 Splay Trees .......................................................................................................................... 319 Augmented Trees................................................................................................................ 319 Interval Trees ...................................................................................................................... 321 Chapter 8 Priority Queue and Heaps .......................................................................... 323 18 < | ©www.CareerMonk.com
  • 19. Data Structures and Algorithms Made Easy Narasimha Karumanchi What is a Priority Queue? ..................................................................................................... 323 Priority Queue ADT............................................................................................................... 323 Main Priority Queues Operations ..................................................................................... 323 Auxiliary Priority Queues Operations .............................................................................. 324 Priority Queue Applications.................................................................................................. 324 Priority Queue Implementations .......................................................................................... 324 Unordered Array Implementation .................................................................................... 324 Unordered List Implementation ........................................................................................ 324 Ordered Array Implementation ........................................................................................ 324 Ordered List Implementation ............................................................................................ 325 Binary Search Trees Implementation ................................................................................ 325 Balanced Binary Trees Implementation ............................................................................ 325 Binary Heap Implementation ............................................................................................ 325 Comparing Implementations ............................................................................................. 325 Heaps and Binary Heap.......................................................................................................... 326 What is a heap? ................................................................................................................... 326 Types of heaps? ................................................................................................................... 326 Binary Heaps .......................................................................................................................... 327 Representing Heaps ............................................................................................................ 327 Parent of a Node ................................................................................................................. 328 Children of a Node ............................................................................................................. 328 Getting the Maximum Element ......................................................................................... 328 Heapifying an Element....................................................................................................... 329 Deleting an Element ........................................................................................................... 331 Inserting an Element .......................................................................................................... 332 Heapifying the Given Data (Array) ................................................................................... 333 Heapsort .............................................................................................................................. 334 Problems on Priority Queues [Heaps] .................................................................................. 335 Chapter 9 Disjoint Sets ADT ........................................................................................ 348 Introduction ........................................................................................................................... 348 Equivalence Relations and Equivalence Classes ................................................................... 348 19 < | ©www.CareerMonk.com
  • 20. Data Structures and Algorithms Made Easy Narasimha Karumanchi Disjoint Sets ADT ................................................................................................................... 349 Applications ............................................................................................................................ 349 Tradeoffs in implementing Disjoint Sets ADT operations ................................................... 349 Faster FIND Implementation (Quick FIND)..................................................................... 350 Faster UNION Implementation (Quick UNION) ............................................................. 350 Faster UNION implementation (Slow FIND) ....................................................................... 351 Faster UNION implementations (Faster FIND).................................................................... 354 UNION by Size ................................................................................................................... 354 UNION by Height (UNION by Rank) ............................................................................... 356 Comparing UNION by Size and UNION by Height ......................................................... 357 Further Improving Faster UNION Implementations (Path Compression) ..................... 357 Summary ................................................................................................................................. 358 Problems on Disjoint Sets ...................................................................................................... 358 Chapter 10 Graph Algorithms ....................................................................................... 360 Introduction ........................................................................................................................... 360 Glossary................................................................................................................................... 360 Applications of graphs ........................................................................................................... 364 Graph Representation ............................................................................................................ 364 Adjacency Matrix ............................................................................................................... 364 Adjacency List..................................................................................................................... 366 Adjacency Set...................................................................................................................... 369 Comparison of Graph Representations ............................................................................. 369 Graph Traversals .................................................................................................................... 369 Depth First Search [DFS] ................................................................................................... 369 Breadth First Search [BFS] ................................................................................................. 375 Comparing DFS and BFS .................................................................................................... 378 Topological Sort...................................................................................................................... 379 Applications of Topological Sorting .................................................................................. 380 Shortest Path Algorithms....................................................................................................... 381 Shortest path in unweighted graph ................................................................................... 381 Shortest path in weighted graph [Dijkstra’s] .................................................................... 383 20 < | ©www.CareerMonk.com
  • 21. Data Structures and Algorithms Made Easy Narasimha Karumanchi Bellman-Ford Algorithm.................................................................................................... 388 Overview of Shortest Path Algorithms ............................................................................. 389 Minimal Spanning Tree ......................................................................................................... 389 Prim's Algorithm ................................................................................................................ 390 Kruskal’s Algorithms .......................................................................................................... 391 Problems on Graph Algorithms ............................................................................................ 394 Chapter 11 Sorting .......................................................................................................... 430 What is sorting?...................................................................................................................... 430 Why Sorting?.......................................................................................................................... 430 Classification........................................................................................................................... 430 By number of comparisons ................................................................................................ 430 By number of swaps ........................................................................................................... 431 By Memory Usage............................................................................................................... 431 By Recursion ....................................................................................................................... 431 By Stability .......................................................................................................................... 431 By Adaptability ................................................................................................................... 431 Other classifications ............................................................................................................... 431 Internal Sort ........................................................................................................................ 431 External Sort ....................................................................................................................... 432 Bubble sort .............................................................................................................................. 432 Performance ........................................................................................................................ 432 Implementation .................................................................................................................. 432 Selection Sort .......................................................................................................................... 433 Performance ........................................................................................................................ 434 Algorithm ............................................................................................................................ 434 Implementation .................................................................................................................. 434 Insertion sort .......................................................................................................................... 435 Insertion advantages ........................................................................................................... 435 Performance ........................................................................................................................ 435 Algorithm ............................................................................................................................ 435 Implementation .................................................................................................................. 436 21 < | ©www.CareerMonk.com
  • 22. Data Structures and Algorithms Made Easy Narasimha Karumanchi Example ............................................................................................................................... 436 Analysis ............................................................................................................................... 437 Comparisons to other sorting algorithms .......................................................................... 437 Shell sort ................................................................................................................................. 438 Performance ........................................................................................................................ 438 Implementation .................................................................................................................. 438 Analysis ............................................................................................................................... 439 Merge sort ............................................................................................................................... 439 Performance ........................................................................................................................ 440 Important Notes .................................................................................................................. 440 Implementation .................................................................................................................. 440 Analysis ............................................................................................................................... 442 Heapsort .................................................................................................................................. 442 Performance ........................................................................................................................ 442 Quicksort ................................................................................................................................ 442 Performance ........................................................................................................................ 442 Algorithm ............................................................................................................................ 443 Implementation .................................................................................................................. 443 Analysis ............................................................................................................................... 444 Comparison of sorting algorithms ......................................................................................... 446 Linear Sorting Algorithms ..................................................................................................... 446 Counting Sort ......................................................................................................................... 447 Bucket sort [or Bin Sort] ........................................................................................................ 448 Radix sort ................................................................................................................................ 449 Topological Sort...................................................................................................................... 450 External Sorting...................................................................................................................... 450 Problems on Sorting ............................................................................................................... 451 Chapter 12 Searching ..................................................................................................... 467 What is searching? ................................................................................................................. 467 Why Searching? ..................................................................................................................... 467 Types of Searching ................................................................................................................. 467 22 < | ©www.CareerMonk.com
  • 23. Data Structures and Algorithms Made Easy Narasimha Karumanchi Unsorted/Unordered Linear Search ...................................................................................... 467 Sorted/Ordered Linear Search ............................................................................................... 468 Binary Search.......................................................................................................................... 469 Comparing Basic Searching Algorithms ............................................................................... 470 Symbol Tables and Hashing................................................................................................... 470 String Searching Algorithms ................................................................................................. 470 Problems on Searching .......................................................................................................... 471 Chapter 13 Selection Algorithms [Medians] ................................................................. 507 What are Selection Algorithms? ........................................................................................... 507 Linear minimum/maximum algorithms................................................................................ 507 Selection by sorting ................................................................................................................ 507 Partition-based general selection algorithm [Quick Select] ................................................ 508 Linear general selection algorithm - Median of Medians algorithm .................................. 508 Finding the k smallest elements in sorted order .................................................................. 508 Problems on Selection Algorithms........................................................................................ 508 Chapter 14 Symbol Tables.............................................................................................. 522 What are Symbol Tables? ...................................................................................................... 522 Symbol Table Applications .................................................................................................... 522 Symbol Table implementations ............................................................................................. 523 Unordered [Unsorted] array implementation................................................................... 523 Ordered [Sorted] array implementation ........................................................................... 523 Unordered linked list implementation .............................................................................. 523 Ordered linked list implementation .................................................................................. 523 Binary search implementation........................................................................................... 523 Ternary search implementation ........................................................................................ 523 Hashing implementation.................................................................................................... 524 Comparing symbol table implementations ........................................................................... 524 Chapter 15 Hashing ........................................................................................................ 525 What is Hashing? ................................................................................................................... 525 Why Hashing? ........................................................................................................................ 525 HashTable ADT ...................................................................................................................... 525 23 < | ©www.CareerMonk.com
  • 24. Data Structures and Algorithms Made Easy Narasimha Karumanchi Understanding Hashing ......................................................................................................... 525 If Arrays Are There Why Hashing? .................................................................................. 526 Components in Hashing ........................................................................................................ 528 Hash Table .............................................................................................................................. 528 Hash Function ........................................................................................................................ 528 How to Choose Hash Function? ............................................................................................ 528 Hash Functions in Practice ................................................................................................ 529 Characteristics of Good Hash Functions ........................................................................... 529 Load Factor ............................................................................................................................. 530 Collisions ................................................................................................................................ 530 Collision Resolution Techniques ........................................................................................... 530 Separate Chaining ............................................................................................................... 530 Open Addressing ................................................................................................................ 531 Comparing collision resolution techniques .......................................................................... 532 Comparisons between Linear probing and double hashing ............................................. 532 Comparisons: Open Addressing vs. Separate Chaining .................................................... 532 Comparisons: Table of Open Addressing methods .......................................................... 532 How Hashing gets O(1) Complexity?.................................................................................... 532 Hashing Techniques ............................................................................................................... 533 Static Hashing ..................................................................................................................... 533 Dynamic Hashing ............................................................................................................... 533 Problems for which Hash Tables are not suitable ................................................................ 533 Problems on Hashing ............................................................................................................. 534 Chapter 16 String Algorithms ........................................................................................ 556 Introduction ........................................................................................................................... 556 String Matching Algorithms .................................................................................................. 556 Brute Force Method ............................................................................................................... 557 Robin-Karp String Matching Algorithm .............................................................................. 557 Selecting Hash Function .................................................................................................... 558 Step by Step explanation .................................................................................................... 559 Generalization..................................................................................................................... 559 24 < | ©www.CareerMonk.com
  • 25. Data Structures and Algorithms Made Easy Narasimha Karumanchi String Matching with Finite Automata ................................................................................ 560 Finite Automata .................................................................................................................. 560 How a Finite Automaton Works?...................................................................................... 560 Important Notes for Constructing the Finite Automata .................................................. 561 Matching Algorithm........................................................................................................... 561 KMP Algorithm ...................................................................................................................... 562 Filling Prefix Table ............................................................................................................. 562 Matching Algorithm........................................................................................................... 563 Boyce-Moore Algorithm ........................................................................................................ 566 Data structures for Storing Strings ........................................................................................ 567 Hash Tables for Strings .......................................................................................................... 567 Binary Search Trees for Strings ............................................................................................. 568 Issues with Binary Search Tree Representation ............................................................... 568 Tries ........................................................................................................................................ 568 What is a Trie? .................................................................................................................... 568 Why Tries? .......................................................................................................................... 569 Inserting a String in Trie .................................................................................................... 569 Searching a String in Trie................................................................................................... 570 Issues with Tries Representation ....................................................................................... 571 Ternary Search Trees ............................................................................................................. 571 Ternary Search Trees Declaration ..................................................................................... 571 Inserting strings in TST ...................................................................................................... 572 Searching in Ternary Search Trees .................................................................................... 574 Displaying All Words of Ternary Search Tree ................................................................. 575 Finding Maximum Length of Words in TST .................................................................... 576 Partial Searching in Ternary Search Trees ........................................................................ 576 Comparing Binary Search Trees, Tries and Ternary Search Trees ...................................... 577 Suffix Trees ............................................................................................................................. 577 Prefix and Suffix ................................................................................................................. 577 Observation ......................................................................................................................... 578 What is a Suffix Tree? ........................................................................................................ 578 25 < | ©www.CareerMonk.com
  • 26. Data Structures and Algorithms Made Easy Narasimha Karumanchi The Construction of Suffix Trees ....................................................................................... 578 Applications of Suffix Trees ............................................................................................... 581 Problems on Strings ............................................................................................................... 582 Chapter 17 Algorithms Design Techniques .................................................................. 584 Introduction ........................................................................................................................... 584 Classification........................................................................................................................... 584 Classification by implementation .......................................................................................... 584 Recursion or iteration ........................................................................................................ 584 Logical or procedural .......................................................................................................... 585 Serial or parallel or distributed .......................................................................................... 585 Deterministic or non-deterministic .................................................................................. 585 Exact or approximate .......................................................................................................... 585 Quantum or Turing ............................................................................................................ 585 Classification by design methods .......................................................................................... 586 Brute-force or exhaustive search ....................................................................................... 586 Divide and conquer ............................................................................................................ 586 The greedy method............................................................................................................. 586 Dynamic programming ...................................................................................................... 586 Linear programming ........................................................................................................... 587 Reduction [Transform and conquer] ................................................................................. 587 Classification by field of study............................................................................................... 587 Classification by complexity .................................................................................................. 587 Other Classifications .............................................................................................................. 587 Randomized algorithms ..................................................................................................... 587 Branch and bound enumeration and backtracking .......................................................... 587 Chapter 18 Greedy Algorithms ...................................................................................... 589 Introduction ........................................................................................................................... 589 Greedy strategy ...................................................................................................................... 589 Elements of Greedy Algorithms ............................................................................................ 589 Greedy choice property...................................................................................................... 589 Optimal substructure .......................................................................................................... 590 26 < | ©www.CareerMonk.com
  • 27. Data Structures and Algorithms Made Easy Narasimha Karumanchi Does greedy works always? ................................................................................................... 590 Advantages and disadvantages of greedy technique ............................................................ 590 Examples of greedy algorithms.............................................................................................. 590 Understanding Greedy Technique ........................................................................................ 591 Huffman coding algorithm ................................................................................................ 591 Problems on greedy algorithms ............................................................................................. 595 Chapter 19 Divide and Conquer Algorithms ................................................................ 609 Introduction ........................................................................................................................... 609 What is Divide and Conquer Strategy? ................................................................................. 609 Does Divide and Conquer works always? ............................................................................. 609 Divide and Conquer Visualization ........................................................................................ 609 Understanding Divide and Conquer ..................................................................................... 610 Advantages of Divide and Conquer ...................................................................................... 611 Disadvantages of Divide and Conquer .................................................................................. 612 Master Theorem ..................................................................................................................... 612 Examples on Divide and Conquer ......................................................................................... 612 Problems on Divide and Conquer ......................................................................................... 613 Chapter 20 Dynamic Programming............................................................................... 634 Introduction ........................................................................................................................... 634 What is Dynamic Programming Strategy? ........................................................................... 634 Can Dynamic Programming Solves any problem? ............................................................... 634 Dynamic Programming Approaches ..................................................................................... 635 Bottom-up dynamic programming .................................................................................... 635 Top-down dynamic programming..................................................................................... 635 Bottom-up versus Top-down programming ..................................................................... 635 Examples of Dynamic Programming Algorithms................................................................. 636 Understanding Dynamic Programming ................................................................................ 636 Factorial of a number ......................................................................................................... 636 Fibonacci Series .................................................................................................................. 638 Observations ....................................................................................................................... 641 Problems on Dynamic Programming.................................................................................... 642 27 < | ©www.CareerMonk.com
  • 28. Data Structures and Algorithms Made Easy Narasimha Karumanchi Chapter 21 Complexity Classes ...................................................................................... 699 Introduction ........................................................................................................................... 699 Polynomial/exponential time ................................................................................................ 700 What is decision problem? .................................................................................................... 700 Decision Procedure ................................................................................................................ 700 What is a complexity class? ................................................................................................... 700 Types of Complexity Classes.................................................................................................. 701 P Class ................................................................................................................................. 701 NP Class............................................................................................................................... 701 Co-NP Class ........................................................................................................................ 701 Relationship between P, NP and Co-NP ........................................................................... 701 NP-hard Class ..................................................................................................................... 702 NP-complete Class .............................................................................................................. 702 Relationship between P, NP Co-NP, NP-Hard and NP-Complete.................................. 703 Does P==NP? ....................................................................................................................... 703 Reductions .............................................................................................................................. 704 Important NP-Complete Problems (Reductions) ............................................................. 705 Problems on Complexity Classes ........................................................................................... 707 Chapter 22 Miscellaneous Concepts .............................................................................. 711 Introduction ........................................................................................................................... 711 Hacks on Bitwise Programming ............................................................................................ 711 Bitwise AND ....................................................................................................................... 711 Bitwise OR .......................................................................................................................... 711 Bitwise Exclusive-OR ......................................................................................................... 712 Bitwise Left Shift ................................................................................................................ 712 Bitwise Right Shift.............................................................................................................. 712 Bitwise Complement .......................................................................................................... 713 Checking whether K-th bit is set or not ........................................................................... 713 Setting K-th bit ................................................................................................................... 713 Clearing K-th bit ................................................................................................................. 713 Toggling K-th bit ................................................................................................................ 714 28 < | ©www.CareerMonk.com
  • 29. Data Structures and Algorithms Made Easy Narasimha Karumanchi Toggling Rightmost One bit .............................................................................................. 714 Isolating Rightmost One bit ............................................................................................... 714 Isolating Rightmost Zero bit .............................................................................................. 715 Checking Whether Number is Power of 2 not ................................................................. 715 Multiplying Number by Power of 2 .................................................................................. 716 Dividing Number by Power of 2 ....................................................................................... 716 Finding Modulo of a Given Number ................................................................................. 716 Reversing the Binary Number ........................................................................................... 716 Counting Number of One’s in number ............................................................................. 717 Creating for Mask for Trailing Zero’s ................................................................................ 719 29 < | ©www.CareerMonk.com
  • 30. Data Structures and Algorithms Made Easy Narasimha Karumanchi 30 < | ©www.CareerMonk.com
  • 31. Data Structures and Algorithms Made Easy Narasimha Karumanchi DATA STRUCTURES AND ALGORITHMS MADE EASY 31 < | ©www.CareerMonk.com
  • 32. Data Structures and Algorithms Made Easy Narasimha Karumanchi 32 < | ©www.CareerMonk.com