Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Algorithm book by karumanchi, Essays (university) of Advanced Education

Algorithm book by karumanchi

Typology: Essays (university)

2015/2016

Uploaded on 09/11/2016

Asggfgklgu
Asggfgklgu 🇮🇳

4

(1)

1 document

1 / 94

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e

Partial preview of the text

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.................................................................................................................................
      • Ω Examples
    • 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
      • Recursion
      • 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
    • Performance
  • Quicksort
    • Algorithm
    • Implementation................................................................................................................................
    • Analysis
    • Performance
    • Randomized Quick sort
  • Tree Sort
    • Performance
  • Comparison of Sorting Algorithms
  • Linear Sorting Algorithms
  • Counting Sort
  • Bucket sort [or Bin Sort]
  • Radix sort..............................................................................................................................................