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 2 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 hasn’t 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. 6 Tournament tree: example Italien 1 Italien 1 Argentinien 2 Argentinien 2 Deutschland 5 Niederlande 3 Italien 1 Frankreich 6 Niederlande 3 Italien 1 Niederlande 3 Polen 7 Spanien 4 Spanien 4 USA 8 7 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 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N S Argentinien 2 Deutschland 5 Frankreich 6 Italien 1 Niederlande 3 Polen 7 Spanien 4 USA 8 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 9 Tournament search tree Problem: not very efficient 2·n -1 nodes for n values many repeated entries Italien 1 I Italien 1 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 Spanien 4 A F Deutschland 5 Frankreich 6 N S 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 Deutschland 5 Frankreich 6 Polen 7 Spanien 4 N S USA 8 13 Tournament search tree: contraction Promote winners up the tree Italien 1 I Niederlande 3 D P Argentinien 2 Frankreich 6 Polen 7 Spanien 4 A F N 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 Niederlande 3 D A Deutschland 5 P Frankreich 6 Polen 7 Spanien 4 F N S 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 Niederlande 3 D Deutschland 5 A P Frankreich 6 Polen 7 Spanien 4 F N 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 Niederlande 3 D Deutschland 5 A P Frankreich 6 Polen 7 Spanien 4 F N 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 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 I Italien 1 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 Niederlande 3 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 Spanien 4 D P Argentinien 2 Italien 1 Niederlande 3 Spanien 4 A F N 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 Spanien 4 D P Deutschland 5 Frankreich 6 Polen 7 F N A Argentinien 2 Deutschland 5 Frankreich 6 Italien 1 Niederlande 3 USA 8 S Polen 7 Spanien 4 USA 8 22 Tournament search tree: contraction Drop leaf nodes Italien 1 Niederlande 3 I Deutschland 5 A Argentinien 2 Spanien 4 D P Frankreich 6 Polen 7 F N USA 8 S 23 Tournament search tree: contraction Put winner on top Italien 1 U Niederlande 3 I Deutschland 5 A Argentinien 2 Spanien 4 D P Frankreich 6 Polen 7 F 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 Deutschland 5 A Argentinien 2 Spanien 4 D P Frankreich 6 Polen 7 F 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 Deutschland 5 A Argentinien 2 Spanien 4 D P Frankreich 6 Polen 7 F 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 A, 2 F, 6 F D, 5 F, 6 P, 7 N I, 1 N, 3 U, 8 S 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 4, 5 2 2, 4 1 1, 2 3, 8 3 2, 4 3, 8 6, 9 5 4, 5 5, 4 7, 1 6, 9 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, don‘t 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 3, 4 1 10, 2 14 9, 13 7 4, 10 3 14, 8 13 13, 5 10 20, 6 16 16, 4 15 18, 9 18 31 Example: insertion of (5, 2) 15, 1 20 5, 2 5 1, 3 9 7, 8 4 3, 4 1 10, 2 14 9, 13 7 4, 10 3 14, 8 13 13, 5 10 20, 6 16 16, 4 15 18, 9 18 32 Example: insertion of (5, 2) 15, 1 20 5, 2 9 7, 8 4 3, 4 1 1, 3 5 10, 2 14 9, 13 7 4, 10 3 14, 8 13 13, 5 10 20, 6 16 16, 4 15 18, 9 18 33 Example: insertion of (5, 2) 15, 1 20 5, 2 9 1, 3 4 3, 4 1 7, 8 5 4, 10 3 10, 2 14 9, 13 7 14, 8 13 13, 5 10 20, 6 16 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 4, 10 3 7, 8 5 14, 8 13 13, 5 10 20, 6 16 16, 4 15 18, 9 18 35 Analysis of insertion • If the key x is already in the tree, don‘t 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 Intro 2 • Recap • Begriffe: Pennant, Top node 37 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!) 38 Priority search pennant: example 1, 2 20 20, 3 9 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 15, 5 16 16, 11 15 18, 9 18 39 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). 40 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. 41 Example: delete(20) 1, 2 20 20, 3 9 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 15, 5 16 16, 11 15 18, 9 18 42 Example: delete(20) S 1, 2 20 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 15, 5 16 16, 11 15 18, 9 18 E 43 Example: delete(20) S 1, 2 18 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 15, 5 16 16, 11 15 18, 9 20 E 44 Example: delete(20) S 1, 2 18 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 15, 5 16 18, 9 20 E 16, 11 15 45 Example: delete(20) S 1, 2 18 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 15, 5 20 14, 8 13 13, 6 10 E 18, 9 16 16, 11 15 46 Example: delete(20) S 1, 2 18 20, 3 9 N 10, 4 20 5, 10 4 15, 5 14 3, 7 1 9, 12 7 4, 15 3 E 7, 13 5 14, 8 13 13, 6 10 18, 9 16 16, 11 15 47 Example: delete(20) S 1, 2 18 10, 4 9 N 20, 3 20 5, 10 4 15, 5 14 3, 7 1 9, 12 7 4, 15 3 E 7, 13 5 14, 8 13 13, 6 10 18, 9 16 16, 11 15 48 Example: delete(20) 1, 2 18 10, 4 9 5, 10 4 15, 5 14 3, 7 1 9, 12 7 4, 15 3 7, 13 5 14, 8 13 13, 6 10 18, 9 16 16, 11 15 49 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? 50 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. 51 References • R. Hinze. A simply implementation technique for priority search queues. In Proceedings of the International Conference on Functional Programming (ACM ICFP’01), 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. 52 Example: delete(20) 15, 1 20 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 20, 6 16 16, 4 15 18, 9 18 53 Example: delete(20) S 15, 1 20 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 N 16, 4 15 20, 6 16 18, 9 18 E 54 Example: delete(20) S 15, 1 18 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 N 16, 4 15 20, 6 16 18, 9 20 E 55 Example: delete(20) S 15, 1 18 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 N 20, 6 16 18, 9 20 E 16, 4 15 56 Example: delete(20) S 15, 1 18 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 N 18, 9 16 20, 6 20 E 16, 4 15 57 Example: delete(20) 15, 1 18 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 4, 10 3 7, 13 5 14, 8 13 13, 5 10 18, 9 16 16, 4 15 58