CS计算机代考程序代写 algorithm data structure PowerPoint Presentation

PowerPoint Presentation

EECS 4101/5101
Augmenting
Data Structures
Prof. Andy Mirzaian

TOPICS
Augmentation
Order Statistics Dictionary
Interval Tree
Overlapping Windows
2

References:

[CLRS] chapter 14
3

Augmenting a Data Structure
Suppose we have a base data structure D that efficiently handles a standard set of operations. For instance, D is a Red-Black Tree that supports operations SEARCH, INSERT, and DELETE.

In some applications, besides the existing operations, we wish our data structure to support an additional set of operations. For instance, the order-statistics operations SELECT and RANK (see next slides).

How do we efficiently implement the new operations without degrading the efficiency of the existing ones?

This can be done by augmenting the data structure, i.e., maintaining added pieces of information in it to assist fast implementation of the new operations.

However, this forces revision of the existing operations to consistently maintain the augmented info while they modify D.

Given a new application, we need to figure out the following:
What is the base data structure D we wish to use?
What is the augmented information?
How do we efficiently implement the new ops on the augmented D?
How do we revise the existing operations on the augmented D,
(ideally) without performance degradation?
4

ORDER STATISTICS
Dictionary
5

ORDER STATISTICS DICTIONARY
Two new (inverse) operations:

RANK(K,D): return the number of items in data set D that are  key K.
SELECT(r,D): return the item in D with rank r (return nil if none exists).
Solution 1: D as an un-ordered set of n items.

RANK and SELECT can be done in O(n) time in the worst-case.
RANK(K,D): Sequentially scan through D and count # items  K.
SELECT(r,D): See [CLRS chapter 9] or my EECS3101 LS5 or LN4.
Solution 2: D as a sorted array of n items.

RANK takes O(log n) time by binary-search.
SELECT takes O(1) time by probing rank index position.
What about the dictionary operations?
Solution 3: Augment a search tree. See next slides.
6

Augmenting a BST
Let T be any BST.
What is rank of the root?
1 + # items in the left subtree.

L
R
root
T:

1
2
1
1
1
3
5
3
9
Rank of root = 1 + size[left[root]].
Augmented info in each node x:

size[x] = # items in the subtree rooted at x.
(size[nil] = 0.)
7

RANK & SELECT on BST
Rank(K,x) (* return rank of key K in BST rooted at x *)
if x = nil then return 0
R  1+ size[left[x]] (* root rank *)
if K = key[x] then return R
if K < key[x] then return Rank(K,left[x]) if K > key[x] then return R + Rank(K, right[x])
end
Select(r,x) (* return item of rank r in BST rooted at x *)
if x = nil then return nil
R  1+ size[left[x]] (* root rank *)
if r = R then return x
if r < R then return Select(r,left[x]) if r > R then return Select(r-R, right[x])
end
Running time = O(# nodes on the search path).

x
8

Maintain Augmented Info
“size[.]” field can be evaluated by a local recurrence in O(1) time.

size[x] = 1 + size[left[x]] + size[right[x]], if x nil
size[nil] = 0.
With each dictionary operation (Search, Insert, Delete), update “size” field
of affected nodes. What local changes affect the “size” field?
Insert: attach a new leaf (increment size of all ancestors)
Delete: splice-out a node (decrement size of all ancestors)
Rotation:

C
y

A

B
x

A
x

B

C
y

Rotate(y,x);
size[x] size[y];
size[y] 1+size[left[y]]+size[right[y]]

(Left Rotation is analogous)
Search, Insert, Delete: asymptotic running time unaffected!

T
x

9

Order Statistics Complexity
THEOREM 1:
Augmented Red-Black trees, with the added field size[x] at each node x, support the Order Statistics operations RANK & SELECT, as well as the dictionary operations SEARCH, INSERT, DELETE, in O(log n) worst-case time per operation.

If we use the same augmentation on Splay trees, each of these five operations takes O(log n) amortized time.
[Note: we should “splay the deepest accessed node” after each operation, even after operations RANK & SELECT.]
DEFINITION: Suppose we augment each node x of a BST (or any variant) with a new field f[x]. We say “f” is “O(1) locally composable” if for every node x in the tree, f[x] can be determined in O(1) time from the contents of nodes x, left[x], and right[x] (including their “f” fields).

[For instance “size”, as defined above, is O(1) locally composable.]
10

AUGMENTATION THEOREM
THEOREM 2:
Suppose we augment Red-Black trees with a new O(1) locally composable field f[x] at each node x. Then field “f” in every node of the tree can be consistently maintained by dictionary operations SEARCH, INSERT, DELETE, without affecting their O(log n) worst-case running time per operation.

If we use the same augmentation on Splay trees, each dictionary operation still takes O(log n) amortized time.
Proof: Generalize the “size” field augmentation idea.
If x is the deepest affected accessed node,
then bottom-up update f[y], for every ancestor y of x, inclusive.
Also revise each rotation in O(1) time to update the field f at its affected local nodes.
NOTE: In general, “f” may be not just one scalar field, but a record of a constant number of scalar fields.
11

INTERVAL TREES
12

Intervals on the real line
Interval I on the real line = [s[I], f[I]] (from start s[I] to finish f[I], inclusive).

I
s[I]
f[I]
R

Dichotomy: For intervals X and Y exactly one of the following 3 holds:

X

Y
(1) X left of Y: f[X] < s[Y]. or (3) X and Y overlap: f[X]  s[Y] and f[Y]  s[X]. partial overlap total overlap Y X (2) Y left of X: f[Y] < s[X]. 13 Interval Dictionary Problem PROBLEM: Maintain a set S of (possibly overlapping) intervals with the following operations: Insert(I, S ): Insert interval I into S. Delete(I, S): Delete interval I from S. OverlapSearch(I, S): Return an arbitrarily chosen interval of S that overlaps interval I. (Return nil if none exists.) ReportAllOverlaps(I, S): Output all intervals of S that overlap interval I. CountAllOverlaps(I, S): Output the # of intervals of S that overlap interval I. SOLUTION: Augment a Red-Black tree or a Splay tree. Each node x holds an interval Int[x]  [s[x],f[x]] of S. For each node x: key[x]  s[x]. So, intervals are inorder sorted by their starting point. What augmented fields should we maintain? 14 OverlapSearch(I,x) x L R Case 1) I and Int[x] overlap: return x (* or Int[x] *) Case 2) I to the left of Int[x]: f[I] < s[x].  yR: f[I] < s[x]  s[y] I is disjoint from x and from every interval in R. return OverlapSearch(I,left[x]) I Int[x] yL: s[y]  s[x] < s[I]. yL: Int[y] overlap I  f[y]  s[I]. (R may or may not have overlapping intervals.) Int[y] Define: LAST(L) = max { f[y] | yL }.  ( yL: Int[y] overlap I )  LAST(L)  s[I]. if LAST(L)  s[I] then OverlapSearch(I,left[x]) else OverlapSearch(I,right[x]) Case 3) I to the right of Int[x]: f[x] < s[I]. Where to search next? Int[x] I factor out 15 OverlapSearch cases x L R f[x] < s[I]  f[I] < s[x] f[x]  s[I]  f[I]  s[x] L   LAST[L]  s[I] L =  LAST[L] < s[I] yL: f[y] = LAST[L]  s[I] yL: f[y]  LAST[L] < s[I] yL: s[y]s[x]f[x]0?)
Can we do better than the obvious O(n2) time brute-force solution?
YES: Counting & Disjointness in O(n log n) time,
Reporting in O(R + n log n) time.

l(w)
r(w)

b(w)
t(w)

w
x
y
22

Plane Sweep Method

sweep line

x
y

sweep line
x
y
active rectangles

x
active x-intervals
23

Sweep Schedule & Sweep Status
Sweep Schedule is the 2n events (corresponding to the bottom and top y
coordinates of the n windows in input set S) in ascending order.
We can pre-sort these 2n events in O(n log n) time.

Sweep Status is the dictionary of the active windows maintained in an
interval tree T. For each window w, its x-interval is [l(w), r(w)], starting at
l(w) and finishing at r(w). Dictionary size: |T|  n.

Operations:
Insert (w, T): insert (x-interval of) activated window w into T.
Delete(w,T): delete (x-interval of) deactivated window w from T.
OverlapSearch(w,T): return a window in T that overlaps w.
CountAllOverlaps(w,T): return count of # windows in T that overlap w.
ReportAllOverlaps(w,T): report all windows in T that overlap w.

All but the last operation take O(log n) time each.
ReportAllOverlaps takes O(Rw + log n) time, where Rw is the output size.

SwS O(Rw + log n) = O( ( SwS Rw ) + n log n) = O( R + n log n).
24

Plane Sweep Algorithm
ALGORITHM ReportOverlappingPairs (S,n)
Step 0: Initialize sweep status interval tree T:
T  

Step 1: Initialize sweep schedule of events E:
E = {  b(w), w  | wS }  {  t(w), w  | wS }.
Sort E in ascending order of the first component.
[Tie breaking rule: if b(w)=t(w’), b has precedence over t]

Step 2: Sweep the plane according to sweep schedule E:
for each y,w  E, in ascending order of y do
if y = b(w)
then do
ReportAllOverlaps(w,T)
Insert(w,T)
end-then
else Delete(w,T) (* y = t(w) *)
end-for
end
Running time = O(R + n log n).
25

Exercises
26

Range-Search-Counting: Given a Red-Black tree T and a pair of keys a and b, a < b (not necessarily in T), we wish to output the number of items x in T such that a  key[x]  b. Augment T so that, without degrading the time complexities of the dictionary operations Search, Insert, Delete, operation RangeSearchCount can be done in O(log n) time in the worst case, where n is the number of items in T. Dictionary with Average and Median: Operations Average(K,T) and Median(K,T) return, respectively, the average and median of those items in dictionary T that are  the given key K. (a) Describe the augmented dictionary data structure to support these new operations. (b) Describe the algorithms for Average and Median, and explain their running times. (c) What are the required modifications to Search, Insert, Delete? Are their running times affected? [CLRS, Exercise 14.1-7, p. 345] We wish to output the number of inversions in a given array A[1..n] of n real numbers. Show how to use Order Statistics Dictionary (OSD) to solve this problem in O(n log n) time. Selection in a Pair of Order Statistics Dictionaries: As we learned in the course, an Order Statistics Dictionary (OSD) can be implemented efficiently by an augmented Red-Black Tree in which each node x has the extra field size[x] which stores the number of descendants of node x. We are given two OSDs S and T with n keys in total, and a positive integer k, k  n. The elements within an OSD have distinct keys, but the elements in the two OSDs may have some equal keys. The problem is to find the kth smallest key among the n keys stored in S and T (without changing S and T). Design and analyze an algorithm that solves this problem in worst-case O(log n) time. 27 [CLRS, Exercise 14.1-8, p. 345] Consider n given chords on a circle, each defined by its two endpoints. Describe an O(n log n) time algorithm to determine the number of pairs of chords that intersect inside the circle. [For instance, if the n chords are all diameters that meet at the center, the correct answer would be n(n-1)/2.] For simplicity, assume that no two chords share an end-point. [CLRS, Problem 14-1, p. 354] We wish to keep track of a Point of Maximum Overlap (PMO) in a dynamic set T of intervals, i.e., a point on the real line that is overlapped by the largest number of intervals in data set T. (a) Show that there is always a PMO that is an end-point of an interval in T (if not empty). (b) Design and analyze an augmented interval data structure T that efficiently supports the old operations Insert, Delete, and the new operation FindPMO(T). The latter returns a point of maximum overlap with the intervals in the data set T. [Hint: Keep a Red-Black tree of all endpoints. Associate a value of +1 with each left end-point, and associate a value of –1 with each right end-point. Augment each node of the tree with extra information to maintain the PMO.] [CLRS, Exercise 14.3-6, p. 354] Dynamic Closest Pair: Show how to maintain a dynamic data set Q of numbers that supports the operation ClosestPair, which returns the pair of numbers in Q with minimum absolute value difference. For example, if Q = {1, 5, 9, 15, 18, 22}, then ClosestPair(Q) returns (15,18), since |18 – 15| = 3 is the minimum difference among any distinct pair of numbers in Q. Design and analyze an augmented data structure to support such a data set with the operations Search, Insert, Delete, and ClosestPair as efficiently as possible. 28 Closest Bichromatic Pair: We want to maintain a dynamic data set Q of items. Each item has two attributes: a key, which is a real number, and a colour, which is either blue or green. In addition to the usual dictionary operations, we wish to support a new operation called Closest Bichromatic Pair (CBP). CBP(Q) returns a pair of items in Q that have different colours and minimum possible absolute value difference in their key values. (Note, we are referring to the values of their keys, not their index or position in the data set.) CBP returns nil if all items in Q have the same colour. Design and analyze an augmented data structure to support such a data set with the operations Search, Insert, Delete, and CBP as efficiently as possible. [CLRS, Exercise 14.3-3, p. 353] EarliestOverlapSearch(I,T): We are given an interval tree T and an interval I. For simplicity, assume all intervals have distinct starting points. Design and analyze an efficient implementation of EarliestOverlapSearch(I,T), which returns the interval with minimum starting time among those intervals in T that overlap interval I. It returns nil if no interval in T overlaps I. [CLRS, Exercise 14.3-4, p. 354] ReportAllOverlaps(I,T): (a) Show that ReportAllOverlaps(I,T) can be done in O(min{n, (R+1)log n}) time, where n is the number of intervals in Interval tree T, and R is the output size, i.e., the number of intervals in T that overlap interval I. You should not redefine or augment Interval trees in any way. However, the operation may temporarily modify T during its process (e.g., delete an item, then reinsert it back into T). (b) Answer part (a) but with ReportAllOverlaps not modifying T in any way, not even temporarily. (c) [Extra credit and optional:] Improve the time complexity of ReportAllOverlaps(I,T) to O(R + log n), without degrading the time complexities of the other operations. [You may redefine the structure.] 29 Nested Rectangles: We are given a set R= {R1 , ... , Rn} of n axis-parallel rectangles in the plane. Each rectangle is given by its two pairs of extreme x and y coordinates. One of these rectangles is the bounding box and encloses all the remaining n-1 rectangles in R. All rectangles in R have disjoint boundaries. So, for any pair of rectangles Ri and Rj in R, either they are completely outside each other, or one encloses the other. Therefore, we may have several levels of nesting among these rectangles. (See Fig. (a) below.) We say Ri immediately encloses Rj, if Ri encloses Rj and there is no other rectangle Rk in R such that Ri encloses Rk and Rk encloses Rj. (a) The immediate enclosure relationship on R defines a directed graph, where we have a directed edge from Ri to Rj iff Ri immediately encloses Rj. Prove this digraph is indeed a rooted tree (called the Nesting Tree of R). That is, you need to prove that the said digraph is acyclic, and every node has in- degree 1 except for one node that has in-degree 0. What rectangle in R corresponds to the root of that tree? (As an illustration, see Fig. (b) corresponding to Fig. (a).) (b) Design and analyze an efficient algorithm that given the set R, constructs its corresponding Nesting Tree by parent pointers. That is, outputs the parent array p[1..n] such that p[j] = i means Ri immediately encloses Rj. Carefully explain the correctness and running time of your algorithm. (a) A set of axis parallel rectangles with disjoint boundaries. A B D C E F G H J I F B D C G H (b) The Nesting Tree. I J A E 30 END 31 ï ï î ï ï í ì = ¥ ¹ ï þ ï ý ü ï î ï í ì = nil x if - nil x if ] ] x [ right [ LAST ], ] x [ left [ LAST ], x [ f max ] x [ LAST /docProps/thumbnail.jpeg