Data Structures & Problem Solving

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Greedy Algorithms Greed is good. (Some of the time)
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Trees Chapter 8.
Fall 2007CS 2251 Trees Chapter 8. Fall 2007CS 2252 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information.
Trees Chapter 8. Chapter 8: Trees2 Chapter Objectives To learn how to use a tree to represent a hierarchical organization of information To learn how.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Priority queues CS310 – Data Structures Professor Roch Weiss, Chapter 6.9, 21 All figures marked with a chapter and section number are copyrighted © 2006.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
©Brooks/Cole, 2003 Chapter 12 Abstract Data Type.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Important Problem Types and Fundamental Data Structures
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Lecture Objectives  To learn how to use a tree to represent a hierarchical organization of information  To learn how to use recursion to process trees.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
Spring 2010CS 2251 Trees Chapter 6. Spring 2010CS 2252 Chapter Objectives Learn to use a tree to represent a hierarchical organization of information.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Recursion Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 7 © 2002 Addison Wesley.
TREES. What is a tree ? An Abstract Data Type which emulates a tree structure with a set of linked nodes The nodes within a tree are organized in a hierarchical.
The Disjoint Set Class Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 24 © 2002 Addison Wesley.
Tree Traversals, TreeSort 20 February Expression Tree Leaves are operands Interior nodes are operators A binary tree to represent (A - B) + C.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Rooted Tree a b d ef i j g h c k root parent node (self) child descendent leaf (no children) e, i, k, g, h are leaves internal node (not a leaf) sibling.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 © 2002 Addison Wesley.
Chapter 10: Trees A tree is a connected simple undirected graph with no simple circuits. Properties: There is a unique simple path between any 2 of its.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Splay Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 22 © 2002 Addison Wesley.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
Chapter 11. Chapter Summary  Introduction to trees (11.1)  Application of trees (11.2)  Tree traversal (11.3)  Spanning trees (11.4)
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
Final Exam Review COP4530.
Trees Saurav Karmakar
Trees Chapter 15.
Data Structure and Algorithms
Top 50 Data Structures Interview Questions
Data Structure Interview Question and Answers
Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Lecture Trees Chapter 9 of textbook 1. Concepts of trees
B+ Tree.
Week 11 - Friday CS221.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
Priority Queues (Heaps)
i206: Lecture 13: Recursion, continued Trees
Binary Trees, Binary Search Trees
Sorting.
Review for Midterm Neil Tang 03/04/2010
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Trees 1: Theory, Models, Generic Heap Algorithms, Priority Queues
i206: Lecture 14: Heaps, Graphs intro.
Final Exam Review COP4530.
Ch. 8 Priority Queues And Heaps
Week nine-ten: Trees Trees.
CE 221 Data Structures and Algorithms
Data Structures and Algorithm Analysis Trees
Binary Trees, Binary Search Trees
CE 221 Data Structures and Algorithms
Trees.
Priority Queues (Heaps)
Important Problem Types and Fundamental Data Structures
Data Structures for Shaping and Scheduling
Heaps & Multi-way Search Trees
Priority Queues Binary Heaps
Binary Trees, Binary Search Trees
Data Structures Using C++ 2E
Presentation transcript:

Data Structures & Problem Solving Appendix A Operators Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure A.1 Java operators listed from highest to lowest precedence Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Appendix B Graphical User Interfaces Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure B.1 A GUI that illustrates some of the basic Swing components Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure B.2 Compressed hierarchy of Swing Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure B.5 Five buttons arranged using BorderLayout Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 1 Primitive Java Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 1.2 The eight primitive types in Java Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 1.4 Result of logical operators Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 2 Reference Types Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 2.1 An illustration of a reference: The Point object stored at memory location 1000 is referenced by both point1 and point3. The point object stored at memory location 1024 is referenced by point2. The memory locations where the variables are stored are arbitrary. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.2 The result of point3=point2: point3 now references the same object as point2. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.3 (A) The result of call-by-value. (a) b is a copy of yesButton; (b) after b.setLabel("No"): changes to the state of the object referenced by b are reflected in the object referenced by yesButton because these are the same object. (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.3 (B) The result of call-by-value. (c) after b=null: change to the value of b does not affect the value of yesButton; (d) after the method returns, b is out of scope. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.5 (A) Array expansion, internally: (a) At the starting point, arr represents 10 integers; (b) after step 1, original represents the same 10 integers. (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.5 (B) Array expansion, internally: (c) after steps 2 and 3, arr represents 12 integers, the first 10 of which are copied from original ; and (d) after step 4, the 10 integers are available for reclamation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.12 Common standard run-time exceptions Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 2.13 Common standard checked exceptions Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 3 Objects and Classes Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 3.2 IntCell members: read and write are accessible, but storedValue is hidden. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 3.5 (A) javadoc output for Figure 3.4 (partial output) (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 3.5 (B) javadoc output for Figure 3.4 (partial output) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 3.9 Packages defined in this text Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 4 Inheritance Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 4.4 Memory layout with inheritance. Light shading indicates fields that are private, and accessible only by methods of the class. Dark shading in the Student class indicates fields that are not accessible in the Student class, but are nonetheless present. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 4.6 The Person hierarchy Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 4.10 The hierarchy of shapes used in an inheritance example Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 4.17 The hierarchy of exceptions (partial list) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 4.37 Four types of class methods Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 5 Algorithm Analysis Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 5.1 Running times for small inputs Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.2 Running times for moderate inputs Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.3 Functions in order of increasing growth rate Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.6 The subsequences used in Theorem 5.2 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.7 The subsequences used in Theorem 5.3. The sequence from p to q has a sum that is, at most, that of the subsequence from i to q. On the left-hand side, the sequence from i to q is itself not the maximum (by Theorem 5.2). On the right-hand side, the sequence from i to q has already been seen. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.9 Meanings of the various growth functions Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.10 Observed running times (in seconds) for various maximum contiguous subsequence sum algorithms Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 5.13 Empirical running time for N binary searches in an N-item array Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 6 The Collections API Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 6.19 A simple linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 6.20 The stack model: Input to a stack is by push, output is by top, and deletion is by pop. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 6.22 The queue model: Input is by enqueue, output is by getFront, and deletion is by dequeue. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 6.34 The priority queue model: Only the minimum element is accessible. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 6.38 A summary of some data structures Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 7 Recursion Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 7.5 A stack of activation records Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.7 A trace of the recursive calculation of the Fibonacci numbers Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.8 A tree viewed recursively Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.9 A tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.12 A recursively drawn ruler Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.14 (a) A fractal star outline drawn by the code shown in Figure 7.15 (b) The same star immediately before the last square is added Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.19 Dividing the maximum contiguous subsequence problem into halves Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.21 Trace of recursive calls for recursive maximum contiguous subsequence sum algorithm for N = 8 elements Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.22 Some of the subproblems solved recursively in Figure 7.23 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.24 An alternative recursive algorithm for the coin-changing problem Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 7.30 Grid for Exercise 7.31 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 8 Sorting Algorithms Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 8.3 Basic action of insertion sort (the shaded part is sorted) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.4 A closer look at the action of insertion sort (the dark shading indicates the sorted area; the light shading is where the new element was placed). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.5 Shellsort after each pass if the increment sequence is {1, 3, 5} Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.6 Running time (milliseconds) of the insertion sort and Shellsort for various increment sequences Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.10 (A) The steps of quicksort (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.10 (B) The steps of quicksort Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.11 Partitioning algorithm: Pivot element 6 is placed at the end. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.12 Partitioning algorithm: i stops at large element 8; j stops at small element 2. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.13 Partitioning algorithm: The out-of-order elements 8 and 2 are swapped. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.14 Partitioning algorithm: i stops at large element 9; j stops at small element 5. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.15 Partitioning algorithm: The out-of-order elements 9 and 5 are swapped. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.16 Partitioning algorithm: i stops at large element 9; j stops at small element 3. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.17 Partitioning algorithm: Swap pivot and element in position i. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.18 Original array Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.19 Result of sorting three elements (first, middle, and last) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 8.20 Result of swapping the pivot with the next-to-last element Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 9 Randomization Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 9.3 Distribution of lottery winners if the expected number of winners is 2 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 10 Fun and Games Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 10.1 A sample word search grid Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 10.10 Alpha–beta pruning: After H2a is evaluated, C2, which is the minimum of the H2’s, is at best a draw. Consequently, it cannot be an improvement over C1. We therefore do not need to evaluate H2b, H2c, and H2d and can proceed directly to C3. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 10.12 Two searches that arrive at identical positions Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 10.17 Alpha–beta pruning example for Exercise 10.2. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 11 Stacks and Compilers Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 11.1 Stack operations in a balanced-symbol algorithm Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 11.11 Steps in the evaluation of a postfix expression Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 11.12 Examples of using associativity to break ties in precedence Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 11.13 Infix to postfix conversion Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 11.23 Expression tree for (a+b)*(a-b) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 12 Utilities Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 12.1 A standard coding scheme Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.2 Representation of the original code by a tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.3 A slightly better tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.4 An optimal prefix code tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.5 Optimal prefix code Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.6 Initial stage of Huffman’s algorithm Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.7 Huffman’s algorithm after the first merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.8 Huffman’s algorithm after the second merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.9 Huffman’s algorithm after the third merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.10 Huffman’s algorithm after the fourth merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.11 Huffman’s algorithm after the fifth merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 12.12 Huffman’s algorithm after the final merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 13 Simulation Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 13.1 The Josephus problem: At each step, the darkest circle represents the initial holder and the lightly shaded circle represents the player who receives the hot potato (and is eliminated). Passes are made clockwise. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 13.10 (A) The priority queue for modem bank simulation after each step (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 13.10 (B) The priority queue for modem bank simulation after each step Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 14 Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 14.1 A directed graph. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.2 Adjacency list representation of the graph shown in Figure 14.1; the nodes in list i represent vertices adjacent to i and the cost of the connecting edge. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.4 An abstract scenario of the data structures used in a shortest-path calculation, with an input graph taken from a file. The shortest weighted path from A to C is A to B to E to D to C (cost is 76). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.5 Data structures used in a shortest-path calculation, with an input graph taken from a file; the shortest weighted path from A to C is A to B to E to D to C (cost is 76). Legend: Dark-bordered boxes are Vertex objects. The unshaded portion in each box contains the name and adjacency list and does not change when shortest-path computation is performed. Each adjacency list entry contains an Edge that stores a reference to another Vertex object and the edge cost. Shaded portion is dist and prev, filled in after shortest path computation runs. Dark arrows emanate from vertexMap. Light arrows are adjacency list entries. Dashed arrows are the prev data member that results from a shortest-path computation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.16 The graph, after the starting vertex has been marked as reachable in zero edges Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.17 The graph, after all the vertices whose path length from the starting vertex is 1 have been found Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.18 The graph, after all the vertices whose shortest path from the starting vertex is 2 have been found Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.19 The final shortest paths Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.20 If w is adjacent to v and there is a path to v, there also is a path to w Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.21A Searching the graph in the unweighted shortest-path computation. The darkest-shaded vertices have already been completely processed, the lightest-shaded vertices have not yet been used as v, and the medium-shaded vertex is the current vertex, v. The stages proceed left to right, top to bottom, as numbered (continued). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.21B Searching the graph in the unweighted shortest-path computation. The darkest-shaded vertices have already been completely processed, the lightest-shaded vertices have not yet been used as v, and the medium-shaded vertex is the current vertex, v. The stages proceed left to right, top to bottom, as numbered. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.23 The eyeball is at v and w is adjacent, so Dw should be lowered to 6. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.24 If Dv is minimal among all unseen vertices and if all edge costs are nonnegative, Dv represents the shortest path. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.25A Stages of Dijkstra’s algorithm. The conventions are the same as those in Figure 14.21 (continued). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.25B Stages of Dijkstra’s algorithm. The conventions are the same as those in Figure 14.21. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.28 A graph with a negative-cost cycle Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.30A A topological sort. The conventions are the same as those in Figure 14.21 (continued). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.30B A topological sort. The conventions are the same as those in Figure 14.21. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.31A The stages of acyclic graph algorithm. The conventions are the same as those in Figure 14.21 (continued). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.31B The stages of acyclic graph algorithm. The conventions are the same as those in Figure 14.21. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.33 An activity-node graph Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.34 An event-node graph Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.35 Earliest completion times Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.36 Latest completion times Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.37 Earliest completion time, latest completion time, and slack (additional edge item) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 14.38 Worst-case running times of various graph algorithms Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 15 Inner Classes and Implementation of ArrayList Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 15.6 Iterator/container relationship Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 15.7 Iterator/container with inner classes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 16 Stacks and Queues Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 16.1 How the stack routines work: (a) empty stack: (b) push(a); (c) push(b); and (d) pop() Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 16.8 Basic array implementation of the queue Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 16.9 Array implementation of the queue with wraparound Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 16.18 Linked list implementation of the Stack class Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 16.22 Skeleton for the linked list–based queue class Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 16.26 The enqueue operation for the linked list–based implementation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 17 Linked Lists Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 17.1 Basic linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.2 Insertion in a linked list: Create new node (tmp), copy in x , set tmp’s next link, and set current’s next link Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.3 Deletion from a linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.4 Using a header node for the linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.5 Empty list when a header node is used Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.15 A doubly linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.16 An empty doubly linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.17 Insertion in a doubly linked list by getting new node and then changing pointers in the order indicated Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.18 A circularly and doubly linked list Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.31 (A) Commands for editor in Exercise 17.19 (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 17.31 (B) Commands for editor in Exercise 17.19 (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 18 Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 18.1 A tree, with height and depth information Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.2 A tree viewed recursively Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.3 First child/next sibling representation of the tree in Figure 18.1 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.4 A Unix directory Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.6 The directory listing for the tree shown in Figure 18.4 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.7 The Unix directory with file sizes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.9 A trace of the size method Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.11 Uses of binary trees: (a) an expression tree and (b) a Huffman coding tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.14 Result of a naive merge operation: Subtrees are shared. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.15 Aliasing problems in the merge operation; t1 is also the current object. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.18 Recursive view used to calculate the size of a tree: ST = SL + SR + 1. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.20 Recursive view of the node height calculation: HT = Max (HL + 1, HR + 1) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.23 (a) Preorder, (b) postorder, and (c) inorder visitation routes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.25 Stack states during postorder traversal Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 18.33 Tree for Exercises 18.1 and 18.2. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 19 Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 19.39 A color flip at 50 induces a violation; because the violation is outside, a single rotation fixes it. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.40 Result of single rotation that fixes the violation at node 50 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.41 Insertion of 45 as a red node Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.50 X has two black children, and both of its sibling’s children are black; do a color flip. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.51 X has two black children, and the outer child of its sibling is red; do a single rotation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.52 X has two black children, and the inner child of its sibling is red; do a double rotation. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.53 X is black, and at least one child is red; if we fall through to the next level and land on a red child, fine; if not, we rotate a sibling and parent. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.54 AA-tree resulting from the insertion of 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55, and 35 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.55 The skew procedure is a simple rotation between X and P. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.56 The split procedure is a simple rotation between X and R; note that R’s level increases. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.57 After insertion of 45 in the sample tree; consecutive hori-zontal links are introduced, starting at 35. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.58 After split at 35; a left horizontal link at 50 is introduced. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.59 After skew at 50; consecutive horizontal nodes are introduced starting at 40. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.60 After split at 40; 50 is now on the same level as 70, inducing an illegal left horizontal link. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.61 After skew at 70; consecutive horizontal links are introduced, starting at 30. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.62 After split at 30; the insertion is complete. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.63 When 1 is deleted, all nodes become level 1, thereby introducing horizontal left links. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.82 A 5-ary tree of 31 nodes has only three levels Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.83 A B-tree of order 5 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.84 The B-tree after insertion of 57 in the tree shown in Figure 19.83. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.85 Insertion of 55 in the B-tree shown in Figure 19.84 causes a split into two leaves. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 19.86 Insertion of 40 in the B-tree shown in Figure 19.85 causes a split into two leaves and then a split of the parent node. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 20 Hash Tables Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 20.4 Linear probing hash table after each insertion Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 20.5 Illustration of primary clustering in linear probing (b) versus no clustering (a) and the less significant secondary clustering in quadratic probing (c). Long lines represent occupied cells, and the load factor is 0.7. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 20.6 A quadratic probing hash table after each insertion (note that the table size was poorly chosen because it is not a prime number). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 21 A Priority Queue: The Binary Heap Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 21.1 A complete binary tree and its array representation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.2 Heap-order property Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.3 Two complete trees: (a) a heap; (b) not a heap Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.7 Attempt to insert 14, creating the hole and bubbling the hole up Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.8 The remaining two steps required to insert 14 in the original heap shown in Figure 21.7 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.10 Creation of the hole at the root Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.11 The next two steps in the deleteMin operation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.12 The last two steps in the deleteMin operation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.15 Recursive view of the heap Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.17 Implementation of the linear-time buildHeap method Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.18 (a) After percolateDown(6); (b) after percolateDown(5) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.19 (a) After percolateDown(4); (b) after percolateDown(3) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.20 (a) After percolateDown(2); (b) after percolateDown(1) and buildHeap terminates Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.21 Marking the left edges for height 1 nodes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.22 Marking the first left edge and the subsequent right edge for height 2 nodes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.23 Marking the first left edge and the subsequent two right edges for height 3 nodes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.24 Marking the first left edge and the subsequent two right edges for the height 4 node Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.25 Max heap after the buildHeap phase Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.26 Heap after the first deleteMax operation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.27 Heap after the second deleteMax operation Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.29 Initial tape configuration Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.30 Distribution of length 3 runs to two tapes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.31 Tapes after the first round of merging (run length = 6) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.32 Tapes after the second round of merging (run length = 12) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.33 Tapes after the third round of merging Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.34 Initial distribution of length 3 runs to three tapes Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.35 After one round of three-way merging (run length = 9) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.36 After two rounds of three-way merging Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.37 The number of runs for a polyphase merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 21.38 Example of run construction Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 22 Splay Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 22.1 Rotate-to-root strategy applied when node 3 is accessed Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.2 Insertion of 4 using the rotate-to-root strategy Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.3 Sequential access of items takes quadratic time Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.4 The zig case (normal single rotation) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.5 The zig-zag case (same as a double rotation); the symmetric case has been omitted Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.6 Zig-zig case (unique to the splay tree); the symmetric case has been omitted Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.7 Result of splaying at node 1 (three zig-zigs) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.8 The remove operation applied to node 6: First, 6 is splayed to the root, leaving two subtrees; a findMax is performed on the left subtree, raising 5 to the root of the left subtree; then the right subtree can be attached (not shown). Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.9A Top-down splay rotations: (a) zig (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.9B Top-down splay rotations: (b) zig-zig (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.9C Top-down splay rotations: (c) zig-zag Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.10 Simplified top–down zig-zag Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.11 Final arrangement for top–down splaying Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.12 (A) Steps in a top–down splay (accessing 19 in the top tree) (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 22.12 (B) Steps in a top–down splay (accessing 19 in the top tree) (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 23 Merging Priority Queues Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 23.1 Simplistic merging of heap-ordered trees: Right paths are merged. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.2 Merging of skew heap; right paths are merged, and the result is made a left path. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.3 Change in the heavy or light status of nodes after a merge Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.4 Abstract representation of a sample pairing heap Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.5 Actual representation of the pairing heap shown in Figure 23.4; the dark line represents a pair of links that connect nodes in both directions Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.6 (A) Recombination of siblings after a deleteMin. In each merge, the larger root tree is made the left child of the smaller root tree: (a) the resulting trees (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.6 (B) Recombination of siblings after a deleteMin. In each merge, the larger root tree is made the left child of the smaller root tree: (b) after the first pass (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.6 (C) Recombination of siblings after a deleteMin. In each merge, the larger root tree is made the left child of the smaller root tree: (c) after the first merge of the second pass (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.6 (D) Recombination of siblings after a deleteMin. In each merge, the larger root tree is made the left child of the smaller root tree: (d) after the second merge of the second pass Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 23.13 The compareAndLink method merges two trees Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Data Structures & Problem Solving Chapter 24 The Disjoint Set Class Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss © 2002 Addison Wesley

Figure 24.1 A 50 x 88 maze Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.2 Initial state: All walls are up, and all cells are in their own sets. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.3 At some point in the algorithm, several walls have been knocked down and sets have been merged. At this point, if we randomly select the wall between 8 and 13, this wall is not knocked down because 8 and 13 are already connected. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.4 We randomly select the wall between squares 18 and 13 in Figure 24.3; this wall has been knocked down because 18 and 13 were not already connected, and their sets have been merged. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.5 Eventually, 24 walls have been knocked down, and all the elements are in the same set. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.6 (a) A graph G and (b) its minimum spanning tree Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.7 (A) Kruskal’s algorithm after each edge has been considered. The stages proceed left-to-right, top-to-bottom, as numbered. (continued) Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.7 (B) Kruskal’s algorithm after each edge has been considered. The stages proceed left-to-right, top-to-bottom, as numbered. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.8 The nearest common ancestor for each request in the pair sequence (x, y), (u, z), (w, x), (z, w), and (w, y) is A, C, A, B, and y, respectively. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.9 The sets immediately prior to the return from the recursive call to D; D is marked as visited and NCA(D, v) is v’s anchor to the current path. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.10 After the recursive call from D returns, we merge the set anchored by D into the set anchored by C and then compute all NCA(C, v) for nodes v marked prior to completing C’s recursive call. Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.12 A forest and its eight elements, initially in different sets Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.13 The forest after the union of trees with roots 4 and 5 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.14 The forest after the union of trees with roots 6 and 7 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.15 The forest after the union of trees with roots 4 and 6 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.16 The forest formed by union-by-size, with the sizes encoded as negative numbers Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.17 Worst-case tree for N = 16 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.18 A forest formed by union-by-height, with the height encoded as a negative number Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.19 Path compression resulting from a find (14) on the tree shown in Figure 24.17 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.22 Possible partitioning of ranks into groups Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.23 Actual partitioning of ranks into groups used in the proof Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Figure 24.24 A graph G for Exercise 24.3 Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley

Complexity and Big-O Notation Example N! = # ways to put N objects in linear order. Generating all such orderings (permutations) on N objects takes at least N! time. Example Adding two n-bit integers at most 2n bit operations.

Time estimate for two methods

Time complexity

Order of a function

Definitions

Example

Example

Theorem

Proof

Example

Big-O is transitive

Corollary

Some common functions Decreasing order

Example

Example

Example

Example

Select a theta notation

Fibonacci sequence as the worst case for gcd

Comparing a gcd computation with Fibonacci seq. 62=2.23+16 23=16+7 16=2.7+2 7=3.2+1 2=2.1+0 13=8+5 8=5+3 5=3+2 3=2+1 2=2.1+0

Theorem

Corollary