Download presentation

Presentation is loading. Please wait.

1
**Priority search queues: Loser trees**

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

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

4
**South-grounded range queries**

xleft xright ytop 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

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

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

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

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

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

11
**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

12
**Tournament search tree: contraction**

Remove duplicate entries Italien 1 I Niederlande 3 D P Argentinien 2 Spanien 4 A F N S Deutschland 5 Frankreich 6 Polen 7 USA 8

13
**Tournament search tree: contraction**

Promote winners up the tree Italien 1 I Niederlande 3 D P Argentinien 2 Polen 7 Spanien 4 A F N S Deutschland 5 Frankreich 6 USA 8

14
**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

15
**Tournament search tree: contraction**

To decide who is promoted, an additional match must be played! Italien 1 I Argentinien 2 Niederlande 3 D P Frankreich 6 Polen 7 Spanien 4 A F N S Deutschland 5 USA 8

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

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

18
**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

19
**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

20
**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

21
**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

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

23
**Tournament search tree: contraction**

Drop leaf nodes Italien 1 Niederlande 3 I Argentinien 2 Spanien 4 D P Deutschland 5 Frankreich 6 Polen 7 USA 8 A F N S

24
**Tournament search tree: contraction**

Put winner on top Italien 1 U Niederlande 3 I Argentinien 2 Spanien 4 D P Deutschland 5 Frankreich 6 Polen 7 USA 8 A F N S

25
**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 Spanien 4 D P Deutschland 5 Frankreich 6 Polen 7 USA 8 A F N S

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

27
**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 U, 8 F, 6 D, 5 I, 1 N, 3 P, 7 S, 4 A, 2

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
**Example with points in plane**

key = x-coordinate priority = y-coordinate 7, 1 8 1, 2 4 4, 5 2 5, 4 6 2, 4 1 3, 8 3 6, 9 5 7, 1 1, 2 2, 4 3, 8 4, 5 5, 4 6, 9

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, 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)

31
**Example: insertion of (5, 2)**

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

32
**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

33
**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

34
**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

35
**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

36
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)

37
Intro 2 Recap Begriffe: Pennant, Top node

38
**Special cases of insertion**

The tree is empty. (trivial) The key of the inserted node is larger than all other keys in the tree. (exercise!)

39
**Priority search pennant: example**

1, 2 20 20, 3 9 5, 10 4 10, 4 14 3, 7 1 9, 12 7 14, 8 13 15, 5 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15 18, 9 18

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
**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
Example: delete(20) 1, 2 20 20, 3 9 5, 10 4 10, 4 14 3, 7 1 9, 12 7 14, 8 13 15, 5 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15 18, 9 18

43
Example: delete(20) 1, 2 20 S 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 14, 8 13 15, 5 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15 18, 9 18 E

44
Example: delete(20) 1, 2 18 S 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 14, 8 13 15, 5 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15 18, 9 20 E

45
Example: delete(20) 1, 2 18 S 20, 3 9 N 5, 10 4 10, 4 14 3, 7 1 9, 12 7 14, 8 13 15, 5 16 18, 9 20 E 4, 15 3 7, 13 5 13, 6 10 16, 11 15

46
Example: delete(20) 1, 2 18 S 20, 3 9 N 5, 10 4 10, 4 14 15, 5 20 E 3, 7 1 9, 12 7 14, 8 13 18, 9 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15

47
Example: delete(20) 1, 2 18 S 20, 3 9 10, 4 20 N E 5, 10 4 15, 5 14 3, 7 1 9, 12 7 14, 8 13 18, 9 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15

48
Example: delete(20) 1, 2 18 S 10, 4 9 20, 3 20 N E 5, 10 4 15, 5 14 3, 7 1 9, 12 7 14, 8 13 18, 9 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15

49
Example: delete(20) 1, 2 18 10, 4 9 5, 10 4 15, 5 14 3, 7 1 9, 12 7 14, 8 13 18, 9 16 4, 15 3 7, 13 5 13, 6 10 16, 11 15

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
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
References R. Hinze. A simply implementation technique for priority search queues. In Proceedings of the International Conference on Functional Programming (ACM ICFP’01), pages Florence, Italy: ACM Press, 2001. E. M. McCreight. Priority search trees. In SIAM Journal on Computing, 14(2): May 1985.

53
Example: delete(20) 15, 1 20 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 20, 6 16 4, 10 3 7, 13 5 13, 5 10 16, 4 15 18, 9 18

54
Example: delete(20) 15, 1 20 S 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 20, 6 16 N 4, 10 3 7, 13 5 13, 5 10 16, 4 15 18, 9 18 E

55
Example: delete(20) 15, 1 18 S 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 20, 6 16 N 4, 10 3 7, 13 5 13, 5 10 16, 4 15 18, 9 20 E

56
Example: delete(20) 15, 1 18 S 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 20, 6 16 18, 9 20 N E 4, 10 3 7, 13 5 13, 5 10 16, 4 15

57
Example: delete(20) 15, 1 18 S 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 18, 9 16 20, 6 20 N E 4, 10 3 7, 13 5 13, 5 10 16, 4 15

58
Example: delete(20) 15, 1 18 1, 3 9 5, 6 4 10, 2 14 3, 4 1 9, 8 7 14, 8 13 18, 9 16 4, 10 3 7, 13 5 13, 5 10 16, 4 15

Similar presentations

OK

1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to 1815. Operating originally.

1 The Royal Doulton Company The Royal Doulton Company is an English company producing tableware and collectables, dating to 1815. Operating originally.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google