



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!




3/2/06 CS 5633 Analysis of Algorithms 1
Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk 3/2/06 CS 5633 Analysis of Algorithms 2
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.
D is a dynamic set
OS-SELECT ( i , S ): returns the i th smallest element in the dynamic set S. OS-RANK( x , S ): returns the rank of x ∈ S in 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
Notation for nodes:
M 9 C 5 A 1
F 3
N 1
Q 1
P 3
H 1
D 1
size [ x ] = size [ left [ x ]] + size [ right [ x ]] + 1
3/2/06 CS 5633 Analysis of Algorithms 5
OS-SELECT ( 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-SELECT ( left [ x ] , i ) else return OS-SELECT ( right [ x ] , i – k )
Implementation trick: Use a sentinel (dummy record) for NIL such that size [NIL] = 0.
(OS-RANK is in the textbook.) 3/2/06 CS 5633 Analysis of Algorithms 6
M 9 C 5 A 1
F 3
N 1
Q 1
P 3
H 1
D 1
OS-SELECT ( root , 5)
i = 5 k = 6
M 9 C 5
i = 5 k = 2 i = 3 k = 2
F 3 i = 1 k = 1
H 1
H 1
Running time = O ( h ) = O (log n ) for red-black trees.
Q. Why not keep the ranks themselves in the nodes instead of subtree sizes?
A. They are hard to maintain when the red-black tree is modified.
Modifying operations: INSERT and DELETE. Strategy: Update subtree sizes when inserting or deleting.
k ← size [ left [ x ]] + 1? k = rank( x )
M 9 C 5 A 1
F 3
N 1
Q 1
P 3
H 1
D 1
M 10 C 6 F 4 H 2 K 1
3/2/06 CS 5633 Analysis of Algorithms 13
17, 23
5, 18 4, 8
15, 18 7, 10
22, 23
m [ x ] = max
high [ int [ x ]] m [ left [ x ]] m [ right [ x ]]
red
3/2/06 CS 5633 Analysis of Algorithms 14
3. Verify that this information can be maintained for modifying operations. - INSERT: Fix m ’s on the way down.
6, 30 11, 19
14 19
30
11, 30 6, 30
30 14
19
Total INSERT time = O (log n ); DELETE similar.
17, 23 5, 18 4, 8
15, 18 7, 10
22, 23
x
x ← root [14,16] and [17,19] don’t overlap 14 ≤ 18 ⇒ x ← left [ x ]
14 16
3/2/06 CS 5633 Analysis of Algorithms 17
17, 23
5, 18
4, 8
15, 18
7, 10
22, x 23
[14,16] and [5,11] don’t overlap 14 > 8 ⇒ x ← right [ x ]
14 16
3/2/06 CS 5633 Analysis of Algorithms 18
17, 23
5, 18
4, 8
15, 18
7, 10
22, 23
x
[14,16] and [15,18] overlap return [15,18]
14 16
17, 23 5, 18 4, 8
15, 18 7, 10
22, 23
x
x ← root [12,14] and [17,19] don’t overlap 12 ≤ 18 ⇒ x ← left [ x ]
12 14
17, 23 5, 18 4, 8
15, 18 7, 10
22, x 23
[12,14] and [5,11] don’t overlap 12 > 8 ⇒ x ← right [ x ]
12 14
Proof. Suppose first that the search goes right.
L high ( j ) = m [ left [ x ]]
i low ( i )
Suppose that the search goes left, and assume that { i ′ ∈ L : i ′ overlaps i } = ∅.
i j i ′