



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 5633 Analysis of Algorithms
1
Carola Wenk
Slides courtesy of Charles Leiserson with small
changes by Carola Wenk
CS 5633 Analysis of Algorithms
4/26/
Abstract Data Type (ADT) Dictionary :
Insert (
x
inserts
x
into
Delete (
x
deletes
x
from
Find (
x
finds
x
in
Popular implementation uses any balanced searchtree (not necessarily binary). Like that eachoperation takes
O(log n)
time.
is a
dynamic set
ELECT
i
returns the
i
th smallest element
in the dynamic set
ANK
x
returns the rank of
x
in the
sorted order of
’s elements.
DEA
Use a red-black tree for the set
, but keep
subtree sizes in the nodes.
key size key
size
Notation for nodes:
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
CS 5633 Analysis of Algorithms
5
ELECT
x
i
⊳
i
th smallest element in the
subtree rooted at
x
k
size
left
x
⊳
k
= rank(
x
if
i
k
then return
x
if
i
k
then return
ELECT
left
x
, i
else return
ELECT
right
x
, i – k
Implementation trick:
Use a
sentinel
(dummy record) for
NIL
such that
size
NIL
ANK
is in the textbook.)
CS 5633 Analysis of Algorithms
4/26/
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
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 =
h
(log
n
) for red-black trees.
Why not keep the ranks themselvesin the nodes instead of subtree sizes?
They are hard to maintain when thered-black tree is modified.
Modifying operations:
NSERT
and D
ELETE
Strategy:
Update subtree sizes when
inserting or deleting.
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
NSERT
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
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/
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
(1) time per rotation:
Total I
NSERT
time =
(log
n
ELETE
similar.
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
NTERVAL
EARCH
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
NTERVAL
EARCH
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/
NTERVAL
EARCH
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]
NTERVAL
EARCH
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
]
NTERVAL
EARCH
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
Proof.
Suppose first that the search goes right.
left
x
NIL
, then we’re done, since
low
i
m
left
x
]]. The value
m
left
x
corresponds to the right endpoint of someinterval
j
, and no other interval in
can
have a larger right endpoint than
high
j
high
(
j
)
= m
[
left
[
x
]]
i
low
(
i
)
i
i
overlaps
i
CS 5633 Analysis of Algorithms
4/26/
Suppose that the search goes left, and assume that
i
i
overlaps
i
low
i
m
left
x
high
j
] for some
j
j
, it does not overlap
i
, and hence
high
i
low
j
for all
i
, we have
low
j
low
i
i
i
overlaps
i
i
j
i
′