Download presentation

Presentation is loading. Please wait.

Published byZoe Thompson Modified over 3 years ago

1
Priority search queues: Loser trees Advanced Algorithms & Data Structures Lecture Theme 06 Tobias Lauer Summer Semester 2006

2
2 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

3
3 Geometric problem: south-grounded range queries Finite set P of points (x, y) in the plane with pairwise distinct x-coordinates. Rectangle bounded by x left, x right, y top and the x-axis. x left x right y top

4
4 South-grounded range queries 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 x left x right y top

5
5 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(x left, x right, y top ) minXinRectangle(x left, x right, y top ) maxXinRectangle(x left, x right, y top ) minYinXRange(x left, x right ) can be computed efficiently. This abstract data type is called priority search queue.

6
6 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.

7
7 Tournament tree: example Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Italien 1 Niederlande 3 Argentinien 2Italien 1Niederlande 3Spanien 4

8
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 Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P

9
9 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

10
10 Tournament search tree Problem: not very efficient 2·n -1 nodes for n values many repeated entries Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P

11
11 Tournament search tree: contraction 1 st idea: remove duplicate entries, promote winners up the tree Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P

12
12 Tournament search tree: contraction Remove duplicate entries Deutschland 5Frankreich 6Polen 7USA 8 Argentinien 2 Italien 1 Niederlande 3 Spanien 4 I D A F NS P

13
13 Tournament search tree: contraction Promote winners up the tree Deutschland 5Frankreich 6USA 8 Argentinien 2 Italien 1 Niederlande 3 Spanien 4 I Polen 7 D A F NS P

14
14 Tournament search tree: contraction Promote winners up the tree Deutschland 5USA 8 Argentinien 2 Italien 1 Niederlande 3 Spanien 4 I Polen 7Frankreich 6 D A F NS P

15
15 Tournament search tree: contraction To decide who is promoted, an additional match must be played! Deutschland 5USA 8 Italien 1 Niederlande 3 Spanien 4 I Polen 7Frankreich 6 Argentinien 2 D A F NS P

16
16 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 USA 8 Italien 1 Niederlande 3 Spanien 4 I D S P Polen 7Frankreich 6 Argentinien 2 Deutschland 5 A F N

17
17 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). USA 8 Italien 1 Niederlande 3 Spanien 4 I D S P Polen 7Frankreich 6 Argentinien 2 Deutschland 5 A F N

18
18 Tournament search tree: contraction 2 nd idea: label the internal nodes with loser of the match instead of winner Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P

19
19 Tournament search tree: contraction 2 nd idea: label the internal nodes with loser Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P Italien 1

20
20 Tournament search tree: contraction 2 nd idea: label the internal nodes with loser Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1 Niederlande 3 Spanien 4 I D A F NS P Italien 1 Niederlande 3

21
21 Tournament search tree: contraction 2 nd idea: label the internal nodes with loser Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 Argentinien 2Italien 1Niederlande 3Spanien 4 I D A F NS P Italien 1 Niederlande 3 Argentinien 2Spanien 4

22
22 Tournament search tree: contraction 2 nd idea: label the internal nodes with loser Argentinien 2Deutschland 5Frankreich 6Italien 1Niederlande 3Polen 7Spanien 4USA 8 I D A F NS P Italien 1 Niederlande 3 Argentinien 2Spanien 4 Deutschland 5Frankreich 6Polen 7USA 8

23
23 Tournament search tree: contraction Drop leaf nodes I D A F NS P Italien 1 Niederlande 3 Argentinien 2Spanien 4 Deutschland 5Frankreich 6Polen 7USA 8

24
24 Tournament search tree: contraction Put winner on top I D P Italien 1 Niederlande 3 Argentinien 2Spanien 4 Deutschland 5Frankreich 6Polen 7USA 8 A F NS U

25
25 Topped loser tree n nodes for n elements no additional matches needed still a search tree but: no heap! I D P Italien 1 Niederlande 3 Argentinien 2Spanien 4 Deutschland 5Frankreich 6Polen 7USA 8 U A F NS

26
26 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! I D P Italien 1 Niederlande 3 Argentinien 2Spanien 4 Deutschland 5Frankreich 6Polen 7USA 8 U A F NS

27
27 D, 5 A F, 6 F P, 7 N U, 8 S A, 2 D S, 4 P N, 3 I U, 8F, 6D, 5I, 1N, 3P, 7S, 4A, 2 I, 1 U Priority search pennant (Hinze 2001) label key strength priority

28
28 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.

29
29 3, 82, 44, 55, 46, 9 7, 1 1, 2 2, 4 1 3, 8 3 6, 9 5 4, 5 2 5, 4 6 1, 2 4 7, 1 8 Example with points in plane key = x-coordinate priority = y-coordinate

30
30 Insertion of a new point (x, y) We must ensure that search-tree condition, semi-heap condition, and split- key 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 y M is larger than y N 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)

31
31 18, , , , , 4 1 9, , , , , , , , 2 5 Example: insertion of (5, 2)

32
32 18, , , , , 4 1 9, , , , , , , , 2 5 Example: insertion of (5, 2)

33
33 18, , , , , 4 1 9, , , , , , , , 3 5 Example: insertion of (5, 2)

34
34 18, , , , , 4 1 9, , , , , , , , 8 5 Example: insertion of (5, 2)

35
35 18, , , , , 4 1 9, , , , , , , , 8 5 Example: insertion of (5, 2)

36
36 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 y M is larger than y N 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)

37
37 Intro 2 Recap Begriffe: Pennant, Top node

38
38 Special cases of insertion 1.The tree is empty. (trivial) 2.The key of the inserted node is larger than all other keys in the tree. (exercise!)

39
39 18, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Priority search pennant: example

40
40 Deletion of a point with key x Again, we must ensure that search-tree condition, semi-heap condition, and split-key condition are still fulfilled after the deletion. Follow the search path for x until the end. On the way down, remember… the node N containing x as its key the node S containing x as its split key If x is not contained in the tree, we are done. Otherwise, consider the last node E on the search path. Observe that… E has at most one child (otherwise, we could proceed further down). The split key of E is either x itself or the symmetric predecessor of x. (The symmetric predecessor of x is the largest key in the tree that is smaller than x.) Exchange the split keys of E and S (if E S) and remove E from the tree (replace it by its child if it has one).

41
41 Deletion of a point with key x Walk back the search path until N. At each node C on the way up: If the priority of C is smaller than that in E, exchange the points in E and C (but leave the split keys). Note that this may flip the dominated subtree of C but never destroys the semi-heap condition! Continue by the same method up the tree. If we arrive at N, exchange the points of N and E. Now E can be completely deleted.

42
42 18, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20)

43
43 18, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20) S N E

44
44 18, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20) S N E

45
45 18, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20) S N E

46
46 15, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20) S N E

47
47 10, , , , , 7 1 9, , , , , , 3 9 1, , 13 5 Example: delete(20) S N E

48
48 20, , , , , 7 1 9, , , , , , 4 9 1, , 13 5 Example: delete(20) S N E

49
49 4, , , , 7 1 9, , , , , , 4 9 1, , 13 5 Example: delete(20)

50
50 Analysis of delete We require one complete walk down the search path. Find N, S and E Then we walk up the same search path again (until N) Swap points if necessary. Running time: If the tree is balanced: How can we keep a priority search pennant balanced?

51
51 Balancing We always leave the top node as it is and only balance the loser tree. The loser tree is a standard binary tree. Binary trees can be balanced with different schemes Information for balancing must be kept in the nodes, e.g. height difference of subtrees (AVL trees) weight (#nodes) of the subtree (BB[α] trees, IPR balancing) color (red-black trees) If the tree is out of balance after an insertion or deletion, the balance is restored by restructuring the tree using rotations.

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

53
53 18, , , , , 4 1 9, , , , , , , , 13 5 Example: delete(20)

54
54 18, , , , , 4 1 9, , , , , , , , 13 5 Example: delete(20) S N E

55
55 18, , , , , 4 1 9, , , , , , , , 13 5 Example: delete(20) S N E

56
56 18, , , , , 4 1 9, , , , , , , , 13 5 Example: delete(20) S N E

57
57 20, , , , , 4 1 9, , , , , , , , 13 5 Example: delete(20) S N E

58
58 4, , , , 4 1 9, , , , , , , , 13 5 Example: delete(20)

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google