Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures."— Presentation transcript:

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

2 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

3 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)

4 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

5 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.

6 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

7 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

8 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=7+2+1 5=1+3+1 3=1+1+1 1= size[NIL] + size[NIL] + 1 = 0+0+1 Linear Order A C D F G H K M P Q

9 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)

10 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

11 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

12 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

13 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 !!!

14 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)

15 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

16 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 = 2 + 1 + 1 = 4

17 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 = 4 + 1 + 1 = 6

18 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

19 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

20 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)

21 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.

22 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

23 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

24 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)

25 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.

26 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

27 CS 473Lecture X27 Maintaining Subtree Sizes in an Insert Operation 19 11 6 4 7 LEFT-ROTATE(T,x) 19 6 7 4 12 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

28 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

29 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.

30 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 =      1 2 3 4 5 I(A) = {(1,5), (2,5), (3,4), (3,5), (4,5)}

31 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;

32 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;

33 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


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

Similar presentations


Ads by Google