CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
Binary Search Trees Comp 550.
David Luebke 1 5/22/2015 CS 332: Algorithms Augmenting Data Structures: Interval Trees.
Augnenting data structures Augment an existing data structure to apply to a new problem. Red-black tree as an example.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
Interval Trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Lecture X Augmenting Data Structures
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary Search Tree Qamar Abbas.
October 3, Algorithms and Data Structures Lecture VII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 11 Prof. Erik Demaine.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
2IL05 Data Structures Spring 2010 Lecture 9: Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
Analysis of Algorithms CS 477/677
Introduction to Algorithms
Lecture 7 Algorithm Analysis
CS200: Algorithms Analysis
Augmenting Data Structures
Red-Black Trees.
Lecture 9 Algorithm Analysis
Chapter 14: Augmenting Data Structures
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VII
Chapter 12: Binary Search Trees
CS 583 Analysis of Algorithms
Introduction to Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Design and Analysis of Algorithms
Augmenting Data Structures: Interval Trees
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Analysis of Algorithms CS 477/677
Red-Black Trees CS302 Data Structures
Presentation transcript:

CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures

CS 473Lecture X2 Augmenting Data Structures  When dealing with a new problem Data structures play important role Must design or addopt a data structure.  Only in rare situtations We need to create an entirely new type of data structure.  More Often It suffices to augment a known data structure by storing additional information. Then we can program new operations for the data structure to support the desired application

CS 473Lecture X3 Augmenting Data Structures (2)  Not Always Easy Because, added info must be updated and maintained by the ordinary operations on the data structure.  Operations Augmented data structure (ADS) has operations inherited from underlying data structure (UDS). UDS Read/Query operations are not a problem. (ie. Min-Heap Minimum Query) UDS Modify operations should update additional information without adding too much cost. (ie. Min- Heap Extract Min, Decrease Key)

CS 473Lecture X4 Dynamic Order Statistics  Example problem; Dynamic Order Statistics, where we need two operations; OS-SELECT(x,i): returns i th smallest key in subtree rooted at x OS-RANK(T,x):returns rank (position) of x in sorted (linear) order of tree T.  Other operations Query: Search, Min, Max, Successor, Predecessor Modify: Insert, Delete

CS 473Lecture X5 Dynamic Order Statistics (2)  Sorted or linear order of a binary search tree T is determined by inorder tree walk of T. IDEA: Use Red-Black (R-B) tree as the underlying data structure. Keep subtree size in nodes as additional information.

CS 473Lecture X6 Dynamic Order Statistics (3)  Relation Between Subtree Sizes; size[x] = size[left[x]] + size[right[x]] + 1  Note on implementation; For convenience use sentinel NIL[T] such that; size[NIL[T]] = 0 Since high level languages do not have operations on NIL values. (ie. Java has NullPointerException) The node itself

CS 473Lecture X7 Dynamic Order Statistics Notation  Node Structure: Key as with any Binary Search Tree (Tree is indexed according to key) Subtree Size as additional Data on Node. KEY SUBTREE SIZE

CS 473Lecture X8 Dynamic Order Statistics Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 10= = = = size[NIL] + size[NIL] + 1 = Linear Order A C D F G H K M P Q

CS 473Lecture X9 Retrieving an Element With a Given Rank OS-SELECT(x, i) r  size[left[x]] + 1 if i = r then return x elseif i < r then return OS-SELECT( left[x], i) else return OS-SELECT(right[x], i)

CS 473Lecture X10 OS-SELECT Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 i=6 r=8 r > i Go Left

CS 473Lecture X11 OS-SELECT Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 i=6 r=8 i=6 r=2 r < i Go Right

CS 473Lecture X12 OS-SELECT Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 i=6 r=8 i=6 r=2 i=4 r=2 r < i Go Right

CS 473Lecture X13 OS-SELECT Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 i=6 r=8 i=6 r=2 i=4 r=2 i=2 r=2 r = i Found !!!

CS 473Lecture X14 OS-SELECT  IDEA: Knowing size of left subtree (number of nodes smaller than current) tells you which subtree the answer is in  Running Time: O(lgn) Each recursive call goes down one level in the OS- TREE Running time = O(d) where d = depth of the i th element Worst case running time is proportional to the height of the tree Since tree is a R-B tree, its height is balanced and O(lgn)

CS 473Lecture X15 Determining the Rank of an Element OS-RANK(T, x) r  size[left[x]] + 1 y  x while y  root[T] do if y = right[p[y]] then r  r + size[left[p[y]]] + 1 y  p[y] return r

CS 473Lecture X16 Dynamic Order Statistics Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 init r=2 right child r = = 4

CS 473Lecture X17 Dynamic Order Statistics Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 right child r = = 6

CS 473Lecture X18 Dynamic Order Statistics Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 left child r = 6

CS 473Lecture X19 Dynamic Order Statistics Example M 10 P 2 C 7 A 1 F 5 D 1 H 3 K 1 G 1 Q 1 Root and Answer!!! r = 6

CS 473Lecture X20 OS-RANK  IDEA: rank[x] = # of nodes preceeding x in an inorder tree walk + 1 (itself) Follow the simple upward path from node x to the root. All left subtrees in this path contribute to rank[x]  Running Time: O(lgn) Each iteration of the while-loop takes O(1) time y goes up one level in the tree with each iteration. Running time = O(d x ), where d x is the depth of node x Running time, at worst proportional to the height of the tree. (if x is a leaf node)

CS 473Lecture X21 Determining the Rank of an Element  Follow the nodes y (y=x initially) on the path from x to root.  Consider Subtree T p[y] rooted at p[y], where z is y’s sibling. x zy TyTy TzTz r Important: r contains the number of nodes in T y that preceedes x.

CS 473Lecture X22 Determining the Rank of an Element x zy TyTy TzTz r=r+size[z]+1 Case 1: If y is a right child  z is a left child then All nodes in T z and p[y] precedes x Thus, must add size[z] + 1 to r

CS 473Lecture X23 Determining the Rank of an Element x z y TyTy TzTz r=r Case 2: If y is a left child  z is a right child then Neither p[y] nor any node in T z precedes x. Don’t update r

CS 473Lecture X24 Maintaining Subtree Sizes  OS-SELECT and OS-RANK works if we are able to update the subtree sizes with modifications.  Two operations INSERT and DELETE modifies the contents of the Tree. We should try to update subtree size without extra traversals.  If not, would have to make a pass over the tree to set up the sizes whenever the tree is modified, at cost  (n)

CS 473Lecture X25 Red Black Tree Insertion  Insertion is a two phase process; Phase 1: Insert node. Go down the tree from the root. Inserting the new node as a child of an existing node. (Search in O(lgn) time) Phase 2: Balance tree and correct colors. Go up the tree changing colors and ultimately performing rotations to maintain the R-B properties.

CS 473Lecture X26 Maintaining Subtree Sizes in an Insert Operation  Phase 1: Increment size[x] for each node x on the downward path from root to leaves The new added node gets the size of 1 O(lgn) operation  Phase 2: Only rotations cause structural changes At most two rotations Good News!!! Rotation is a local operation. Invalidates only two size fields of the two nodes, around which the rotation is performed O(1) time operation

CS 473Lecture X27 Maintaining Subtree Sizes in an Insert Operation LEFT-ROTATE(T,x) x y     x y After the rotation: size[y]  size[x] size[x]  size[left[x]] + size[right[x]] + 1 Note: only size fields of x and y are modified

CS 473Lecture X28 Red Black Tree Deletion Deletion is a two phase process;  Phase 1: Splice out one node y  Phase 2: Performs at most 3 rotations Otherwise performs no structural changes

CS 473Lecture X29 Maintaining Subtree Sizes in an Delete Operation  Phase 1: Traverse a path from node y up to the root. Decrementing the size field of each node on the path. Length of this path = d y = O(lgn) O(lgn) Time Operation  Phase 2: The O(1) Time Rotations can be handled in the same manner as for insertion.

CS 473Lecture X30 Application: Counting the Number of Inversions  Definition: Let A[1..n] be an array of n distinct numbers. (i,j) is an inversion of A if i A[j] Inversions of A =      I(A) = {(1,5), (2,5), (3,4), (3,5), (4,5)}

CS 473Lecture X31 Application: Counting the Number of Inversions  Question : What array with elements from the set {1...n} has the most inversions? A = number of inversions;

CS 473Lecture X32 Application: Counting the Number of Inversions  Question : What is the relationship between the running time of insertion sort and the number of inversions |I(A)| = # of inversions = # of element moves (shifts) where; i.e; Let r(j) be the rank of A[j] in A[i...j], then;

CS 473Lecture X33 Number of inversions INVERSION(A) sum  0 T   for j  1 to n do x  MAKE-NEW-NODE() left[x]  right[x]  p[x]  NIL key[x]  A[j] OS-INSERT(T,x) r  OS-RANK(T,x) I j  j-r sum  sum+I j return sum