Contents i advanced anced data structures and algorithms FOR m.tech (jntu - hyderabad) i year i semester (COMMON TO CSE, CS,, IT,, WT AND SE) CONTENTS UNIT - I [CH. H. - 1] ] [INTRODUCTION INTRODUCTION]... 1.1-1.46 1.1 INTRODUCTION TO O ALGORITHM... 1.2 1.1.1 Definition of Algorithm... 1.2 1.1.2 Notation of Algorithm... 1.2 1.1.3 Properties of Algorithm... 1.3 1.1.3.1 A Typical Example... 1.3 1.1.4 Process for Design and Analysis of Algorithms... 1.4 1.1.4.1 Understanding the Problem... 1.5 1.1.4.2 Desire Problem Solving Technique echnique... 1.5 1.1.4.3 Design of an Algorithm... 1.5 1.1.4.4 Validation of an Algorithm... 1.5 1.1.4.5 Analyze the Algorithm... 1.5 1.1.4.6 Coding of an Algorithm... 1.6 1.1.4.7 Testing of an Algorithm... 1.6 1.1.5 PSEUDO Code for Expressing Algorithms... 1.7 1.2 PERFORMANCE ANALYSIS... 1.15 1.2.1 Space Complexity... 1.17
ii Contents 1.2.2 Time Complexity... 1.20 1.2.2.1 The Unit of Algorithm s Runun-Time... 1.23 1.2.2.2 The Order of Growth... 1.24 1.2.2.3 Best Case, Average Case and Worst Case Efficiencies... 1.25 1.2.2.4 Solved Problems... 1.26 1.2.3 Comparison Between Priori Analysis and Posteriori Analysis... 1.29 1.3 ASYMPTOTIC TIC NOTATION TION... 1.30 1.3.1 Big-Oh (O) Notation... 1.30 1.3.1.1 Big-Oh Ratio Theorem... 1.32 1.3.1.2 Big-oh Notation Theorem... 1.33 1.3.1.3 Basic Efficiency Classes... 1.34 1.3.2 Omega (Ω) ) Notation... 1.36 1.3.2.1 Big Omega Ratio Theorem... 1.38 1.3.2.2 Little-Omega Notation (w)... 1.38 1.3.3 Theta (θ) ) Notation... 1.39 1.3.3.1 Theta Ratio Theorem... 1.40 1.3.3.2 Theta Notation Theorem... 1.40 1.3.4 Small-oh (o) Notation... 1.41 1.3.5 Order of Growth using Limits (Alternative Definitions for Asymptotic Notations)... 1.41 1.4 COMPLEXITY ANALYSIS EXAMPLES... 1.45
Contents iii UNIT - I [CH. H. - 2] ] [DATA STRUCTURES]... 1.47-1.112 2.1 INTRODUCTION TO DATA A STRUCTURES... 1.48 2.2 LINEAR AND NON LINEAR DATA A STRUCTURES... 1.49 2.2.1 Linear Data Structure... 1.49 2.2.2 Non-Linear Data Structure... 1.49 2.3 ABSTRACT DATA TYPE (ADT) CONCEPT... 1.50 2.3.1 Linear List ADT... 1.51 2.3.2 Array Implementation... 1.53 2.3.3 Linked Implementation... 1.57 2.4 ARRAY Y REPRESENTATION TION... 1.58 2.4.1 Representation... 1.58 2.4.2 Changing the Length of a One-Dimensional Array... 1.60 2.4.3 The Class ArrayLinearList... 1.62 2.4.3.1 Constructors for ArrayLinearList... 1.62 2.4.3.2 Elementary Methods of ArrayLinearList... 1.63 2.4.4 An Interator for ArrayLinear List... 1.67 2.4.4.1 Using a Top op-l -Level Class... 1.68 2.4.4.2 Using a Member Class... 1.69 2.5 VECTOR OR REPRESENTATION TION... 1.70 2.6 LINKED REPRESENTATION TION... 1.76 2.6.1 Singly Linked List... 1.77 2.6.1.1 The Representation... 1.77 2.6.1.2 Operations... 1.78 2.6.1.3 Insertion... 1.78 2.6.1.4 Deletion... 1.81 2.6.1.5 Search... 1.84
iv Contents 2.6.2 Doubly Linked List... 1.86 2.6.2.1 Operations... 1.87 2.6.2.2 Inserting a Node in Doubly Linked List at the Beginning... 1.87 2.6.2.3 Doubly Linked List Deletion... 1.90 2.7 CIRCULAR LIST... 1.93 2.7.1 Counting Nodes in a Circular List... 1.94 2.7.2 Printing the Contents of a Circular List... 1.94 2.7.3 Operations... 1.95 2.7.4 Insertion... 1.95 2.7.5 Deletion... 1.98 2.8 REPRESENTATION TION OF SINGLE AND TWO DIMENSIONAL ARRAYS YS... 1.101 2.8.1 One-Dimensional Arrays... 1.101 2.8.2 Two wo Dimensional Arrays... 1.104 2.9 SPARSE MATRICES TRICES... 1.107 2.9.1 Array Representation... 1.107 2.9.2 Representation using a Single Linear List... 1.108 UNIT - II [CH. - 3] ] [STACKS AND QUEUES]... 2.1-2.82 3.1 INTRODUCTION TO O STACKS... 2.2 3.1.1 Stack ADT... 2.6 3.1.2 Array Representation... 2.7 3.1.2.1 Implementation as a Subclass... 2.9 3.1.2.2 Performance Measurement... 2.10 3.1.3 Linked Representation... 2.11 3.1.3.1 Performance Measurement... 2.12
Contents v 3.2 QUEUES... 2.13 3.2.1 Queue ADT... 2.17 3.2.2 Array Representation... 2.19 3.2.2.1 The Class ArrayQueue... 2.20 3.2.3 Linked Representation... 2.21 3.3 INFIX TO POSTFIX CONVERSION USING STACK... 2.25 3.4 IMPLEMENTATION TION OF RECURSION... 2.29 3.5 CIRCULAR QUEUE... 2.32 3.5.1 Operations... 2.34 3.5.1.1 Insert... 2.34 3.5.1.2 Delete... 2.35 3.6 DEQUE ADT... 2.36 3.6.1 Array Implementation of Deque... 2.37 3.6.2 Linked Implementation of Deque... 2.41 3.7 PRIORITY QUEUE ADT... 2.45 3.7.1 Main Priority Queues Operations... 2.46 3.7.2 Auxiliary Priority Queues Operations... 2.46 3.8 IMPLEMENTATION TION USING HEAPS... 2.46 3.8.1 Insertion into Max Heap... 2.47 3.8.2 Deletion from Max Heap... 2.49 3.8.3 Example Problem... 2.52 3.9 JAVA.UTIL A.UTIL PACKA CKAGES... 2.59 3.9.1 Array List... 2.59 3.9.1.1 Obtaining an Array from a ArrayList... 2.64 3.9.2 Linked List... 2.65 3.9.3 Vector Classes... 2.70 3.9.4 Stacks in Java.util... 2.75 3.9.5 Queues Interface... 2.76 3.9.6 Interators in Java.util... 2.79
vi Contents UNIT - III [CH. H. - 4] ] [SEARCHING SEARCHING]... 3.1-3.46 4.1 SEARCHING... 3.2 4.2 LINEAR AND BINARY SEARCH METHODS... 3.2 4.2.1 Linear Search... 3.2 4.2.2 Binary Search... 3.3 4.3 HASHING... 3.4 4.3.1 Hash Functions... 3.5 4.3.1.1 Division Method... 3.6 4.3.1.2 Mid-Square Method... 3.7 4.3.1.3 Folding Method... 3.7 4.3.1.4 Multiplicative Method... 3.8 4.4 COLLISION RESOLUTION METHODS... 3.9 4.4.1 Open Addressing... 3.9 4.4.1.1 Linear Probing... 3.10 4.4.1.2 Quadratic Probing... 3.22 4.4.1.3 Double Hashing... 3.25 4.4.2 Chaining... 3.30 4.4.3 Rehashing... 3.32 4.4.4 Extendible Hashing... 3.34 4.5 HASHING IN JAVA.UTIL... 3.37 4.5.1 Hash Map... 3.37 4.5.2 Hash Set... 3.40 4.5.3 Hash Table able... 3.43 UNIT - III [CH. - 5] ] [SORTING SORTING]... 3.47-3.60 5.1 SORTING... 3.48 5.2 BUBBLE SORT... 3.48 5.3 INSERTION SORT... 3.50
Contents vii 5.4 QUICK SORT... 3.52 5.5 MERGE SORT... 3.55 5.6 HEAP SORT... 3.58 5.7 RADIX SORT... 3.59 5.8 COMPARISON OF SORTING METHODS... 3.60 UNIT - IV [CH. H. - 6] ] [TREES TREES]... 4.1-4.22 6.1 INTRODUCTION TO TREES... 4.2 6.2 ORDINARY (OR) BASIC TREE TERMINOLOGY... 4.2 6.3 BINARY TREE TERMINOLOGY... 4.4 6.4 PROPERTIES OF BINARY TREES... 4.5 6.5 BINARY TREE ADT... 4.7 6.6 REPRESENTATION TION OF BINARY Y TREE ADT... 4.9 6.6.1 Array Representation... 4.9 6.6.2 Linked Representation... 4.10 6.7 RECURSIVE AND NON-RECURSIVE TRAVERSALS... 4.11 6.7.1 Pre re-oder Traversal raversal... 4.12 6.7.2 In-Order Traversal raversal... 4.13 6.7.3 Post ost-order Traversal... 4.15 6.7.4 Levelevel-Order Traversal raversal... 4.17 6.8 JAVA CODE FOR TRAVERSALS... 4.18 6.9 THREADED BINARY TREE... 4.21 UNIT - IV [CH. - 7] ] [GRAPHS GRAPHS]... 4.23-4.66 7.1 INTRODUCTION TO GRAPHS... 4.24 7.2 GRAPH TERMINOLOGY... 4.25 7.3 PROPERTIES OF GRAPHS... 4.30 7.4 GRAPH ADT... 4.32
viii Contents 7.5 REPRESENTATION TION OF GRAPH ADT... 4.33 7.5.1 Representation of Unweighted Graphs... 4.33 7.5.2 Representation of Weighted Graphs... 4.36 7.6 GRAPH TRAVERSALS/SEARCH METHODS... 4.37 7.6.1 Depth First Search (DFS)... 4.37 7.6.2 Breadth First Search (BFS)... 4.42 7.7 JAVA CODE FOR GRAPH TRAVERSALS... 4.50 7.7.1 Java Program to Traverse a Graph using DFS... 4.50 7.7.2 Java Program to Traverse a Graph using BFS... 4.52 7.8 APPLICATIONS OF GRAPHS... 4.54 7.8.1 Finding a Path ath... 4.54 7.8.2 Connected Graphs and Components... 4.56 7.8.3 Component Labelling... 4.58 7.8.4 Spanning Trees... 4.59 7.9 MINIMUM COST SPANNING TREE USING KRUSKAL S S ALGORITHM... 4.60 7.10 DIJKSTAR AR S S ALGORITHM FOR SINGLE SOURCE SHORTEST PATH TH PROBLEM... 4.64 UNIT - V [CH. - 8] ] [BINARY SEARCH TREES AND BALANCED SEARCH TREES]... 5.1-5.78 8.1 BINARY SEARCH TREE... 5.2 8.1.1 Indexed Binary Search Trees... 5.3 8.2 BINARY SEARCH TREE ADT... 5.3 8.2.1 Insertion... 5.4 8.2.2 DELETION... 5.6 8.2.3 Searching... 5.10 8.2.4 Ascend( )... 5.11 8.2.5 Height of Binary Search Tree... 5.11 8.2.6 Implementation of Binary Search Tree ree... 5.11
Contents ix 8.3 BALANCED SEARCH TREES... 5.15 8.3.1 AVL Trees... 5.15 8.3.1.1 Height of an AVL Tree ree... 5.16 8.3.1.2 Representation of an AVL Tree... 5.16 8.3.1.3 Insertion into an AVL Search Tree... 5.17 8.3.1.4 Deletion from an AVL Search Tree ree... 5.21 8.3.2 Red Black Trees rees... 5.27 8.3.2.1 Representation of a Red-Black Tree... 5.28 8.3.2.2 Searching a Red-Black Tree... 5.28 8.3.2.3 Insertion into a Red-Black Tree ree... 5.28 8.3.2.4 Deletion from Red-Black Tree ree... 5.33 8.3.3 B-Trees... 5.36 8.3.3.1 Indexed Sequential Access Method (ISAM)... 5.36 8.3.3.2 Representation of B-Trees rees... 5.37 8.3.3.3 B-Tree ree of Order m... 5.37 8.3.3.4 Height of B-tree... 5.38 8.3.3.5 Searching a B-tree... 5.39 8.3.3.6 Insertion into a B-Tree ree... 5.39 8.3.3.7 Deletion from B-tree... 5.42 8.3.4 Trees in Java.util... 5.45 8.3.4.1 Treeset... 5.45 8.3.4.2 Tree Map Classes... 5.54 8.3.5 Tries... 5.63 8.3.5.1 Standard Tries... 5.64 8.3.5.2 Compressed Tries... 5.65 8.3.5.3 Suffix Tries... 5.66 8.3.6 Comparisons of Search Trees... 5.69
x Contents 8.4 TEXT COMPRESSION... 5.70 8.4.1 Huffman Coding and Decoding... 5.70 8.5 PATTERN MATCHING... 5.73 8.5.1 KMP Algorithm... 5.74