Docsity

Augmenting Data Structures - Analysis of Algorithms - Slides | CS 5633, Study notes of Algorithms and Programming

Toggle sidebar
bg1
CS 5633 Analysis of Algorithms 14/26/05
CS 5633 -- Spring 2005
Augmenting Data Structures
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 5633 Analysis of Algorithms 24/26/05
Dictionaries and Dynamic Sets
Abstract Data Type (ADT) Dictionary :
Insert (x,D): inserts x into D
Delete (x,D): deletes x from D
Find (x,D):finds x in D
Popular implementation uses any balanced search
tree (not necessarily binary). Like that each
operation takes O(log n) time.
Dis a
dynamic set
CS 5633 Analysis of Algorithms 34/26/05
Dynamic order statistics
OS-SELECT(i,S): returns the ith smallest element
in the dynamic set S.
OS-RANK(x,S): returns the rank of x ∈Sin the
sorted order of S’s elements.
IDEA:Use a red-black tree for the set S, but keep
subtree sizes in the nodes.
key
size
key
size
Notation for nodes:
CS 5633 Analysis of Algorithms 44/26/05
Example of an OS-tree
M
9
M
9
C
5
C
5
A
1
A
1
F
3
F
3
N
1
N
1
Q
1
Q
1
P
3
P
3
H
1
H
1
D
1
D
1
size[x] = size[left[x]] + size[right[x]] + 1
pf3
pf4
pf5

Partial preview of the text

Download Augmenting Data Structures - Analysis of Algorithms - Slides | CS 5633 and more Study notes Algorithms and Programming in PDF only on Docsity!

CS 5633 Analysis of Algorithms

1

CS 5633 -- Spring 2005

Augmenting Data Structures

Carola Wenk

Slides courtesy of Charles Leiserson with small

changes by Carola Wenk

CS 5633 Analysis of Algorithms

4/26/

Dictionaries and Dynamic Sets

Abstract Data Type (ADT) Dictionary :

Insert (

x

D

inserts

x

into

D

Delete (

x

D

deletes

x

from

D

Find (

x

D

finds

x

in

D

Popular implementation uses any balanced searchtree (not necessarily binary). Like that eachoperation takes

O(log n)

time.

D

is a

dynamic set

Dynamic order statistics

OS-S

ELECT

i

S

returns the

i

th smallest element

in the dynamic set

S.

OS-R

ANK

x

S

returns the rank of

x

S

in the

sorted order of

S

’s elements.

I

DEA

Use a red-black tree for the set

S

, but keep

subtree sizes in the nodes.

key size key

size

Notation for nodes:

Example of an OS-tree

M

9

M

9

C

5

C

5

A

1

A

1

F 3

F

3

N

1

N

1

Q

1

Q

1

P 3

P 3

H

1

H

1

D

1

D

1

size

[

x

] =

size

[

left

[

x

]] +

size

[

right

[

x

]] + 1

CS 5633 Analysis of Algorithms

5

Selection

OS-S

ELECT

x

i

⊳

i

th smallest element in the

subtree rooted at

x

k

size

[

left

[

x

]] + 1

⊳

k

= rank(

x

if

i

k

then return

x

if

i

k

then return

OS-S

ELECT

left

[

x

]

, i

else return

OS-S

ELECT

right

[

x

]

, i – k

Implementation trick:

Use a

sentinel

(dummy record) for

NIL

such that

size

[

NIL

] = 0.

(OS-R

ANK

is in the textbook.)

CS 5633 Analysis of Algorithms

4/26/

Example

M

9

M

9

C 5

C

5

A

1

A

1

F 3

F 3

N

1

N

1

Q

1

Q

1

P 3

P 3

H

1

H

1

D

1

D

1

OS-S

ELECT

root

i

= 5

k

= 6

M

9

M

9

C 5

C

5

i

= 5

k

= 2

i

= 3

k

= 2

F 3

F 3

i

= 1

k

= 1

H

1

H

1

H

1

H

1

Running time =

O

h

O

(log

n

) for red-black trees.

Data structure maintenance

Q.

Why not keep the ranks themselvesin the nodes instead of subtree sizes?

A.

They are hard to maintain when thered-black tree is modified.

Modifying operations:

I

NSERT

and D

ELETE

Strategy:

Update subtree sizes when

inserting or deleting.

Example of insertion

M

9

M

9

C

5

C

5

A

1

A

1

F 3

F

3

N

1

N

1

Q

1

Q

1

P 3

P 3

H

1

H

1

D

1

D

1

I

NSERT

(“K”)

M 10

M 10

C

6

C

6

F 4

F

4

H

2

H

2

K

1

K

1

CS 5633 Analysis of Algorithms

13

17,

23

17,

23

Example interval tree

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

m

[

x

] = max

high

[

int

[

x

]]

m

[

left

[

x

]]

m

[

right

[

x

]]

CS 5633 Analysis of Algorithms

4/26/

Modifying operations

3. Verify that this information can be maintained

for modifying operations. • I

NSERT

: Fix

m

’s on the way down.

6,

30

6,

30

11,

19

11,

19

19

19

14

14

30

30

11,

30

11,

30

6,

30

6,

30

30

30

14

14

19

19

  • Rotations — Fixup =

O

(1) time per rotation:

Total I

NSERT

time =

O

(log

n

); D

ELETE

similar.

New operations

  1. Develop new dynamic-set operations that use

the information.

I

NTERVAL

-S

EARCH

(

i

)

x

←

root

while

x

≠

NIL

and (

low

[

i

] >

high

[

int

[

x

]]

or

low

[

int

[

x

]] >

high

[

i

])

do

⊳

i

and

int

[

x

] don’t overlap

if

left

[

x

]

≠

NIL

and

low

[

i

]

≤

m

[

left

[

x

]]

then

x

←

left

[

x

]

else

x

←

right

[

x

]

return

x

Example 1:

I

NTERVAL

-S

EARCH

([14,16])

17,

23

17,

23

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

x

x

←

root

[14,16] and [17,19] don’t overlap 14

≤

18

⇒

x

←

left

[

x

]

CS 5633 Analysis of Algorithms

17

Example 1:

I

NTERVAL

-S

EARCH

([14,16])

17,

23

17,

23

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

x

[14,16] and [5,11] don’t overlap 14

8

⇒

x

←

right

[

x

]

CS 5633 Analysis of Algorithms

4/26/

Example 1:

I

NTERVAL

-S

EARCH

([14,16])

17,

23

17,

23

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

x

[14,16] and [15,18] overlap return

[15,18]

Example 2:

I

NTERVAL

-S

EARCH

([12,14])

17,

23

17,

23

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

x

x

←

root

[12,14] and [17,19] don’t overlap 12

≤

18

⇒

x

←

left

[

x

]

Example 2:

I

NTERVAL

-S

EARCH

([12,14])

17,

23

17,

23

5,

18

5,

18

4,

8

4,

8

15,

18

15,

18

7,

10

7,

10

22,

23

22,

23

x

[12,14] and [5,11] don’t overlap 12

8

⇒

x

←

right

[

x

]

CS 5633 Analysis of Algorithms

25

Correctness proof

Proof.

Suppose first that the search goes right.

  • If

left

[

x

] =

NIL

, then we’re done, since

L

  • Otherwise, the code dictates that we must have

low

[

i

] >

m

[

left

[

x

]]. The value

m

[

left

[

x

]]

corresponds to the right endpoint of someinterval

j

L

, and no other interval in

L

can

have a larger right endpoint than

high

j

L

high

(

j

)

= m

[

left

[

x

]]

i

low

(

i

)

  • Therefore, {

i

L

i

overlaps

i

CS 5633 Analysis of Algorithms

4/26/

Proof (continued)

Suppose that the search goes left, and assume that

i

L

i

overlaps

i

  • Then, the code dictates that

low

[

i

]

m

[

left

[

x

]] =

high

[

j

] for some

j

L.

  • Since

j

L

, it does not overlap

i

, and hence

high

[

i

] <

low

[

j

].

  • But, the binary-search-tree property implies that

for all

i

R

, we have

low

[

j

]

low

[

i

].

  • But then {

i

R

i

overlaps

i

L

i

j

i

′