Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

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

Ähnliche Präsentationen


Präsentation zum Thema: "Priority search queues: Loser trees Advanced Algorithms & Data Structures Lecture Theme 06 Tobias Lauer Summer Semester 2006."—  Präsentation transkript:

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

2 2 Intro 2 Recap Begriffe: Pennant, Top node Linien gestrichelt vs. durchgezogen

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

4 4 18, 9 18 4, 15 3 13, 6 10 16, 11 15 3, 7 1 9, 12 7 14, 8 13 15, 5 16 5, 10 4 10, 4 14 20, 3 9 1, 2 20 7, 13 5 Priority search pennant: example

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

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

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

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

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

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

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

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

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

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

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

16 16 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. Rotations preserve the search-tree property! But what about the semi-heap property?

17 17 5, 6 4 7, 3 9 Rotation t1t1 t2t2 t3t3 5, 6 4 7, 3 9 t1t1 t2t2 t3t3 Bad news: Rotation can destroy the semi-heap property! Good news: We can repair it!

18 18 2, 6 4 7, 3 9 1st case: x A s A and x B s B t1t1 t2t2 t3t3 2, 6 4 7, 3 9 t1t1 t2t2 t3t3 A B No repair needed!

19 19 5, 6 4 7, 3 9 2nd case: x A s A and x B > s B t1t1 t2t2 t3t3 7, 3 4 5, 6 9 t1t1 t2t2 t3t3 A B Swap!

20 20 3rd case: x A > s A and x B s B 2, 6 4 10, 3 9 t1t1 t2t2 t3t3 2, 6 4 10, 3 9 t1t1 t2t2 t3t3 2, 3 4 10, 6 9 t1t1 t2t2 t3t3 2, 3 4 10, 6 9 t1t1 t2t2 t3t3 Case 3a y A y B Case 3b y A > y B A B A B No repair needed!

21 21 4th case: x A > s A and x B > s B 7, 6 4 10, 3 9 t1t1 t2t2 t3t3 10, 3 4 7, 6 9 t1t1 t2t2 t3t3 7, 3 4 10, 6 9 t1t1 t2t2 t3t3 7, 3 4 10, 6 9 t1t1 t2t2 t3t3 A B A B Case 4a y A y B Case 4b y A > y B No repair needed! Swap!

22 22 Rotations Right rotation: Condition for swapping: x B > s B and y A y B Only one additional match is required Repair takes constant time! Left rotation: Symmetric condition Double rotations (left-right and right-left) Either: like two single rotations Or: Exercise 3

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

24 24 MinXinRectangle Sei Min ein Zeiger auf den besten bisher gefundenen Knoten (zu Beginn setze Min auf einen Dummyknoten D mit x D = x max ). Beim Besuch von Knoten N: 1)Falls N der Suchbedingung genügt (x x N < x Min AND y N y), setze Min = N. 2)Besuche den linken Teilbaum, falls nötig, d.h. falls dieser existiert UND a)gültige x-Werte enthalten kann: s N x UND b)gültige y-Werte enthalten kann: y N y ODER N stammt aus seinem rechten Teilbaum (x N > s N ) 3)Besuche den rechten Teilbaum, falls nötig, d.h. falls dieser existiert UND d)gültige x-Werte enthalten kann: s N < x Min UND e)gültige y-Werte enthalten kann: y N y ODER N stammt aus seinem linken Teilbaum (x N s N ) Depth-first search (in preorder) with pruning

25 25 Analyse Definition: Ein bei minXinRectangle(x, x, y) inspizierter innerer Knoten N heißt potentiell verzweigend, wenn N bei seinem ersten Besuch alle der Bedingungen a-d erfüllt. N heißt (tatsächlich) verzweigend für minXinRectangle(x, x, y), wenn bei der Suche beide seiner Söhne inspiziert werden. Bemerkungen: (1) Für potentiell verzweigende Knoten N gilt immer y N y. (2) Jeder tatsächliche verzweigende Knoten ist auch potentiell verzweigend. (3) Umgekehrt muss jedoch nicht jeder potentiell verzweigende Knoten auch tatsächlich verzweigend sein.

26 26 Analyse Beobachtung 1: Wenn während minXinRectangle (x, x, y) im linken Teilbaum eines potentiell verzweigenden Knotens N ein neuer Knoten L mit y L y und x x L x Min gefunden wird, so ist N nicht tatsächlich verzweigend. Beobachtung 2: Für den Splitwert s N eines Knotens N, der bei einer Suchanfrage (x, x, y) potentiell verzweigend ist, gilt beim ersten Besuch von N: x s N < k Min. [Die Aussage ist die Zusammenfassung von (a) und (d).]

27 27 Verteilung verzweigender Knoten Lemma 1: Sei N ein verzweigender Knoten für minXinRectangle(x, x, y). Dann gibt es im rechten Teilbaum von N keine potentiell verzweigenden Knoten. Beweis: Da N verzweigend, ist x s N < x Min (s. Beobachtung 2). Annahme: R im rechten Teilbaum ist potentiell verzweigend, d.h. x s R < x Min xNsNxNsN xRsRxRsR sNsN sRsR xx Min

28 28 Beweis Lemma 1: Sei N ein verzweigender Knoten für minXinRectangle(x, x, y). Dann gibt es im rechten Teilbaum von N keine potentiell verzweigenden Knoten. 1. Fall: R stammt aus seinem linken Teilbaum (x R s R ) xNsNxNsN xRsRxRsR sNsN sRsR xx Min xRxR

29 29 Beweis xNsNxNsN xRsRxRsR sNsN sRsR x x Min xRxR xW xW xWxW Lemma 1: Sei N ein verzweigender Knoten für minXinRectangle(x, x, y). Dann gibt es im rechten Teilbaum von N keine potentiell verzweigenden Knoten. 2. Fall: R stammt aus seinem rechten Teilbaum (x R > s R )

30 30 Verteilung verzweigender Knoten Lemma 2: Trifft man bei minXinRectangle (x, x, y) im linken Teilbaum eines potentiell verzweigenden Knotens N auf einen weiteren potentiell verzweigenden Knoten L, so ist N nicht tatsächlich verzweigend. Beweis: Da N potentiell verzweigend ist, gilt x s N < x Min. Sei L im linken Teilbaum potentiell verzweigend, d.h. x s L < x Min xNsNxNsN xLsLxLsL sNsN sLsL xLxL xx Min xWxW xWxW

31 31 Verzweigende Knoten Lemma 1: Sei N verzweigender Knoten für minXinRectangle(x, x, y). Dann gibt es im rechten Teilbaum von N keine potentiell verzweigenden Knoten. Lemma 2: Trifft man bei minXinRectangle(x, x, y) im linken Teilbaum eines potentiell verzweigenden Knotens N auf einen weiteren potentiell verzweigenden Knoten L, so ist N nicht tatsächlich verzweigend. Satz 1: Bei einer MinXinRectangle-Suche (x, x, y) in einem Priority Search Pennant gibt es höchstens einen verzweigenden Knoten. (Formaler Beweis per Induktion über den Suchpfad)

32 32 Ergebnis Satz 2: Die Laufzeit der Min-X-in-Rectangle-Suche in einem Priority Search Pennant P der Höhe h ist durch O(h) beschränkt. Genauer gilt: Es werden höchstens 2 (h – 1) Knoten besucht. Ist P ein balancierter Priority Search Pennant mit n Elementen, so ist die Laufzeit der Min-X-in-Rectangle-Suche durch O(log n) beschränkt. Bemerkung: Priority Search Pennants sind also für die MinXinRectangle-Suche (asymptotisch) worst-case-optimal. (Denn schon die einfache Suche nach einem Schlüssel erfordert ja im worst case O(log n) Schritte.)

33 33 Andere Rechteckanfragen maxXinRectangle Analoger Beweis: O(log n) minYinXRange Ähnliche Argumentation über Verzweigung: O(log n) enumerateRectangle Beweis in Hinze (2001):O(r · (log n – log r +1)) wobei r die Anzahl der Punkte im Rechteck ist Bemerkung: Hier sind Priority Search Trees (McCreight, 1985) besser: O(log n + r)

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


Herunterladen ppt "Priority search queues: Loser trees Advanced Algorithms & Data Structures Lecture Theme 06 Tobias Lauer Summer Semester 2006."

Ähnliche Präsentationen


Google-Anzeigen