Skewed Binary Search Trees 6 2 10 1 3 7 12 4 8 11 13 5 9 - - PowerPoint PPT Presentation

skewed binary search trees
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

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

slide-2
SLIDE 2

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 in progress with Gabriel Moruz

Dagstuhl seminar on Data Structures, February 26-March 3, 2006.

1

slide-3
SLIDE 3

Perfectly Balanced Search Trees

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Skewed Binary Search Trees

2

slide-4
SLIDE 4

Skewed Binary Search Trees

x

1 − α

⌊α(n − 1)⌋ ⌈(1 − α)(n − 1)⌉

α

Skewed Binary Search Trees

3

slide-5
SLIDE 5

Skewed Binary Search Trees

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

α = 0.5

Skewed Binary Search Trees

4

slide-6
SLIDE 6

Skewed Binary Search Trees

1 7 2 13 14 15 6 11 8 12 9 10 3 4 5

α = 0.4

Skewed Binary Search Trees

5

slide-7
SLIDE 7

Skewed Binary Search Trees

3 10 9 11 12 13 4 5 8 14 15 6 1 2 7

α = 0.2

Skewed Binary Search Trees

6

slide-8
SLIDE 8

Skewed Binary Search Trees

5 1 11 7 13 12 8 15 2 9 6 4 10 3 14

α = 0.05

Skewed Binary Search Trees

7

slide-9
SLIDE 9

Skewed Binary Search Trees

— Average Node Depth

≤ 1 −α log2 α − (1 − α) log2(1 − α)

  • H(α)

· n + 1 n · log2(n + 1) − 2

Nievergelt and E. M. Reingold, 1972

Skewed Binary Search Trees

8

slide-10
SLIDE 10

H(α)

α H(α) 1 0.8 0.6 0.4 0.2 10 8 6 4 2

Skewed Binary Search Trees

9

slide-11
SLIDE 11

Comparisons

α 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

slide-12
SLIDE 12

Running Time

α 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

slide-13
SLIDE 13

Conclusion

Skewed binary search trees beat Perfectly balanced binary search trees !

Skewed Binary Search Trees

12

slide-14
SLIDE 14

Conclusion

Skewed binary search trees c a n beat Perfectly balanced binary search trees !

Skewed Binary Search Trees

12

slide-15
SLIDE 15

Why ?

Skewed Binary Search Trees

13

slide-16
SLIDE 16

Why ?

The costs going left and right are different ! Possible reasons

  • Number of instructions
  • Branch mispredictions
  • Cache faults (what is a good memory layout?)
  • ...

Skewed Binary Search Trees

14

slide-17
SLIDE 17

Expected Cost

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

slide-18
SLIDE 18

Expected Cost

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

slide-19
SLIDE 19

Search Code

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

slide-20
SLIDE 20

Branch Mispredictions

α 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

slide-21
SLIDE 21

Simple Layouts

6 2 1 3 4 5 7

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

slide-22
SLIDE 22

Running Time for Simple Layouts

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

slide-23
SLIDE 23

Cache Faults for Simple Layouts

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

slide-24
SLIDE 24

Blocked Layouts — pqDFSk

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

k = 3

6 10 2 12 13 14 15 1 5 4 3 9 8 7 11

  • layout the k heavest nodes in order of decreasing size
  • recurse on subtrees in order of decreasing size

Skewed Binary Search Trees

22

slide-25
SLIDE 25

Blocked Layouts — veb

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

  • top = ⌈√n⌉ heavest nodes
  • recurse on top and bottom trees in order of decreasing size

Skewed Binary Search Trees

23

slide-26
SLIDE 26

Running Time for Blocked Layouts

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

slide-27
SLIDE 27

Cache Faults for Blocked Layouts

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

slide-28
SLIDE 28

Experimental Summary

α 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

slide-29
SLIDE 29

Conclusion

Skewed binary search trees c a n beat Perfectly balanced binary search trees because The costs going left and right are different !

Skewed Binary Search Trees

27

slide-30
SLIDE 30

Questions

  • Can we give a precise theoretical explanation of the

experimental results ?

  • Different values of n ?
  • Different computer architectures ?
  • What is the best layout of a tree on a given machine ?
  • What is the best tree ?
  • ...

1 7 2 13 14 15 6 11 8 12 9 10 3 4 5

Skewed Binary Search Trees

28

slide-31
SLIDE 31

Experimental setup

  • AMD Athlon XP 2400+
  • 2.0 GHz
  • 256 Kb L2 cache
  • 64 Kb L1 data cache
  • 64 Kb L1 instruction cache
  • 1Gb RAM
  • Linux 2.6.8.1
  • GCC 3.3.2
  • Tree nodes = 12 bytes

Skewed Binary Search Trees

29