Introduction to Algorithms - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Introduction to Algorithms

Description:

We shall also see the rank of an element?its position in the linear order of the ... i and i' satisfy the interval trichotomy; that exactly one of the following ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 25
Provided by: webNc9
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Algorithms


1
Introduction to Algorithms
  • Unit 14. Augmenting Data Structures
  • Chih-Hung Wang
  • Dynamic Order Statistics
  • How to Augment a Data Structure
  • Interval Trees

2
Dynamic Order Statistics
  • We shall also see the rank of an element?its
    position in the linear order of the set?can
    likewise be determined in O(lg n) time.

3
Dynamic Order Statistics
  • Beside the usual red-black tree fields keyx,
    colorx, px, leftx, and rightx in a node
    x, we have another field sizex. This field
    contains the number of (internal) nodes in the
    subtree rooted at x (including x itself), that
    is, the size of the subtree.
  • If we define the sentinels size to be 0, that
    is, we set sizenilT to be 0, then we have the
    identity
  • sizex sizeleftx sizerightx 1

4
An order-statistic Tree
5
Retrieving an element with a given rank
6
Analysis
  • Analysis Each recursive call goes down one
    level. Since R-B tree has O(lg n) levels, have
    O(lg n) calls? O(lg n) time.

7
Determining the rank of an element
8
Example
  • Find the rank of the node with key 38
  • Iteration keyy r
  • 38 2
  • 30 4
  • 41 4
  • 26 17
  • Return 17

9
Analysis
  • Analysis y goes up one level in each iteration ?
    O(lg n) time.

10
Maintaining subtree size
  • Referring to the code for LEFT-ROTATE(T, x) in
    section 13.2 we add the following lines
  • 12 sizey ? sizex
  • 13 sizex ? sizeleftx sizerightx 1

11
Updating subtree sizes during rotations
12
How to augment a data structure
  • 1.Choosing an underlying data structure,
  • 2.Determining additional information to be
    maintained in the underlying data structure,
  • 3.Verifying that the additional information can
    be maintained for the basic modifying operations
    on the underlying data structure, and
  • 4.Developing new operations.

13
Augmenting red-black trees
  • Theorem 14.1
  • Augment a R-B tree with field f , where f x
    depends only on information in x, leftx, and
    rightx (including f leftx and f
    rightx). Then can maintain values of f in all
    nodes during insert and delete without affecting
    O(lg n) performance.

14
Augmenting red-black trees
  • Proof
  • Since f x depends only on x and its children,
    when we alter information in x, changes propagate
    only upward (to px, ppx, . . . , root).
  • Height O(lg n)? O(lg n) updates, at O(1) each.

15
Interval Trees
  • We can represent an intervalt1,t2 as an object
    i, with fields lowi t1 (the low endpoint) and
    highi t2 (the high endpoint). We say that
    intervals i and i overlap if i ? i ? Ø, that
    is, if lowi highi and lowi highi.
    Any two intervals i and i satisfy the interval
    trichotomy that exactly one of the following
    three properties holds
  • a. i and i overlap,
  • b. i is to the left of i (i.e., highi lt
    lowi),
  • c. i is to the right of i (i.e., highi
    lt lowi)

16
Interval Trees
17
Data Structure of Interval Trees
  • An interval tree is a red-black tree that
    maintains a dynamic set of elements, with each
    element x containing an interval intx.

18
Operations
  • INTERVAL-INSERT(T,x)
  • Add the element x in T
  • INTERVAL-DELETE(T,x)
  • Remove the element x in T
  • INTERVAL-SEARCH(T,i)
  • Returns a pointer to an element x in T such that
    intx overlaps interval i, or the sentinel
    nilT if no such element is in the set.

19
Data Structure(1)
20
Data Structure (2)
21
Maintaining the Information
  • Maintaining the information
  • We determine maxx given interval intx and the
    max values of node xs children
  • maxx max(highintx, maxleftx,
    maxrightx)
  • Thus, by Theorem 14.1, insertion and deletion run
    in O(lg n) time.

22
New OperationInterval Search
23
Example
  • Successful Search
  • i22, 25
  • 8, 9 -gt 15, 23 (answer)
  • Unsuccessful Search
  • i 11, 14
  • 8, 9 -gt 15, 23 -gt nilT

24
Theorem
  • Theorem 14.2
  • Any execution of INTERVAL-SEARCH(T,i) either
    returns a node whose interval overlaps i, or it
    returns nilT and the tree T contain no node
    whose interval overlaps i.
Write a Comment
User Comments (0)
About PowerShow.com