Вы находитесь на странице: 1из 37

Priority search queues: Loser trees

Advanced Algorithms & Data Structures Lecture Theme 06 Tobias Lauer Summer Semester 2006

Overview
Motivation: a geometric problem South-grounded range queries Priority search queues Tournament trees Knockout tournaments Winner trees Loser trees Priority search pennants Insertion Deletion Balancing Range queries

Geometric problem: south-grounded range queries


Finite set P of points (x, y) in the plane with pairwise distinct x-coordinates. Rectangle bounded by xleft, xright, ytop and the x-axis.

ytop

xleft

xright
3

South-grounded range queries

ytop

xleft

xright

enumerateRectangle: Report all points of S inside the rectangle minXinRectangle: Find the leftmost point of S inside the rectangle maxXinRectangle: Find the rightmost point of S inside the rectangle minYinXRange: Find the bottommost point of S in a given x-range
4

Problem
Maintain a set P of points (x, y) such that the operations Insert a new point: insert(x,y) Find the point with given x-coordinate: lookup(x) Delete the point with given x-coordinate: delete(x) Find a point with minimum y-coordinate: accessmin() Delete a point with minimum y-coordinate: deletemin() South-grounded range queries enumerateRectangle(xleft , xright , ytop ) minXinRectangle(xleft , xright , ytop ) maxXinRectangle(xleft , xright , ytop ) minYinXRange(xleft , xright ) can be computed efficiently. This abstract data type is called priority search queue.
5

Tournament trees
Knockout tournament n = number of players Two players play one match until a winner is determined (no ties!) The loser drops out of the tournament The winner moves stays in the tournament The overall winner is the last player who is left (i.e. the only one who hasnt lost any match) Example: final round of football world cup* Number of matches played in a K.O. tournament with n players:

* Ignoring the match for 3rd place.

Tournament tree: example

Italien 1

Italien 1

Niederlande 3

Argentinien 2

Italien 1

Niederlande 3

Spanien 4

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

Tournament search tree


Insert a split key to each internal node direct the search The split key is the largest key in the left subtree

Italien 1 I

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

Back to geometric problem


Idea: Points are players in a tournament x-coordinate is the name y-coordinate is the strength Point with minimum y is the overall winner (at the root) and can always be accessed in O(1) The tree is a search tree for the x-coordinates, so the x-ranges can easily be determined

Tournament search tree


Problem: not very efficient 2n -1 nodes for n values many repeated entries
Italien 1 I

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

10

Tournament search tree: contraction


1st idea: remove duplicate entries, promote winners up the tree

Italien 1 I

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

11

Tournament search tree: contraction


Remove duplicate entries

Italien 1 I

Niederlande 3 D P

Argentinien 2 A F N

Spanien 4 S

Deutschland 5

Frankreich 6

Polen 7

USA 8

12

Tournament search tree: contraction


Promote winners up the tree

Italien 1 I

Niederlande 3 D P

Argentinien 2 A F

Polen 7 N

Spanien 4 S

Deutschland 5

Frankreich 6

USA 8

13

Tournament search tree: contraction


Promote winners up the tree

Italien 1 I

Niederlande 3 D P

Argentinien 2 A

Frankreich 6 F

Polen 7 N

Spanien 4 S

Deutschland 5

USA 8

14

Tournament search tree: contraction


To decide who is promoted, an additional match must be played!

Italien 1 I

Argentinien 2 D

Niederlande 3 P

Frankreich 6 A F

Polen 7 N

Spanien 4 S

Deutschland 5

USA 8

15

Contracted winner tree


Still a search tree (nodes are promoted along their search path) Only n nodes for n elements, but additional matches are required

Italien 1 I

Argentinien 2 D

Niederlande 3 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

Spanien 4 S

USA 8

16

Contracted winner tree


The tree is a min-heap for the strength and a search tree for the labels. Such a tree is called priority search tree (McCreight 1985).

Italien 1 I

Argentinien 2 D

Niederlande 3 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

Spanien 4 S

USA 8

17

Tournament search tree: contraction


2nd idea: label the internal nodes with loser of the match instead of winner

Italien 1 I

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

18

Tournament search tree: contraction


2nd idea: label the internal nodes with loser
Italien 1

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

19

Tournament search tree: contraction


2nd idea: label the internal nodes with loser
Italien 1

Niederlande 3 I

Italien 1 D

Niederlande 3 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

20

Tournament search tree: contraction


2nd idea: label the internal nodes with loser
Italien 1

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Argentinien 2 A

Italien 1 F

Niederlande 3 N

Spanien 4 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

21

Tournament search tree: contraction


2nd idea: label the internal nodes with loser
Italien 1

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

USA 8 S

Argentinien 2

Deutschland 5

Frankreich 6

Italien 1

Niederlande 3

Polen 7

Spanien 4

USA 8

22

Tournament search tree: contraction


Drop leaf nodes
Italien 1

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

USA 8 S

23

Tournament search tree: contraction


Put winner on top
Italien 1 U

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

USA 8 S

24

Topped loser tree


n nodes for n elements no additional matches needed still a search tree but: no heap!

Italien 1 U

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

USA 8 S

25

Topped loser tree


The tree is a semi-heap: each node dominates one of its subtrees (the subtree where it originally came from) To see where a node came from, check its split key!
Italien 1 U

Niederlande 3 I

Argentinien 2 D

Spanien 4 P

Deutschland 5 A

Frankreich 6 F

Polen 7 N

USA 8 S

26

Priority search pennant (Hinze 2001)


label key strength priority
I, 1 U

N, 3 I

A, 2 D

S, 4 P

D, 5 A

F, 6 F

P, 7 N

U, 8 S

A, 2

D, 5

F, 6

I, 1

N, 3

P, 7

S, 4

U, 8
27

Priority search pennants: invariants


Search tree condition: for each node, the keys in the left subtree must be less than or equal to the split key and the keys in the right subtree must be greater than the split key. Also, no two elements may have the same key. Semi-heap condition: for all nodes, the priority must be less than or equal to the priorities in the subtree from which the element originates. (An element originates from the left subtree if its key is less than or equal to the split key, otherwise it originates from the right subtree.) Split key condition: every key must also occur as a split key and vice versa.

Corollaries: 1. In each internal node, we find the loser of the match played between the winners of the left and right subtrees. 2. Each proper subtree contains all elements originating from that subtree except the winner.
28

Example with points in plane


key = x-coordinate priority = y-coordinate

7, 1 8

1, 2 4 5, 4 6 3, 8 3 2, 4 3, 8 4, 5 5, 4 6, 9 5 6, 9

4, 5 2 2, 4 1 1, 2

7, 1

29

Insertion of a new point (x, y)


We must ensure that search-tree condition, semi-heap condition, and splitkey condition are still fulfilled after the insertion. If the key x is already in the tree, dont do anything Otherwise, create a new node N with pair (x, y) and x as the split key Walk down the search path for x, because (x, y) must end up in a node somewhere on that path If we encounter a node M whose priority yM is larger than yN and which originates from the same subtree to where the search path of x leads, then swap the pairs of the nodes M and N (but leave the split keys as they are). Continue by the same method until the end of the search path When we arrive at the end, insert N as the appropriate child (as dictated by the search tree condition)

30

Example: insertion of (5, 2)


5, 2 5 15, 1 20

1, 3 9

7, 8 4

10, 2 14

3, 4 1

9, 13 7

14, 8 13

20, 6 16

4, 10 3

13, 5 10

16, 4 15

18, 9 18
31

Example: insertion of (5, 2)


15, 1 20

5, 2 5

1, 3 9

7, 8 4

10, 2 14

3, 4 1

9, 13 7

14, 8 13

20, 6 16

4, 10 3

13, 5 10

16, 4 15

18, 9 18
32

Example: insertion of (5, 2)


15, 1 20

5, 2 9

7, 8 4

1, 3 5

10, 2 14

3, 4 1

9, 13 7

14, 8 13

20, 6 16

4, 10 3

13, 5 10

16, 4 15

18, 9 18
33

Example: insertion of (5, 2)


15, 1 20

5, 2 9

1, 3 4

10, 2 14

3, 4 1

7, 8 5

9, 13 7

14, 8 13

20, 6 16

4, 10 3

13, 5 10

16, 4 15

18, 9 18
34

Example: insertion of (5, 2)


15, 1 20

5, 2 9

1, 3 4

10, 2 14

3, 4 1

9, 13 7

14, 8 13

20, 6 16

4, 10 3

7, 8 5

13, 5 10

16, 4 15

18, 9 18
35

Analysis of insertion
If the key x is already in the tree, dont do anything Otherwise, create a new node N with pair (x, y) and x as the split key Walk down the search path for x, because (x, y) must end up in a node somewhere on that path If we encounter a node M whose priority yM is larger than yN and which originates from the same subtree to where the search path of x leads, then swap the pairs of the nodes M and N (but leave the split keys as they are). Continue by the same method until the end of the search path At the end of the search path, insert N as the appropriate child (as dictated by the search tree condition)

36

References
R. Hinze. A simply implementation technique for priority search queues. In Proceedings of the International Conference on Functional Programming (ACM ICFP01), pages 110-121. Florence, Italy: ACM Press, 2001. E. M. McCreight. Priority search trees. In SIAM Journal on Computing, 14(2):257-276. May 1985.

37

Вам также может понравиться