Upload
careermonk-publications
View
17.616
Download
2
Embed Size (px)
DESCRIPTION
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
Citation preview
DDAATTAA SS TT RR UU CC TT UU RR EE SS
AA NN DD
AA LL GG OO RR II TT HH MM SS
MM AA DD EE EE AA SS YY
CCaarreeeerrMMoonnkk PPuubblliiccaattiioonnss
..
MM--TTeecchh,, IIIITT BBoommbbaayy
SSuucccceessss KKeeyy ffoorr::
•• CCaammppuuss PPrreeppaarraattiioonn
•• DDeeggrreeee//MMaasstteerrss CCoouurrssee PPrreeppaarraattiioonn
•• IInnssttrruuccttoorr’’ss
•• GGAATTEE PPrreeppaarraattiioonn
•• BBiigg JJoobb hhuunntteerrss:: MMiiccrroossoofftt,, GGooooggllee,, AAmmaazzoonn,,
YYaahhoooo,, FFaacceebbooookk,, AAddoobbee aanndd mmaannyy mmoorree
•• RReeffeerreennccee MMaannuuaall ffoorr WWoorrkkiinngg PPeeooppllee
Multiple
Solutions
with different
complexities
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
2 < | CareerMonk Publications ™
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
3 < | CareerMonk Publications ™
To My Parents -Laxmi and Modaiah To My Family Members To My Friends To IIT Bombay To All Hard Workers
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
4 < | CareerMonk Publications ™
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
5 < | CareerMonk Publications ™
Copyright ©2010 by CareerMonk.com
All rights reserved.
Designed by Narasimha Karumanchi
Printed in India
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
6 < | CareerMonk Publications ™
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
7 < | CareerMonk Publications ™
AAAAAAAAcccccccckkkkkkkknnnnnnnnoooooooowwwwwwwwlllllllleeeeeeeeddddddddggggggggeeeeeeeemmmmmmmmeeeeeeeennnnnnnnttttttttssssssss
I would like to thank for encouraging me when I was at IIT Bombay. He is the first person who taught me the importance of and its . From that
day I keep on updating myself.
I would like to thank [ ] and [] for spending time in
reviewing this book and providing me the suggestions.
I would like to thank [! ""#, % ] for spending his valuable time in
reviewing the book and suggestions. Being a PhD holder in algorithms his review gave me
the confidence in the quality of the book.
I would like to thank and & [Founder’s of #'#('#(. ] for approaching
me for teaching of Algorithms and Data Structures at their GATE centers. They are the
primary reason for initiation of this book.
I would like to thank # [Founder of '. . ] for providing me the valuable suggestions in writing, reviewing and for providing coding solutions to some of the
problems.
Last but not least, I would like to thank Director’s of ,
[Director of '( ( '], #) * [Former Director of '!#] and + ) [) , (
, % ] for helping me and my family during our studies.
Also, my 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, , for her encouragement and help during writing
of this book.
-------- NNaarraassiimmhhaa KKaarruummaanncchhii
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
8 < | CareerMonk Publications ™
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
9 < | CareerMonk Publications ™
PPPPPPPPrrrrrrrreeeeeeeeffffffffaaaaaaaacccccccceeeeeeee
Dear Reader,Dear Reader,Dear Reader,Dear Reader,
Please Hold onPlease Hold onPlease Hold onPlease Hold on! I know many people do not read preface. But I would like to strongly recommend reading preface of this book at least. This preface has
from regular prefaces.
As a , 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 , you will give better lectures with easy go approach and as a
result your students will feel proud for selecting Computer Science / Information Technology
as their degree.
This book is very much useful for the of ( and during their
academic preparations. All the chapters of this book contain theory and their related
problems as many as possible. There a total of approximately 700 algorithmic puzzles and all
of them are with solutions.
Finally if you read as a preparing for competition exams [like
' # ( ('#(), /)/0 or any other exam for Computer
Science/Information Technology], then the content of this book covers the 1 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, solutions are provided with different complexities. First, we start with solution and slowly move towards the possible for
that problem. For each problem we will try to understand how much time the algorithm is
taking and how much memory the algorithm is taking.
It is that, at least one complete reading of this book is required to get full understanding of all the topics. In the subsequent readings, readers can directly go to any
chapter and refer.
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
10 < | CareerMonk Publications ™
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:
"@ . .
Always remember the following quotes by 3 which will give you full energy
and confidence.
Wish you all the best. Have a nice reading.
--------NNaarraassiimmhhaa KKaarruummaanncchhii
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
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
11 < | CareerMonk Publications ™
TTTTTTTTaaaaaaaabbbbbbbblllllllleeeeeeee ooooooooffffffff CCCCCCCCoooooooonnnnnnnntttttttteeeeeeeennnnnnnnttttttttssssssss
Chapter 1 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
Declaration of Pointers ........................................................................................................ 38
Pointers Usage ...................................................................................................................... 38
Pointer Manipulation ........................................................................................................... 39
Arrays and Pointers .............................................................................................................. 40
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 ........................................................................................................................ 43
Pass by Result........................................................................................................................ 44
Pass by Value-Result ............................................................................................................ 46
Pass by reference (aliasing) .................................................................................................. 47
Pass by name ......................................................................................................................... 48
Binding ...................................................................................................................................... 48
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
12 < | CareerMonk Publications ™
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 ....................................................................................................................... 63
Shallow copy versus Deep copy .............................................................................................. 65
Chapter 2 Analysis of Algorithms.................................................................................. 66
Introduction ............................................................................................................................. 66
What is an Algorithm? ............................................................................................................. 66
Why Analysis of Algorithms? ................................................................................................. 67
Goal of Analysis of Algorithms? .............................................................................................. 67
What is Running Time Analysis? ............................................................................................ 67
How to Compare Algorithms? ................................................................................................. 67
What is Rate of Growth? ......................................................................................................... 68
Commonly used Rate of Growths ........................................................................................... 68
Types of Analysis ...................................................................................................................... 69
Asymptotic notation? ............................................................................................................... 70
Big-O notation .......................................................................................................................... 70
Big-O Visualization .............................................................................................................. 71
Big-O examples ..................................................................................................................... 72
No uniqueness? ..................................................................................................................... 72
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
13 < | CareerMonk Publications ™
Omega-Ω notation.................................................................................................................... 73
Ω Examples............................................................................................................................ 73
Theta-θ notation ....................................................................................................................... 74
θ Examples ............................................................................................................................ 74
Important Notes .................................................................................................................... 75
Why is it called Asymptotic Analysis?.................................................................................... 75
Guidelines for asymptotic analysis? ........................................................................................ 76
Properties of notations ............................................................................................................. 78
Commonly used logarithms and summations ........................................................................ 79
Master Theorem for Divide and Conquer .............................................................................. 79
Problems on Master Theorem for Divide and Conquer......................................................... 80
Master Theorem for Subtract and Conquer Recurrences ...................................................... 82
Variant of subtraction and conquer master theorem ......................................................... 82
Problems on Algorithms Analysis ........................................................................................... 83
Chapter 3 Recursion and Backtracking ......................................................................... 99
Introduction ............................................................................................................................. 99
What is recursion? ................................................................................................................... 99
Why recursion? ........................................................................................................................ 99
Format of a recursive function ................................................................................................ 99
Recursion and Memory (Visualization) ................................................................................ 100
Recursion versus Iteration ..................................................................................................... 102
Recursion ............................................................................................................................ 102
Iteration ............................................................................................................................... 102
Notes On Recursion ............................................................................................................... 102
Example Algorithms of Recursion ........................................................................................ 102
Problems on recursion ........................................................................................................... 103
What is backtracking? ............................................................................................................ 104
Example Algorithms Of Backtracking .................................................................................. 104
Problems On Backtracking .................................................................................................... 105
Chapter 4 Linked Lists .................................................................................................. 107
What is a Linked List? ............................................................................................................ 107
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
14 < | CareerMonk Publications ™
Linked Lists ADT ................................................................................................................... 107
Main Linked Lists Operations ............................................................................................ 107
Auxiliary Linked Lists Operations ..................................................................................... 107
Why Linked Lists? .................................................................................................................. 107
Arrays Overview................................................................................................................. 108
Why constant time for accessing array elements?............................................................ 108
Advantages of arrays .......................................................................................................... 108
Disadvantages of arrays ...................................................................................................... 108
Dynamic Arrays .................................................................................................................. 109
Advantages of Linked Lists ................................................................................................ 109
Issues with Linked Lists (Disadvantages) .......................................................................... 109
Comparison of Linked Lists with Arrays and Dynamic Arrays ........................................... 110
Singly Linked Lists ................................................................................................................. 110
Basic Operations on a List .................................................................................................. 110
Traversing the Linked List ................................................................................................. 111
Singly Linked List Insertion .................................................................................................. 111
Inserting a node in Singly Linked List at the beginning .................................................. 112
Inserting a node in Singly Linked List at the ending ....................................................... 112
Inserting a node in Singly Linked List in the middle ....................................................... 113
Singly Linked List Deletion ................................................................................................... 115
Deleting the first node in Singly Linked List .................................................................... 115
Deleting the last node in Singly Linked List ..................................................................... 115
Deleting an intermediate node in Singly Linked List ....................................................... 116
Deleting Singly Linked List ................................................................................................... 118
Doubly Linked Lists ............................................................................................................... 118
Doubly Linked List Insertion ................................................................................................ 119
Inserting a node in Doubly Linked List at the beginning ................................................ 119
Inserting a node in Doubly Linked List at the ending ..................................................... 119
Inserting a node in Doubly Linked List in the middle ..................................................... 120
Doubly Linked List Deletion ................................................................................................. 122
Deleting the first node in Doubly Linked List .................................................................. 122
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
15 < | CareerMonk Publications ™
Deleting the last node in Doubly Linked List ................................................................... 123
Deleting an intermediate node in Doubly Linked List ..................................................... 123
Circular Linked Lists .............................................................................................................. 125
Counting nodes in a circular list ........................................................................................ 126
Printing the contents of a circular list ............................................................................... 127
Inserting a node at the end of a Circular Linked List ....................................................... 127
Inserting a node at front of a Circular Linked List ........................................................... 129
Deleting the last node in a circular list ............................................................................. 131
Deleting the first node in a circular list ............................................................................ 132
Applications of circular list ................................................................................................ 133
A Memory-Efficient Doubly Linked List .............................................................................. 133
Problems on Linked Lists ....................................................................................................... 135
Chapter 5 Stacks ........................................................................................................... 160
What is a Stack? ..................................................................................................................... 160
How are stacks used? .............................................................................................................. 160
Stack ADT ............................................................................................................................... 161
Main stack operations......................................................................................................... 161
Auxiliary stack operations .................................................................................................. 161
Exceptions ........................................................................................................................... 161
Applications ............................................................................................................................ 161
Implementation ...................................................................................................................... 162
Simple Array Implementation ........................................................................................... 162
Dynamic Array Implementation ....................................................................................... 164
Performance ........................................................................................................................ 167
Linked List Implementation .............................................................................................. 168
Performance ........................................................................................................................ 170
Comparison of Implementations ........................................................................................... 170
Comparing Incremental Strategy and Doubling Strategy ................................................ 170
Comparing Array Implementation and Linked List Implementation ............................. 170
Problems on Stacks ................................................................................................................ 171
Chapter 6 Queues ......................................................................................................... 197
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
16 < | CareerMonk Publications ™
What is a queue? .................................................................................................................... 197
How are queues used? ............................................................................................................ 197
Queue ADT ............................................................................................................................. 198
Main queue operations ....................................................................................................... 198
Auxiliary queue operations ................................................................................................ 198
Exceptions ............................................................................................................................... 198
Applications ............................................................................................................................ 198
Direct applications .............................................................................................................. 198
Indirect applications ........................................................................................................... 199
Implementation ...................................................................................................................... 199
Why Circular Arrays? ........................................................................................................ 199
Simple Circular Array Implementation ............................................................................ 200
Performance & Limitations ................................................................................................ 202
Dynamic Circular Array Implementation ......................................................................... 203
Performance ........................................................................................................................ 205
Linked List Implementation .............................................................................................. 205
Performance ........................................................................................................................ 207
Comparison of Implementations ....................................................................................... 208
Problems on Queues .............................................................................................................. 208
Chapter 7 Trees ............................................................................................................. 212
What is a tree? ........................................................................................................................ 212
Glossary ................................................................................................................................... 212
Binary Trees ............................................................................................................................ 213
Types of Binary Trees ......................................................................................................... 214
Properties of Binary Trees.................................................................................................. 215
Structure of Binary Trees ................................................................................................... 216
Operations on Binary Trees ............................................................................................... 217
Applications of Binary Trees .............................................................................................. 217
Binary Tree Traversals ........................................................................................................... 217
Traversal Possibilities ......................................................................................................... 218
Classifying the Traversals ................................................................................................... 218
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
17 < | CareerMonk Publications ™
Preorder Traversal .............................................................................................................. 219
Inorder Traversal ................................................................................................................ 221
Postorder Traversal ............................................................................................................. 222
Level order Traversal .......................................................................................................... 224
Problems on Binary Trees .................................................................................................. 225
Generic Trees (N-ary Trees) .................................................................................................. 252
Representation of generic trees ......................................................................................... 253
Problems on Generic Trees ................................................................................................ 254
Threaded Binary Tree Traversals [Stack or Queue less Traversals] .................................... 263
Issues with regular Binary Trees ........................................................................................ 263
Motivation for threaded binary trees ................................................................................ 263
Classifying threaded binary trees ...................................................................................... 264
Types of threaded binary trees .......................................................................................... 264
Threaded binary tree structure .......................................................................................... 264
Difference between Binary Tree and Threaded Binary Tree Structures ......................... 265
Finding Inorder Successor in Inorder Threaded Binary Tree .......................................... 266
Inorder Traversal in Inorder Threaded Binary Tree ........................................................ 267
Finding Preorder Successor in Inorder Threaded Binary Tree ........................................ 268
Preorder Traversal of Inorder Threaded Binary Tree ...................................................... 268
Insertion of Nodes in Inorder Threaded Binary Trees ..................................................... 269
Problems on Threaded binary Trees ................................................................................. 271
Expression Trees ..................................................................................................................... 273
Algorithm for Building Expression Tree from Postfix Expression .................................. 273
Example: .............................................................................................................................. 274
XOR Trees ............................................................................................................................... 276
Binary Search Trees (BSTs) .................................................................................................... 278
Why Binary Search Trees? ................................................................................................. 278
Binary Search Tree Property.............................................................................................. 278
Binary Search Tree Declaration ......................................................................................... 279
Operations on Binary Search Trees ................................................................................... 279
Important Notes on Binary Search Trees .......................................................................... 279
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
18 < | CareerMonk Publications ™
Finding an Element in Binary Search Trees ..................................................................... 280
Finding an Minimum Element in Binary Search Trees.................................................... 281
Finding an Maximum Element in Binary Search Trees ................................................... 282
Where is Inorder Predecessor and Successor? .................................................................. 283
Inserting an Element from Binary Search Tree ................................................................ 284
Deleting an Element from Binary Search Tree ................................................................. 285
Problems on Binary Search Trees ...................................................................................... 287
Balanced Binary Search Trees ............................................................................................... 299
Complete balanced binary search trees ............................................................................. 299
AVL (Adelson-Velskii and Landis) trees .............................................................................. 299
Properties of AVL Trees ..................................................................................................... 299
Minimum/Maximum Number of Nodes in AVL Tree...................................................... 300
AVL Tree declaration ......................................................................................................... 301
Finding height of an AVL tree ........................................................................................... 301
Rotations ............................................................................................................................. 302
Observation ......................................................................................................................... 302
Types of violations .............................................................................................................. 302
Single rotations ................................................................................................................... 303
Double rotations ................................................................................................................. 305
Insertion into an AVL tree ................................................................................................. 307
Problems on AVL Trees ..................................................................................................... 308
Other Variations in Trees ...................................................................................................... 314
Red-Black Trees .................................................................................................................. 314
Splay Trees .......................................................................................................................... 314
Augmented Trees ................................................................................................................ 315
Interval Trees ...................................................................................................................... 316
Chapter 8 Priority Queue and Heaps .......................................................................... 318
What is a Priority Queue? ..................................................................................................... 318
Priority Queue ADT............................................................................................................... 318
Main Priority Queues Operations ..................................................................................... 318
Auxiliary Priority Queues Operations .............................................................................. 319
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
19 < | CareerMonk Publications ™
Priority Queue Applications .................................................................................................. 319
Priority Queue Implementations .......................................................................................... 319
Unordered Array Implementation .................................................................................... 319
Unordered List Implementation ........................................................................................ 319
Ordered Array Implementation ........................................................................................ 320
Ordered List Implementation ............................................................................................ 320
Binary Search Trees Implementation ................................................................................ 320
Balanced Binary Search Trees Implementation ................................................................ 320
Binary Heap Implementation ............................................................................................ 320
Comparing Implementations ............................................................................................. 320
Heaps and Binary Heap .......................................................................................................... 321
What is a heap? ................................................................................................................... 321
Types of heaps? ................................................................................................................... 321
Binary Heaps .......................................................................................................................... 322
Representing Heaps ............................................................................................................ 322
Parent of a Node ................................................................................................................. 323
Children of a Node ............................................................................................................. 323
Getting the Maximum Element ......................................................................................... 323
Heapifying an Element ....................................................................................................... 323
Deleting an Element ........................................................................................................... 326
Inserting an Element .......................................................................................................... 326
Heapifying the Given Data (Array) ................................................................................... 328
Heapsort .............................................................................................................................. 329
Problems on Priority Queues [Heaps] .................................................................................. 330
Chapter 9 Disjoint Sets ADT ........................................................................................ 344
Introduction ........................................................................................................................... 344
Equivalence Relations and Equivalence Classes ................................................................... 344
Disjoint Sets ADT ................................................................................................................... 345
Applications ............................................................................................................................ 345
Tradeoffs in Implementing Disjoint Sets ADT operations ................................................... 346
Fast FIND Implementation (Quick FIND) ........................................................................ 346
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
20 < | CareerMonk Publications ™
Fast UNION Implementation (Quick UNION) ................................................................ 347
Fast UNION implementation (Slow FIND) .......................................................................... 347
Fast UNION implementations (Quick FIND) ....................................................................... 350
UNION by Size ................................................................................................................... 351
UNION by Height (UNION by Rank) ............................................................................... 352
Comparing UNION by Size and UNION by Height ......................................................... 353
Further Improving Fast UNION Implementations (Path Compression) ........................ 354
Summary ................................................................................................................................. 355
Problems on Disjoint Sets ...................................................................................................... 355
Chapter 10 Graph Algorithms ....................................................................................... 357
Introduction ........................................................................................................................... 357
Glossary ................................................................................................................................... 357
Applications of graphs ........................................................................................................... 361
Graph Representation ............................................................................................................ 361
Adjacency Matrix ............................................................................................................... 361
Adjacency List ..................................................................................................................... 363
Adjacency Set ...................................................................................................................... 365
Comparison of Graph Representations ............................................................................. 366
Graph Traversals .................................................................................................................... 366
Depth First Search [DFS] ................................................................................................... 366
Breadth First Search [BFS] ................................................................................................. 371
Comparing DFS and BFS .................................................................................................... 374
Topological Sort ...................................................................................................................... 375
Applications of Topological Sorting .................................................................................. 377
Shortest Path Algorithms....................................................................................................... 377
Shortest path in unweighted graph ................................................................................... 377
Shortest path in weighted graph [Dijkstra’s] .................................................................... 379
Bellman-Ford Algorithm .................................................................................................... 384
Overview of Shortest Path Algorithms ............................................................................. 385
Minimal Spanning Tree ......................................................................................................... 385
Prim's Algorithm ................................................................................................................ 386
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
21 < | CareerMonk Publications ™
Kruskal’s Algorithms .......................................................................................................... 387
Problems on Graph Algorithms ............................................................................................ 391
Chapter 11 Sorting .......................................................................................................... 427
What is sorting? ...................................................................................................................... 427
Why Sorting? .......................................................................................................................... 427
Classification ........................................................................................................................... 427
By number of comparisons ................................................................................................ 427
By number of swaps ........................................................................................................... 427
By Memory Usage............................................................................................................... 428
By Recursion ....................................................................................................................... 428
By Stability .......................................................................................................................... 428
By Adaptability ................................................................................................................... 428
Other classifications ............................................................................................................... 428
Internal Sort ........................................................................................................................ 428
External Sort ....................................................................................................................... 429
Bubble sort .............................................................................................................................. 429
Implementation .................................................................................................................. 429
Performance ........................................................................................................................ 430
Selection Sort .......................................................................................................................... 430
Algorithm ............................................................................................................................ 431
Implementation .................................................................................................................. 431
Performance ........................................................................................................................ 431
Insertion sort .......................................................................................................................... 431
Insertion advantages ........................................................................................................... 432
Algorithm ............................................................................................................................ 432
Implementation .................................................................................................................. 432
Example ............................................................................................................................... 433
Analysis ............................................................................................................................... 433
Performance ........................................................................................................................ 434
Comparisons to other sorting algorithms .......................................................................... 434
Shell sort ................................................................................................................................. 434
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
22 < | CareerMonk Publications ™
Implementation .................................................................................................................. 435
Analysis ............................................................................................................................... 435
Performance ........................................................................................................................ 436
Merge sort ............................................................................................................................... 436
Important Notes .................................................................................................................. 436
Implementation .................................................................................................................. 437
Analysis ............................................................................................................................... 438
Performance ........................................................................................................................ 438
Heapsort .................................................................................................................................. 438
Performance ........................................................................................................................ 439
Quicksort ................................................................................................................................ 439
Algorithm ............................................................................................................................ 439
Implementation .................................................................................................................. 439
Analysis ............................................................................................................................... 440
Performance ........................................................................................................................ 442
Randomized Quick sort ...................................................................................................... 442
Tree Sort ................................................................................................................................. 443
Performance ........................................................................................................................ 443
Comparison of sorting algorithms ......................................................................................... 443
Linear Sorting Algorithms ..................................................................................................... 444
Counting Sort ......................................................................................................................... 444
Bucket sort [or Bin Sort] ........................................................................................................ 446
Radix sort ................................................................................................................................ 446
Topological Sort ...................................................................................................................... 447
External Sorting ...................................................................................................................... 447
Problems on Sorting ............................................................................................................... 449
Chapter 12 Searching ..................................................................................................... 467
What is searching? ................................................................................................................. 467
Why Searching? ..................................................................................................................... 467
Types of Searching ................................................................................................................. 467
Unordered Linear Search ....................................................................................................... 467
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
23 < | CareerMonk Publications ™
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
Introduction ........................................................................................................................... 522
What are Symbol Tables? ...................................................................................................... 522
Symbol Table implementations ............................................................................................. 523
Unordered Aarray Implementation ................................................................................... 523
Ordered [Sorted] Array Implementation .......................................................................... 523
Unordered Linked List Implementation ........................................................................... 523
Ordered Linked List Implementation ............................................................................... 523
Binary Search Trees Implementation ................................................................................ 524
Balanced Binary Search Trees Implementation ................................................................ 524
Ternary Search Implementation ........................................................................................ 524
Hashing Implementation ................................................................................................... 524
Comparison Symbol Ttable Implementations ...................................................................... 524
Chapter 15 Hashing ........................................................................................................ 525
What is Hashing? ................................................................................................................... 525
Why Hashing? ........................................................................................................................ 525
HashTable ADT ...................................................................................................................... 525
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
24 < | CareerMonk Publications ™
Understanding Hashing ......................................................................................................... 525
If Arrays Are There Why Hashing? .................................................................................. 526
Components in Hashing ........................................................................................................ 527
Hash Table .............................................................................................................................. 528
Hash Function ........................................................................................................................ 528
How to Choose Hash Function? ............................................................................................ 528
Characteristics of Good Hash Functions ........................................................................... 528
Load Factor ............................................................................................................................. 529
Collisions ................................................................................................................................ 529
Collision Resolution Techniques ........................................................................................... 529
Separate Chaining ............................................................................................................... 529
Open Addressing ................................................................................................................ 530
Comparing Collision Resolution Techniques ....................................................................... 530
Comparisons: Linear Probing 45. Double Hashing .......................................................... 530
Comparisons: Open Addressing 45. Separate Chaining ................................................... 530
Comparisons: Open Addressing methods ........................................................................ 531
How Hashing Gets O(1) Complexity? ................................................................................... 531
Hashing Techniques ............................................................................................................... 532
Static Hashing ..................................................................................................................... 532
Dynamic Hashing ............................................................................................................... 532
Problems for which Hash Tables are not Suitable ............................................................... 532
Problems on Hashing ............................................................................................................. 532
Chapter 16 String Algorithms ........................................................................................ 548
Introduction ........................................................................................................................... 548
String Matching Algorithms .................................................................................................. 548
Brute Force Method ............................................................................................................... 549
Robin-Karp String Matching Algorithm .............................................................................. 549
Selecting Hash Function .................................................................................................... 550
Step by Step explanation .................................................................................................... 551
String Matching with Finite Automata ................................................................................ 551
Finite Automata .................................................................................................................. 551
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
25 < | CareerMonk Publications ™
How Finite Automata Works? ........................................................................................... 552
Important Notes for Constructing the Finite Automata .................................................. 552
Matching Algorithm ........................................................................................................... 552
KMP Algorithm ...................................................................................................................... 553
Filling Prefix Table ............................................................................................................. 553
Matching Algorithm ........................................................................................................... 555
Boyce-Moore Algorithm ........................................................................................................ 558
Data structures for Storing Strings ........................................................................................ 559
Hash Tables for Strings .......................................................................................................... 559
Binary Search Trees for Strings ............................................................................................. 559
Issues with Binary Search Tree Representation ............................................................... 560
Tries ........................................................................................................................................ 560
What is a Trie? .................................................................................................................... 560
Why Tries? .......................................................................................................................... 561
Inserting a String in Trie .................................................................................................... 561
Searching a String in Trie ................................................................................................... 562
Issues with Tries Representation ....................................................................................... 562
Ternary Search Trees ............................................................................................................. 563
Ternary Search Trees Declaration ..................................................................................... 563
Inserting strings in TST ...................................................................................................... 564
Searching in Ternary Search Trees .................................................................................... 566
Displaying All Words of Ternary Search Tree ................................................................. 567
Finding Maximum Length of Words in TST .................................................................... 568
Comparing Binary Search Trees, Tries and Ternary Search Trees ...................................... 568
Suffix Trees ............................................................................................................................. 569
Prefix and Suffix ................................................................................................................. 569
Observation ......................................................................................................................... 569
What is a Suffix Tree? ........................................................................................................ 569
The Construction of Suffix Trees ....................................................................................... 570
Applications of Suffix Trees ............................................................................................... 573
Problems on Strings ............................................................................................................... 573
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
26 < | CareerMonk Publications ™
Chapter 17 Algorithms Design Techniques .................................................................. 581
Introduction ........................................................................................................................... 581
Classification ........................................................................................................................... 581
Classification by Implementation Method ........................................................................... 581
Recursion or Iteration ........................................................................................................ 581
Procedural or Declarative (Non-Procedural) .................................................................... 582
Serial or Parallel or Distributed ......................................................................................... 582
Deterministic or Non-Deterministic ................................................................................. 582
Exact or Approximate ......................................................................................................... 582
Classification by Design Method ........................................................................................... 582
Greedy Method ................................................................................................................... 582
Divide and Conquer ........................................................................................................... 583
Dynamic Programming ...................................................................................................... 583
Linear Programming .......................................................................................................... 583
Reduction [Transform and Conquer] ................................................................................ 583
Other Classifications .............................................................................................................. 584
Classification by Research Area ......................................................................................... 584
Classification by Complexity ............................................................................................. 584
Randomized Algorithms .................................................................................................... 584
Branch and Bound Enumeration and Backtracking ......................................................... 584
Chapter 18 Greedy Algorithms ...................................................................................... 585
Introduction ........................................................................................................................... 585
Greedy strategy ...................................................................................................................... 585
Elements of Greedy Algorithms ............................................................................................ 585
Greedy choice property ...................................................................................................... 585
Optimal substructure .......................................................................................................... 585
Does Greedy Works Always? ................................................................................................ 586
Advantages and Disadvantages of Greedy Method .............................................................. 586
Greedy Applications ............................................................................................................... 586
Understanding Greedy Technique ........................................................................................ 586
Huffman coding algorithm ................................................................................................ 587
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
27 < | CareerMonk Publications ™
Problems on greedy algorithms ............................................................................................. 591
Chapter 19 Divide and Conquer Algorithms ................................................................ 603
Introduction ........................................................................................................................... 603
What is Divide and Conquer Strategy? ................................................................................. 603
Does Divide and Conquer Work Always? ............................................................................ 603
Divide and Conquer Visualization ........................................................................................ 603
Understanding Divide and Conquer ..................................................................................... 604
Advantages of Divide and Conquer ...................................................................................... 605
Disadvantages of Divide and Conquer .................................................................................. 606
Master Theorem ..................................................................................................................... 606
Divide and Conquer Applications ......................................................................................... 606
Problems on Divide and Conquer ......................................................................................... 607
Chapter 20 Dynamic Programming ............................................................................... 625
Introduction ........................................................................................................................... 625
What is Dynamic Programming Strategy? ........................................................................... 625
Can Dynamic Programming Solve Any Problem? ............................................................... 625
Dynamic Programming Approaches ..................................................................................... 626
Bottom-up dynamic programming .................................................................................... 626
Top-down dynamic programming..................................................................................... 626
Bottom-up versus Top-down programming ..................................................................... 626
Examples of Dynamic Programming Algorithms ................................................................. 626
Understanding Dynamic Programming ................................................................................ 627
Fibonacci Series .................................................................................................................. 627
Observations ....................................................................................................................... 630
Factorial of a number ......................................................................................................... 630
Problems on Dynamic Programming .................................................................................... 632
Chapter 21 Complexity Classes ...................................................................................... 684
Introduction ........................................................................................................................... 684
Polynomial/exponential time ................................................................................................ 684
What is Decision Problem? ................................................................................................... 685
Decision Procedure ................................................................................................................ 685
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
28 < | CareerMonk Publications ™
What is a Complexity Class? .................................................................................................. 685
Types of Complexity Classes .................................................................................................. 686
P Class ................................................................................................................................. 686
NP Class ............................................................................................................................... 686
Co-NP Class ........................................................................................................................ 686
Relationship between P, NP and Co-NP ........................................................................... 686
NP-hard Class ..................................................................................................................... 687
NP-complete Class .............................................................................................................. 687
Relationship between P, NP Co-NP, NP-Hard and NP-Complete.................................. 688
Does P==NP? ....................................................................................................................... 688
Reductions .............................................................................................................................. 689
Important NP-Complete Problems (Reductions) ............................................................. 690
Problems on Complexity Classes ........................................................................................... 692
Chapter 22 Miscellaneous Concepts .............................................................................. 696
Introduction ........................................................................................................................... 696
Hacks on Bitwise Programming ............................................................................................ 696
Bitwise AND ....................................................................................................................... 696
Bitwise OR .......................................................................................................................... 696
Bitwise Exclusive-OR ......................................................................................................... 697
Bitwise Left Shift ................................................................................................................ 697
Bitwise Right Shift .............................................................................................................. 697
Bitwise Complement .......................................................................................................... 698
Checking whether K-th bit is set or not ........................................................................... 698
Setting K-th bit ................................................................................................................... 698
Clearing K-th bit ................................................................................................................. 698
Toggling K-th bit ................................................................................................................ 699
Toggling Rightmost One bit .............................................................................................. 699
Isolating Rightmost One bit ............................................................................................... 699
Isolating Rightmost Zero bit .............................................................................................. 700
Checking Whether Number is Power of 2 not ................................................................. 700
Multiplying Number by Power of 2 .................................................................................. 701
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
29 < | CareerMonk Publications ™
Dividing Number by Power of 2 ....................................................................................... 701
Finding Modulo of a Given Number ................................................................................. 701
Reversing the Binary Number ........................................................................................... 701
Counting Number of One’s in number ............................................................................. 702
Creating for Mask for Trailing Zero’s ................................................................................ 704
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
30 < | CareerMonk Publications ™
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
31 < | CareerMonk Publications ™
DDAATTAA SSTTRRUUCCTTUURREESS AANNDD AALLGGOORRIITTHHMMSS MMAADDEE EEAASSYY
Data Structures and Algorithms Made Easy ©www.CareerMonk.com
32 < | CareerMonk Publications ™