26
Augmenting Data Structures Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006

Augmenting Data Structures

Embed Size (px)

DESCRIPTION

Augmenting Data Structures. Advanced Algorithms & Data Structures Lecture Theme 07 – Part II Prof. Dr. Th. Ottmann Summer Semester 2006. Examples for Augmenting DS. Dynamic order statistics: Augmenting binary search trees by size information - PowerPoint PPT Presentation

Citation preview

Augmenting Data Structures

Advanced Algorithms & Data Structures

Lecture Theme 07 – Part II

Prof. Dr. Th. Ottmann

Summer Semester 2006

2

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

3

Interval Trees (CLR-Version)

Problem: Given a set R of intervals that changes under insertions and deletions,

construct a data structure to store R that can be updated in O(log n) time and that can

find for any given query interval i an interval in R that overlaps i, and returns nil if there

is no such interval, in O(log n) time.

Idea: Store the set of intervals in an appropriately augmented balanced binary search

tree and design an algorithm for the new operation.

Interval-search(T, i): Report an interval stored in T that overlaps the query interval i, if

such an interval exists, and Nil otherwise.

4

Observation

Let i = [low(i), high(i)] and i‘ = [low(i‘), high(i‘)] be two intervals.

Then i and i‘ overlap if and only if

low(i) ≤ high(i‘) and low(i‘) ≤ high(i)

Any two intervals satisfy the interval trichotomy:

a) i and i‘ overlap

b) high(i) ≤ low(i‘)

c) high(i‘) ≤ low(i)

5

Interval

Trichotomy

The cases when intervals I1 and I

2 overlap:

The cases when intervals I1 and I

2 do not overlap:

x1

y1

x2

y2

x1

y1

x2

y2

x1

y1

x2

y2

x1

y1

x2

y2

x2

y2

x1

y1 x

1y

1

x2

y2

6

Interval tree (CLR Version)

[0,3]

[6,10]

[8,9]

[15,23]

[16,21]

[17,19]

[25,30]

[27,33]

[26,26]

[5,8]

[29,30]

3

9 21

30

26

30

8 33

10 33

33

[19,20]

20

Each node v stores an interval int(v) and the maximumupper endponit of all intervals storedin the subtree rooted at v;The interval tree is a search treeon the lower endpoints of intervals.

Max(v) is the maximum valueof all right endpoints in the subtreerooted at v.

7

Maintaining max-information

Max (x) = max(high(int(x), max (left(x)), max (right(x))

Max-information can be maintainedduring updates and rebalancingoperations (rotations).

8

Finding an interval in T that overlaps interval i

[0,3]

[6,10]

[8,9]

[15,23]

[16,21]

[17,19]

[25,30]

[27,33]

[26,26]

[5,8]

[29,30]

3

9 21

30

26

30

8 33

10 33

33

[19,20]

20

Interval-search(T, i)

x root(T)while x ≠ Nil and i does not overlap int(x) do if left(x) ≠ Nil and max(left(x))≥ low(i) then x left(x) else x right(x)return x

Interval-search can be carried out in time O(height T).

Observation: if int(x) does notoverlap i, the search alwaysproceeds in a safe direction!

9

Interval Trees: Point-set Variant

Problem: Given a set R of intervals that changes under insertions and deletions,

construct a data structure to store R that can be updated in O(log n) time and that can

find for any given query interval i an interval in R that overlaps i, and returns nil if there

is no such interval, in O(log n) time.

Solution: Map intervals to points and store points in appropriately augmented tree.

i

l = low[i] r = high[i]

(l, r)

10

Max-augmented

Range

Tree

(2, 5) (3, 4) (4, 5)

(8, 13) (10, 15)

(11, 12)

(14, 17)

(21, 28)(15, 22) (17, 18)

2 4

3

11

10

8

14

17

15 21

28

28

28

15

5 15

15

15

Store intervals as points in sortedx-order in a leaf search tree.Store max y-coordinates at internalnodes.

Leaf-search tree on x-coordinates of points

Max-tournament tree on y-coordinates of points

2822

11

Interval Search

Interval-Search(T, i)

/* Find an interval in tree T that overlaps i */

1 P = root[T]

2 while p is not a leaf do

3 if max-y[left[p]] low[i]

4 then p = left[p]

5 else p = right[p]

/* Now p is a leaf storing interval i’ */

6 If (i and i’ overlap) then return “found” else return “not found”

12

Correctness Proof

Case 1: We go right, low[i] > max-y[left[p]]

i

Intervals in left subtree of p

None of them can overlap with i.

13

Correctness Proof

Case 2: We go left, low[i] ≤ max-y[left[p]]

If T[left[p]] does not contain an interval i‘ that overlaps i, then T[right[p]] cannot contain

such an interval as well!

ii‘

max-y[left[p]]

(Here we utilize the fact that the intervals are sorted according to their x-coordinates!)

14

Interval Tree-Summary

A interval tree for a set of n intervals [l1, r1], …, [ln, rn] on the line is a daynamic max-

augmented range tree for the set of points P = {(l1, r1), …, (ln, rn)}.

Interval trees can be used to carry out the following operations:

Interval-Insert(T, i) inserts the interval i into the tree T

Interval-Delete(T, i) removes the interval i from the tree T

Interval-Search(T, i) returns a pointer to a node storing an interval i‘ that overlaps i, or

NIL if no such interval is stored in T.

15

Examples for Augmenting DS

• Dynamic order statistics: Augmenting binary search trees by size information

• D-dimensional range trees: Recursive construction of (static) d-dim range trees

• Min-augmented dynamic range trees: Augmenting 1-dim range trees by min-

information

• Interval trees

• Priority search trees

16

3-Sided

Range

Queries

Goal: Report all k points in the query range in O(log n + k) time.

17

3-Sided

Range

Queries

Goal: Report all k points in the query range in O(log n + k) time.

Age

Salary

18

Priority

Search

Trees

2 3 4

5 8

11

14

2115 17 33

2 4

3

11

8

5

14

17

15 21

Two data structures in one:

● Search tree on points’x-coordinates

● Heap on points’y-coordinates

{ (2, 12), (3, 4) (4, 11), (5, 3), (8, 5), (11, 21), (14, 7), (15, 2), (17, 30), (21, 8), (33, 33) }

19

Priority

Search

Trees

2 4

5 8

11 2115 17

2 4

3

11

8

5

14

17

15 21

(33, 33)

(17, 30)

(21, 8)

(15, 2)

(11, 21)

(2, 12) (4, 11)

(5, 3)

Two data structures in one:● Search tree on points’

x-coordinates● Heap on points’

y-coordinates

33

3

14

(8, 5)

(14, 7)

(3, 4)

20

• Query procedure:

Inspect all nodes on the two bounding paths and report the points that match the query.

For every tree between the two bounding paths, apply the following strategy:

• Inspect the root.

• If this reports a point, recursively visit the children of the root.

3-Sided

Range

Queries

on

a

Priority

Search

Tree

O(log n) time to query red pathsO(log n + k) time to query blue subtrees

21

• O

b

s

e

r

v

a

t

i

o

n

s

:

• W

e

n

e

v

e

r

r

e

p

o

r

t

a

p

o

i

n

t

t

h

a

t

i

s

n

o

t

i

n

t

h

e

q

u

e

r

y

r

a

n

g

e

.

• P

o

i

n

t

s

i

n

t

h

e

y

e

l

l

o

w

s

u

b

t

r

e

e

s

c

a

n

n

o

t

m

a

t

c

h

t

h

e

q

u

e

r

y

.

• P

o

i

n

t

s

i

n

t

h

e

b

l

u

e

s

u

b

t

r

e

e

s

t

h

a

t

a

r

e

n

o

t

r

e

p

o

r

t

e

d

c

a

n

n

o

t

m

a

t

c

h

t

h

e

q

u

e

r

y

.

Correctness of the

Query

Procedure

22

Insertion into a

Priority

Search

Tree

Insertion procedure:

1. Insert new leaf based onpoint’s x-coordinate.

2. Insert point downthe tree, based on

its y-coordinate.

2 4

5 8

11 2115 17

2 4

3

11

8

5

14

17

15 21

(33, 33)

(17, 30)

(21, 8)

(15, 2)

(11, 21)

(2, 12) (4, 11)

(5, 3)

33

3

14

(8, 5)

(14, 7)

(3, 4)

23

Deletion from a

Priority

Search

Tree

Deletion procedure:

1. Search for the point anddelete it.

2. Fill the gap by pulling-uppoints according to

their y-values

2 4

5 8

11 2115 17

2 4

3

11

8

5

14

17

15 21

(33, 33)

(17, 30)

(21, 8)

(15, 2)

(11, 21)

(2, 12) (4, 11)

(5, 3)

33

3

14

(8, 5)

(14, 7)

(3, 4)

24

Priority Search Tress: Observations

Insertion and deletion of point in a priority search tree T of n nodes can be carried out

in time O(height(T)).

Priority search trees support north-grounded range reporting, if the heap-structure is a

max-heap, and they support south-grounded range reporting, if the heap-structure

is a min-heap.

Maintaining the height of the leaf search tree underlying a priority search tree such

that the height is always of order O(log n) for a priority search tree storing n nodes

requires rebalancing!

In order to obtain O(log n) algorithms for insertion and deletion of points one must use

a rebalancing scheme with constant restructuring cost per update!

A PST storing n points requires space O(N).

25

Rotations in a

Priority

Search

Tree

p1

p2

p1

?

• Push p2 to the appropriate child of y.

• Store p1 at y.

• Propagate the point with maximal y-coordinate from the appropriate child of x.

x

y

y

x

26

Priority

Search

Trees

Summary

Theorem: There exists a data structure to represent a dynamically changing set S of points in two dimensions with the following properties:

• The data structure can be updated in O (log n) time after every insertion or deletion into or from S.

• The data structure allows us to answer 3-sided range queries in O (log n + k) time.

• The data structure occupies O (n) space.