Data Structures and Algorithms made Easy (Cover Page)

19,222
-1

Published on

Success key book for:


* Campus Preparation

* Degree/Masters Course Preparation

* Instructor’s

* GATE Preparation

* Big job hunters: Microsoft, Google, Amazon, Yahoo, Flip Kart, Adobe, IBM Labs, Citrix, Mentor Graphics, NetApp, Oracle, Webaroo, De-Shaw, Success Factors, Face book, McAfee and many more

* Reference Manual for working people

Published in: Technology, Education
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
19,222
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
931
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Data Structures and Algorithms made Easy (Cover Page)

  1. 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. 2. Data Structures and Algorithms Made Easy Narasimha Karumanchi 2 < | ©www.CareerMonk.com
  3. 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. 4. Data Structures and Algorithms Made Easy Narasimha Karumanchi 4 < | ©www.CareerMonk.com
  5. 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. 6. Data Structures and Algorithms Made Easy Narasimha Karumanchi 6 < | ©www.CareerMonk.com
  7. 7. Data Structures and Algorithms Made Easy Narasimha Karumanchi AcknowledgementsI would like to thank Mohan Mullapudi for encouraging me when I was at IIT Bombay. He isthe first person who taught me the importance of Algorithms and its Design. From that day Ikeep on updating myself.I would like to thank Vamshi [Mentor Graphics] and Kalyani [Xilinx] for spending time inreviewing this book and providing me the suggestions.I would like to thank Sobhan [Professor IIT, Hyderabad] for spending his valuable time inreviewing the book and suggestions. Being a PhD holder in algorithms his review providedus the confidence in the quality of the book.I would like to thank Kiran and Laxmi [Founder’s of TheGATEMATE.com] for approachingme for teaching of Algorithms and Data Structures at their GATE center. They are theprimary reason for initiation of this book.I would like to thank Timma [Founder of Algorithmica.co.in] for providing me the valuablesuggestions 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 EngineeringAcademy, TRC Boss [Former Director of APTransco] and Venkateswara Rao [VNRVignanajyothi Engineering College, Hyderabad] for helping me and my family during ourstudies.Also, many friends and colleagues have contributed greatly to the quality of this book. Ithank all of you for your help and suggestions.Special thanks should go to my wife, Sailaja for her encouragement and help during writingof this book. - Narasimha Karumanchi 7 < | ©www.CareerMonk.com
  8. 8. Data Structures and Algorithms Made Easy Narasimha Karumanchi 8 < | ©www.CareerMonk.com
  9. 9. Data Structures and Algorithms Made Easy Narasimha Karumanchi PrefaceDear 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 fromregular prefaces.As a job seeker if you read the complete book with good understanding, I am sure you willchallenge 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 aresult your students will feel proud for selecting Computer Science / Information Technologyas their degree.This book is very much useful for the students of Engineering and Masters during theiracademic preparations. All the chapters of this book contain theory and their relatedproblems as many as possible. There a total of approximately 700 algorithmic problems andall of them are with solutions.And finally if you read as a student preparing for competition exams [like Graduate AptitudeTest for Engineering (GATE), DRDO or any other exam for Computer Science/InformationTechnology], 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 shouldhelp students who are preparing for these kinds of exams.In all the chapters you will see more importance given to problems and analyzing theminstead of concentrating more on theory. For each chapter, first you will see the basicrequired 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 thatproblem. For each problem we will try to understand how much time the algorithm is takingand how much memory the algorithm is taking. 9 < | ©www.CareerMonk.com
  10. 10. Data Structures and Algorithms Made Easy Narasimha KarumanchiIt is recommended that, at least one complete reading of this book is required to get fullunderstanding of all the topics. In the subsequent readings, readers can directly go to anychapter 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 updatedat . . . I request readers to constantly monitor this site for anycorrections, 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 energyand confidence. Wish you all the best. Have a nice reading. -Narasimha KarumanchiAll power is within you. You can do anything and everything. Believe in that. -Swami VivekanandaStand up, be bold, be strong. Take the whole responsibility on your own shoulders, andknow that you are the creator of your own destiny. -Swami Vivekananda 10 < | ©www.CareerMonk.com
  11. 11. Data Structures and Algorithms Made Easy Narasimha Karumanchi Table of ContentsChapter 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. 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 .............................................................................................. 65Chapter 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. 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 ........................................................................................... 84Chapter 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. 14. Data Structures and Algorithms Made Easy Narasimha Karumanchi Example algorithms on backtracking .................................................................................... 108 Problems on backtracking ..................................................................................................... 108Chapter 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. 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....................................................................................................... 143Chapter 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. 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 ................................................................................................................ 180Chapter 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 .............................................................................................................. 216Chapter 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. 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. 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 ...................................................................................................................... 321Chapter 8 Priority Queue and Heaps .......................................................................... 323 18 < | ©www.CareerMonk.com
  19. 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] .................................................................................. 335Chapter 9 Disjoint Sets ADT ........................................................................................ 348 Introduction ........................................................................................................................... 348 Equivalence Relations and Equivalence Classes ................................................................... 348 19 < | ©www.CareerMonk.com
  20. 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 ...................................................................................................... 358Chapter 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. 21. Data Structures and Algorithms Made Easy Narasimha Karumanchi Bellman-Ford Algorithm.................................................................................................... 388 Overview of Shortest Path Algorithms ............................................................................. 389 Minimal Spanning Tree ......................................................................................................... 389 Prims Algorithm ................................................................................................................ 390 Kruskal’s Algorithms .......................................................................................................... 391 Problems on Graph Algorithms ............................................................................................ 394Chapter 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. 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 ............................................................................................................... 451Chapter 12 Searching ..................................................................................................... 467 What is searching? ................................................................................................................. 467 Why Searching? ..................................................................................................................... 467 Types of Searching ................................................................................................................. 467 22 < | ©www.CareerMonk.com
  23. 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 .......................................................................................................... 471Chapter 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........................................................................................ 508Chapter 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 ........................................................................... 524Chapter 15 Hashing ........................................................................................................ 525 What is Hashing? ................................................................................................................... 525 Why Hashing? ........................................................................................................................ 525 HashTable ADT ...................................................................................................................... 525 23 < | ©www.CareerMonk.com
  24. 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 ............................................................................................................. 534Chapter 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. 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. 26. Data Structures and Algorithms Made Easy Narasimha Karumanchi The Construction of Suffix Trees ....................................................................................... 578 Applications of Suffix Trees ............................................................................................... 581 Problems on Strings ............................................................................................................... 582Chapter 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 .......................................................... 587Chapter 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. 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 ............................................................................................. 595Chapter 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 ......................................................................................... 613Chapter 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. 28. Data Structures and Algorithms Made Easy Narasimha KarumanchiChapter 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 ........................................................................................... 707Chapter 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. 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. 30. Data Structures and Algorithms Made Easy Narasimha Karumanchi 30 < | ©www.CareerMonk.com
  31. 31. Data Structures and Algorithms Made Easy Narasimha Karumanchi DATA STRUCTURES AND ALGORITHMS MADE EASY 31 < | ©www.CareerMonk.com
  32. 32. Data Structures and Algorithms Made Easy Narasimha Karumanchi 32 < | ©www.CareerMonk.com

×