Advanced Data Structures and Algorithms · Advanced Data Structures and Algorithms Department of...
Transcript of Advanced Data Structures and Algorithms · Advanced Data Structures and Algorithms Department of...
Advanced Data Structures and
Algorithms
Department of Computer Science _ UHD 1
Computer Science Department
College of Science and Technology
University of Human Development,
2015 – 2016
Associate Professor Dr. Raed Ibraheem Hamed
Randomized Data Structures Department of Computer Science _ UHD 2
I flipped a coin 30 times:
HTHTHHHTTTHHHTHTTTTTTHHHHHHTTH
One may almost suggest such a sequence is not random;
what is the probability of getting 6 tails and then 6 heads?
– Unfortunately, this is exactly what we got with the first
attempt to generate such a sequence, and is as random a
sequence as we can generate.
Random Numbers
Randomized Data Structures Department of Computer Science _ UHD
Randomized Data Structures
3
One application of randomized algorithms in the area
of data structures, specifically, Treaps and lists.
Randomized Data Structures Department of Computer Science _ UHD 4
Randomized Data Structures
Skip listsTreaps
Randomized Data Structures Department of Computer Science _ UHD 5
First introduced in 1989 by Aragon and Seidel
Randomized Binary Search Tree
A combination of a binary search tree and a heap (a “tree -
heap”)
Each node contains
◦ Data / Key (comparable)
◦ Priority (random integer number)
◦ Left, right child reference
Nodes are in BST order by data/key and in heap order by priority
random integer number.
Every subtree of a treap is a treap
Treaps
Randomized Data Structures Department of Computer Science _ UHD 6
A treap is a binary search tree in which every node
has both a search key and a priority, where the
inorder sequence of search keys is sorted and each
node’s priority is smaller than the priorities of its
children.
Treaps Definition
Randomized Data Structures Department of Computer Science _ UHD 7
The search tree has the structure that would result if elements
were inserted in the order of their priorities.
Tree + Heap = Treaps
Definition: A treap is a binary tree. Each node contains one element x with
key(x) ∈ U and prio(x) ∈ R. The following properties hold. �
Search tree property For each element x:
• elements y in the left subtree of x satisfy: key(y) < key(x)
• elements y in the right subtree of x satisfy : key(y) > key(x)
Heap property For all elements x, y:
• If y is a child of x, then prio(y) > prio(x).
• All priorities are pairwise distinct.
Randomized Data Structures Department of Computer Science _ UHD
Treap : Dictionary Data Structure
Treap is a binary search tree
binary tree property
search tree property
Treap is also a heap
heap-order property
randomly assigned priorities
15
12
10
30
9
15
7
8
4
18
6
7
2
9
priority
key
8
Heap in Yellow; Search Tree in Blue
Legend
9
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L
34
E
22
D
27
A
25
P
35
O
42
R
priority
key
binary tree
heap-order
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Randomized Data Structures Department of Computer Science _ UHD 10
There is only one possible treap for a given set of Keys and
priorities Proof:
• by heap property: the key k, with the highest priority must
be the root of the treap
• by BST property: all keys < k must be in the left subtree of the
root and all keys > k must be in the right subtree
• Inductively, the subtrees of the root must be constructed in the
same manner
i.e. by heap property: the root of the LST, k1, must have the
highest priority of any key in the LS and by BST: all keys in LST
of k1 are < k1 and all keys in RST of k1 are > k1
Treaps
Randomized Data Structures Department of Computer Science _ UHD
Treap Insert
Choose a random priority
Insert as in normal BST
Rotate up until heap order is restored
6
7
insert(15)
7
8
2
9
14
12
6
7
7
8
2
9
14
12
9
15
6
7
7
8
2
9
9
15
14
12
11
priority
key
Randomized Data Structures Department of Computer Science _ UHD 12
Q
P Q
P
Let P be Q's left child. Set P to be the new root." Basically that's
the description of the rotation to the right or clockwise:
Binary Tree Rotation (Left or Right)
Randomized Data Structures Department of Computer Science _ UHD 13
Binary Tree Rotation (Left or Right)
Randomized Data Structures Department of Computer Science _ UHD 14
The right rotation operation as shown in the image to the
left is performed with Q as the root and hence is a right
rotation on, or rooted at, Q. This operation results in a
rotation of the tree in the clockwise direction. The inverse
operation is the left rotation, which results in a movement
in a counter-clockwise direction (the left rotation shown
above is rooted at P). The key to understanding how a
rotation functions is to understand its constraints.
Binary Tree Rotation (Left or Right)
Randomized Data Structures Department of Computer Science _ UHD 15
Binary Tree Rotation (Left or Right)
Randomized Data Structures Department of Computer Science _ UHD 16
T1, T2 and T3 are subtrees of the tree rooted with y (on left side)
or x (on right side)
y x
/ \ Right Rotation / \
x T3 – – – – – – – > T1 y
/ \ < - - - - - - - / \
T1 T2 Left Rotation T2 T3
Keys in both of the above trees follow the following order
keys(T1) < key(x) < keys(T2) < key(y) < keys(T3)
So BST property is not violated anywhere.
Binary Tree Rotation (Left or Right)
Randomized Data Structures Department of Computer Science _ UHD
Tree + Heap… Why Bother?
Insert data in sorted order into a treap …
What shape tree comes out?
6
7
insert(7)
6
7
insert(8)
7
8
6
7
insert(9)
7
8
2
9
6
7
insert(12)
7
8
2
9
15
12
17
The shape of the tree is
fully specified by what the
keys are and what their
random priorities are!
Randomized Data Structures Department of Computer Science _ UHD 18
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L
34
E
22
D
27
A
25
P
35
O
42
R
G / 3
D / 7 K / 6
3
G
7
D
6
K
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L
34
E
22
D
27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L
34
E
22
D
27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L
34
E
22
D
27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
Insert J with priority 2
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Randomized Data Structures Department of Computer Science _ UHD 19
Find the key
Increase its value to
When X is found, rotate with child that has the smaller
priority
If X is a leaf, just delete it
If X is not a leaf, recursively remove X from its new
subtree
Snip it off
Treap Delete Strategy
Randomized Data Structures Department of Computer Science _ UHD 20
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
De
fuzz
ific
ati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L34
E
22
D27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
1
J
Treap Delete Strategy
Randomized Data Structures Department of Computer Science _ UHD 21
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
De
fuzz
ific
ati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
3
K
15
H
16
V
17
C
12
N
10
G
6
S
24
L34
E
22
D27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
1
J
Treap Delete Strategy
Remove K
Step 1 - Rotate K with
Right Child ( S )
Randomized Data Structures Department of Computer Science _ UHD 22
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
De
fuzz
ific
ati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
6
S
15
H
16
V17
C
12
N
10
G
3
K
24
L34
E
22
D27
A
25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
1
J
After Rotating K with S
Step 2 - Rotate K with
Right Child ( N )
Treap Delete Strategy
Randomized Data Structures Department of Computer Science _ UHD 23
Treap Delete Strategy
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
6
S
15
H
16
V17
C
3
K
10
G
12
N
24
L
34
E
22
D27
A25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
1
J
After Rotating K with N
Step 3 - Rotate K with
Right Child ( L )
Randomized Data Structures Department of Computer Science _ UHD 24
After Rotating K with L
Treap Delete Strategy
Step 4 - K is a leaf
delete K
.
p6
PE
PI
PF
PA
PC
PD
Type-6
.
.
.
.
.
.
.
.
F1
F2
F4
F3
F5
F6
F8
F7
F9
PE
PA
PF
.
Defu
zzif
icati
on
Propositions
of Fuzzy Sets
Input Values
of Features
PE
PI
PF
PA
PC
PD
PE
PA
PF
Fuzzy Operations
AND - OR
priority
key
6
S
15
H
16
V17
C
24
L
10
G
12
N
3
K
34
E
22
D27
A25
P
35
O
42
R
3
G
7
D
6
K
G / 3
D / 7 K / 6
J / 2
G / 3
D / 7 J / 2
K / 6
G / 3
D / 7
J / 2
K / 6
2
J
3
G
7
D
2
J
6
K
2
J
3
G
6
K
7
D
1
J
Randomized Data Structures Department of Computer Science _ UHD 25
6
7
7
8
2
9
9
15
15
12
7
8
6
7
9
9
15
15
12
Delete (9)
Treap Delete Strategy
Randomized Data Structures Department of Computer Science _ UHD 26
7
8
6
7
9
15
9
15
12
7
8
6
7
9
15
15
12
9
7
8
6
7
9
15
15
12
Treap Delete Strategy
Randomized Data Structures Department of Computer Science _ UHD
Thank you
??
27