Skewed Binary Search Trees
1 7 2 13 14 15 6 11 8 12 9 10 3 4 5
Gerth Stølting Brodal University of Aarhus Joint work with Gabriel Moruz
Dagstuhl seminar on Data Structures, February 26-March 3, 2006.
1
Skewed Binary Search Trees 6 2 10 1 3 7 12 4 8 11 13 5 9 - - PowerPoint PPT Presentation
Skewed Binary Search Trees 6 2 10 1 3 7 12 4 8 11 13 5 9 14 15 Gerth Stlting Brodal University of Aarhus Joint work with Gabriel Moruz Dagstuhl seminar on Data Structures, February 26-March 3, 2006. 1 Skewed Binary Search
1 7 2 13 14 15 6 11 8 12 9 10 3 4 5
Gerth Stølting Brodal University of Aarhus Joint work with Gabriel Moruz
Dagstuhl seminar on Data Structures, February 26-March 3, 2006.
1
1 7 2 13 14 15 6 11 8 12 9 10 3 4 5
Gerth Stølting Brodal University of Aarhus Joint work in progress with Gabriel Moruz
Dagstuhl seminar on Data Structures, February 26-March 3, 2006.
1
Skewed Binary Search Trees
2
x
⌊α(n − 1)⌋ ⌈(1 − α)(n − 1)⌉
Skewed Binary Search Trees
3
Skewed Binary Search Trees
4
1 7 2 13 14 15 6 11 8 12 9 10 3 4 5
Skewed Binary Search Trees
5
3 10 9 11 12 13 4 5 8 14 15 6 1 2 7
Skewed Binary Search Trees
6
5 1 11 7 13 12 8 15 2 9 6 4 10 3 14
Skewed Binary Search Trees
7
≤ 1 −α log2 α − (1 − α) log2(1 − α)
· n + 1 n · log2(n + 1) − 2
Nievergelt and E. M. Reingold, 1972
Skewed Binary Search Trees
8
α H(α) 1 0.8 0.6 0.4 0.2 10 8 6 4 2
Skewed Binary Search Trees
9
α Comparisons 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 300 250 200 150 100 50
n = 20.000
Skewed Binary Search Trees
10
α Running time, 10−6 sec 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.15 0.145 0.14 0.135 0.13 0.125 0.12
Best running time achieved for α ≈ 0.3 !?
Skewed Binary Search Trees
11
Skewed Binary Search Trees
12
Skewed Binary Search Trees
12
Skewed Binary Search Trees
13
Skewed Binary Search Trees
14
cost(α) = (α · {left cost} + (1 − α) · {right cost}) · H(α)
α cost(α) 1 0.8 0.6 0.4 0.2 8 7 6 5 4 3 2 1
left cost = 1 and right cost = 3
Skewed Binary Search Trees
15
cost(α) = (α · {left cost} + (1 − α) · {right cost}) · H(α)
α cost(α) 1 0.8 0.6 0.4 0.2 8 7 6 5 4 3 2 1
left cost = 1 and right cost = 0 .. 28
Skewed Binary Search Trees
16
int search(int root, int key) { if (root==NULLV) return NULLV; if (key==t[root].key) return root; if(key>t[root].key) return search(t[root].right, key); else return search(t[root].left, key); }
Skewed Binary Search Trees
17
α Branch mispredictions 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 22 20 18 16 14 12 10 8 6 4 2
Static branch prediction: left cost = 1 and right cost = 0
Skewed Binary Search Trees
18
7 1 4 6 2 3 5
Random
1 2 3 4 5 6 7
Inorder
3 1 5 2 4 6 7
BFS
3 5 6 7 4 1 2
DFS
Skewed Binary Search Trees
19
Inorder Random BFS DFSr DFSl α Running time, 10−6 sec 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.35 0.3 0.25 0.2 0.15 0.1
DFS < Inorder < BFS < Random DFS achieves the best performance for α ≈ 0.2 !
Skewed Binary Search Trees
20
Inorder Random BFS DFSr DFSl α Cache faults 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 14 12 10 8 6 4 2
DFS ≈ expected left cost = 1 and right cost = 1 cache-line size
Skewed Binary Search Trees
21
13 14 15 11 12 7 9 10 3 4 5 1 2 6 8
9 2 1 1 15 5 2 3 1 1 3 2 1 5 3
6 10 2 12 13 14 15 1 5 4 3 9 8 7 11
Skewed Binary Search Trees
22
13 14 15 11 12 7 9 10 3 4 5 1 2 6 8
9 2 1 1 15 5 2 3 1 1 2 1 5 3 3
top
6 10 2 12 13 14 15 1 7 8 9 3 4 5 11
Skewed Binary Search Trees
23
vEB pqDFS8 pqDFS4 pqDFS2 DFSr α Running time, 10−6 sec 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.2 0.19 0.18 0.17 0.16 0.15 0.14 0.13 0.12
Skewed Binary Search Trees
24
vEB pqDFS8 pqDFS4 pqDFS2 DFSr α Cache faults 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 5 4.5 4 3.5 3 2.5 2 1.5
Skewed Binary Search Trees
25
α Comparisons 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 300 250 200 150 100 50 vEB pqDFS8 pqDFS4 pqDFS2 DFSr α Running time, 10−6 sec 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0.2 0.19 0.18 0.17 0.16 0.15 0.14 0.13 0.12 α Branch mispredictions 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 22 20 18 16 14 12 10 8 6 4 2 vEB pqDFS8 pqDFS4 pqDFS2 DFSr α Cache faults 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 5 4.5 4 3.5 3 2.5 2 1.5
Skewed Binary Search Trees
26
Skewed Binary Search Trees
27
experimental results ?
1 7 2 13 14 15 6 11 8 12 9 10 3 4 5
Skewed Binary Search Trees
28
Skewed Binary Search Trees
29