Download Algorithm book by karumanchi and more Essays (university) Advanced Education in PDF only on Docsity!
To My Parents
-Laxmi and Modaiah
To My Family Members
To My Friends
To IIT Bombay
To All Hard Workers
Copyright ©2010 by CareerMonk.com
All rights reserved.
Designed by Narasimha Karumanchi
Printed in India
PPPP
PPPP
rrrr
rrrr
eeee
eeee
ffff
ffff
aaaa
aaaa
cccc
cccc
eeee
eeee
Dear DearDear
Dear Reader, Reader,Reader,
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 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.
If you read as a ᡱᡲᡳᡖᡗᡦᡲ preparing for competition exams for Computer Science/Information
Technology], the content of this book covers ᡓᡤᡤ the ᡰᡗᡩᡳᡡᡰᡗᡖ topics in full details. While writing the
book, an intense care has been taken to help students who are preparing for these kinds of exams.
In all the chapters you will see more importance given to problems and analyzing them instead of
concentrating more on theory. For each chapter, first you will see the basic required theory and then
followed by problems.
For many of the problems, ᡥᡳᡤᡲᡡᡨᡤᡗ solutions are provided with different complexities. We start with
ᡔᡰᡳᡲᡗ ᡘᡧᡰᡕᡗ solution and slowly move towards the ᡔᡗᡱᡲ ᡱᡧᡤᡳᡲᡡᡧᡦ possible for that problem. For each
problem we will try to understand how much time the algorithm is taking and how much memory
the algorithm is taking.
It is ᡰᡗᡕᡧᡥᡥᡗᡦᡖᡗᡖ that, at least one complete reading of this book is required to get full
understanding of all the topics. In the subsequent readings, readers can directly go to any chapter and
refer. Even though, enough readings were given for correcting the errors, due to human tendency
there could be some minor typos in the book. If any such typos found, they will be updated
at ᡵᡵᡵ. ᠩᡓᡰᡗᡗᡰᠹᡧᡦᡣ. ᡕᡧᡥ. I request readers to constantly monitor this site for any corrections, new
problems and solutions. Also, please provide your valuable suggestions at: ᠵᡦᡘᡧ@ᠩᡓᡰᡗᡗᡰᠹᡧᡦᡣ. ᡕᡧᡥ.
Wish you all the best. Have a nice reading.
N
N
a
a r
r a
a s
s i
i m
m h
h a
a K
K
a
a r
r u
u m
m a
a n
n c
c h
h i
i
M M--TTeecchh,, IIIITT BBoommbbaayy
FFoouunnddeerr ooff CCaarreeeerrMMoonnkk..ccoomm
TTTT
T TT
T aaaa
a aa
a bbbb
b bb
b llll
l ll
l eeee
e ee
e oooo
o oo
o ffff
f ff
f CCCC
C CC
C oooo
o oo
o nnnn
n nn
n tttt
t tt
t eeee
e ee
e nnnn
n nn
n tttt
t tt
t ssss
s ss
s
Chapter 1 Introduction ............................................................................................................. 29
Variables ................................................................................................................................................. 29
Data types ............................................................................................................................................... 29
System defined data types (Primitive data types) ............................................................................ 30
User defined data types...................................................................................................................... 30
Data Structure ........................................................................................................................................ 30
Abstract Data Types (ADT’s) ................................................................................................................. 31
Memory and Variables .......................................................................................................................... 31
Size of a Variable ................................................................................................................................ 32
Address of a Variable ......................................................................................................................... 32
Pointers................................................................................................................................................... 33
Declaration of Pointers ...................................................................................................................... 33
Pointers Usage .................................................................................................................................... 33
Pointer Manipulation ........................................................................................................................ 34
Arrays and Pointers ........................................................................................................................... 35
Dynamic Memory Allocation............................................................................................................ 36
Function Pointers............................................................................................................................... 36
Parameter Passing Techniques .............................................................................................................. 37
Actual and Formal Parameters .......................................................................................................... 37
Semantics of Parameter Passing ........................................................................................................ 38
Language Support for Parameter Passing Techniques ..................................................................... 38
Pass by Value...................................................................................................................................... 38
Pass by Result ..................................................................................................................................... 39
Pass by Value-Result.......................................................................................................................... 40
Pass by Reference (aliasing) .............................................................................................................. 41
Pass by Name...................................................................................................................................... 42
Binding ................................................................................................................................................... 43
Binding Times .................................................................................................................................... 43
Static Binding (Early binding) ........................................................................................................... 43
Dynamic Binding (Late binding)....................................................................................................... 43
Scope ....................................................................................................................................................... 44
- Static Scope......................................................................................................................................... - Dynamic Scope
- Storage Classes........................................................................................................................................
- Auto Storage Class..............................................................................................................................
- Extern storage class
- Register Storage Class
- Static Storage Class.............................................................................................................................
- Storage Organization
- Static Segment
- Stack Segment
- Heap Segment
- Shallow Copy versus Deep Copy...........................................................................................................
- Chapter 2 Analysis of Algorithms
- Introduction
- What is an Algorithm?
- Why Analysis of Algorithms?
- Goal of Analysis of Algorithms?............................................................................................................
- What is Running Time Analysis?..........................................................................................................
- How to Compare Algorithms?
- What is Rate of Growth?
- Commonly used Rate of Growths
- Types of Analysis
- Asymptotic Notation?
- Big-O Notation
- Big-O Visualization............................................................................................................................
- Big-O Examples
- No Uniqueness?
- Omega-Ω Notation.................................................................................................................................
- Theta-θ Notation....................................................................................................................................
- Θ Examples.........................................................................................................................................
- Important Notes
- Why is it called Asymptotic Analysis?
- Guidelines for Asymptotic Analysis?
- Properties of Notations
- Commonly used Logarithms and Summations
- Master Theorem for Divide and Conquer
- Problems Divide and Conquer Master Theorem..................................................................................
- Master Theorem for Subtract and Conquer Recurrences
- Variant of subtraction and conquer master theorem
- Problems on Algorithms Analysis.........................................................................................................
- Chapter 3 Recursion and Backtracking
- Introduction
- What is Recursion?
- Why Recursion?.....................................................................................................................................
- Format of a Recursive Function
- Recursion and Memory (Visualization)
- Recursion versus Iteration
- Notes on Recursion
- Example Algorithms of Recursion
- Problems on Recursion
- What is Backtracking?
- Example Algorithms Of Backtracking
- Problems On Backtracking
- Chapter 4 Linked Lists
- What is a Linked List?
- Linked Lists ADT
- Why Linked Lists?
- Arrays Overview
- Why Constant Time for Accessing Array Elements?.......................................................................
- Advantages of Arrays
- Disadvantages of Arrays
- Dynamic Arrays
- Advantages of Linked Lists
- Issues with Linked Lists (Disadvantages)..........................................................................................
- Comparison of Linked Lists with Arrays and Dynamic Arrays...........................................................
- Singly Linked Lists
- Basic Operations on a List
- Traversing the Linked List.................................................................................................................
- Singly Linked List Insertion
- Inserting a Node in Singly Linked List at the Beginning
- Inserting a Node in Singly Linked List at the Ending
- Inserting a Node in Singly Linked List in the Middle....................................................................
- Singly Linked List Deletion
- Deleting the First Node in Singly Linked List
- Deleting the last node in Singly Linked List
- Deleting an Intermediate Node in Singly Linked List
- Deleting Singly Linked List
- Doubly Linked Lists
- Doubly Linked List Insertion
- Inserting a Node in Doubly Linked List at the Beginning
- Inserting a Node in Doubly Linked List at the Ending
- Inserting a Node in Doubly Linked List in the Middle
- Doubly Linked List Deletion
- Deleting the First Node in Doubly Linked List
- Deleting the Last Node in Doubly Linked List
- Deleting an Intermediate Node in Doubly Linked List
- Circular Linked Lists
- Counting Nodes in a Circular List
- Printing the contents of a circular list
- Inserting a Node at the End of a Circular Linked List
- Inserting a Node at Front of a Circular Linked List
- Deleting the Last Node in a Circular List
- Deleting the First Node in a Circular List.......................................................................................
- Applications of Circular List............................................................................................................
- A Memory-Efficient Doubly Linked List
- Problems on Linked Lists
- Chapter 5 Stacks
- What is a Stack?
- How are Stacks Used?
- Stack ADT
- Main stack operations
- Auxiliary stack operations
- Exceptions
- Applications
- Implementation....................................................................................................................................
- Simple Array Implementation
- Dynamic Array Implementation
- Performance
- Linked List Implementation
- Performance
- Comparison of Implementations
- Comparing Incremental Strategy and Doubling Strategy..............................................................
- Comparing Array Implementation and Linked List Implementation...........................................
- Problems on Stacks
- Chapter 6 Queues
- What is a Queue?
- How are Queues Used?
- Queue ADT
- Main queue operations
- Auxiliary queue operations
- Exceptions
- Applications
- Direct applications
- Indirect applications
- Implementation....................................................................................................................................
- Why Circular Arrays?
- Simple Circular Array Implementation
- Performance & Limitations
- Dynamic Circular Array Implementation
- Performance
- Linked List Implementation
- Performance
- Comparison of Implementations
- Problems on Queues
- Chapter 7 Trees........................................................................................................................
- What is a Tree?
- Glossary
- Binary Trees
- Types of Binary Trees
- Properties of Binary Trees
- Structure of Binary Trees.................................................................................................................
- Operations on Binary Trees
- Applications of Binary Trees
- Binary Tree Traversals
- Traversal Possibilities
- Classifying the Traversals
- PreOrder Traversal
- InOrder Traversal
- PostOrder Traversal
- Level Order Traversal
- Problems on Binary Trees
- Generic Trees (N-ary Trees)
- Representation of Generic Trees
- Problems on Generic Trees
- Threaded Binary Tree Traversals [Stack or Queue less Traversals]
- Issues with Regular Binary Trees
- Motivation for Threaded Binary Trees
- Classifying Threaded Binary Trees
- Types of Threaded Binary Trees
- Threaded Binary Tree structure
- Difference between Binary Tree and Threaded Binary Tree Structures
- Finding Inorder Successor in Inorder Threaded Binary Tree
- Inorder Traversal in Inorder Threaded Binary Tree
- Finding PreOrder Successor in InOrder Threaded Binary Tree
- PreOrder Traversal of InOrder Threaded Binary Tree
- Insertion of Nodes in InOrder Threaded Binary Trees
- Problems on Threaded binary Trees
- Expression Trees
- Algorithm for Building Expression Tree from Postfix Expression
- Example
- XOR Trees
- Binary Search Trees (BSTs)
- Why Binary Search Trees?
- Binary Search Tree Property
- Binary Search Tree Declaration
- Operations on Binary Search Trees.................................................................................................
- Important Notes on Binary Search Trees
- Finding an Element in Binary Search Trees
- Finding an Minimum Element in Binary Search Trees
- Finding an Maximum Element in Binary Search Trees
- Where is Inorder Predecessor and Successor?
- Inserting an Element from Binary Search Tree..............................................................................
- Deleting an Element from Binary Search Tree
- Problems on Binary Search Trees
- Balanced Binary Search Trees
- Complete Balanced Binary Search Trees
- AVL (Adelson-Velskii and Landis) trees
- Properties of AVL Trees
- Minimum/Maximum Number of Nodes in AVL Tree
- AVL Tree Declaration......................................................................................................................
- Finding Height of an AVL tree
- Rotations...........................................................................................................................................
- Observation
- Types of Violations
- Single Rotations
- Double Rotations
- Insertion into an AVL tree
- Problems on AVL Trees
- Other Variations in Trees
- Red-Black Trees
- Splay Trees
- Augmented Trees
- Interval Trees
- Chapter 8 Priority Queue and Heaps
- What is a Priority Queue?
- Priority Queue ADT
- Main Priority Queues Operations
- Auxiliary Priority Queues Operations
- Priority Queue Applications
- Priority Queue Implementations
- Unordered Array Implementation
- Unordered List Implementation
- Ordered Array Implementation
- Ordered List Implementation..........................................................................................................
- Binary Search Trees Implementation
- Balanced Binary Search Trees Implementation
- Binary Heap Implementation
- Comparing Implementations...........................................................................................................
- Heaps and Binary Heap
- What is a Heap?
- Types of Heaps?
- Binary Heaps
- Representing Heaps
- Declaration of Heap
- Creating Heap
- Parent of a Node...............................................................................................................................
- Children of a Node
- Getting the Maximum Element
- Heapifying an Element
- Deleting an Element
- Inserting an Element
- Destroying Heap
- Heapifying the Array
- Heapsort
- Problems on Priority Queues [Heaps]
- Chapter 9 Disjoint Sets ADT...................................................................................................
- Introduction
- Equivalence Relations and Equivalence Classes.................................................................................
- Disjoint Sets ADT.................................................................................................................................
- Applications
- Tradeoffs in Implementing Disjoint Sets ADT
- Fast FIND Implementation (Quick FIND)
- Fast UNION Implementation (Quick UNION)
- Fast UNION implementation (Slow FIND)
- Fast UNION implementations (Quick FIND).....................................................................................
- UNION by Size.................................................................................................................................
- UNION by Height (UNION by Rank)
- Comparing UNION by Size and UNION by Height
- Path Compression
- Summary
- Problems on Disjoint Sets
- Chapter 10 Graph Algorithms
- Introduction
- Glossary
- Applications of Graphs
- Graph Representation
- Adjacency Matrix
- Adjacency List
- Adjacency Set
- Comparison of Graph Representations
- Graph Traversals
- Depth First Search [DFS]
- Breadth First Search [BFS]...............................................................................................................
- Comparing DFS and BFS
- Topological Sort
- Applications of Topological Sorting
- Shortest Path Algorithms
- Shortest Path in Unweighted Graph
- Shortest path in Weighted Graph [Dijkstra’s]
- Bellman-Ford Algorithm
- Overview of Shortest Path Algorithms...........................................................................................
- Minimal Spanning Tree
- Prim's Algorithm
- Kruskal’s Algorithm
- Problems on Graph Algorithms
- Chapter 11 Sorting
- What is Sorting?
- Why Sorting?
- Classification
- By Number of Comparisons
- By Number of Swaps
- By Memory Usage
- By Recursion
- By Stability
- By Adaptability
- Other Classifications
- Internal Sort
- External Sort.....................................................................................................................................
- Bubble sort
- Implementation................................................................................................................................
- Performance
- Selection Sort
- Algorithm
- Implementation................................................................................................................................
- Performance
- Insertion sort
- Advantages
- Algorithm
- Implementation................................................................................................................................
- Example
- Analysis
- Performance
- Comparisons to Other Sorting Algorithms
- Shell sort
- Implementation................................................................................................................................
- Analysis
- Performance
- Merge sort
- Important Notes
- Implementation................................................................................................................................
- Analysis
- Performance
- Heapsort
- Quicksort
- Algorithm
- Implementation................................................................................................................................
- Analysis
- Performance
- Randomized Quick sort
- Tree Sort
- Comparison of Sorting Algorithms
- Linear Sorting Algorithms
- Counting Sort
- Bucket sort [or Bin Sort]
- Radix sort..............................................................................................................................................