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

Post on 05-Apr-2015

109 views 4 download

Tags:

Transcript of Priority search queues: Loser trees Advanced Algorithms & Data Structures Lecture Theme 06 Tobias...

Priority search queues: Loser trees

Advanced Algorithms & Data Structures

Lecture Theme 06

Tobias Lauer

Summer Semester 2006

2

Intro 2

• Recap

• Begriffe: Pennant, Top node

• Linien gestrichelt vs. durchgezogen

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

18, 918

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

15, 516

5, 104

10, 414

20, 39

1, 220

7, 135

Priority search pennant: example

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

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

18, 918

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

15, 516

5, 104

10, 414

20, 39

1, 220

7, 135

Example: delete(20)

8

18, 918

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

15, 516

5, 104

10, 414

20, 39

1, 220

7, 135

Example: delete(20)

S

N

E

9

18, 920

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

15, 516

5, 104

10, 414

20, 39

1, 218

7, 135

Example: delete(20)

S

N

E

10

18, 920

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

15, 516

5, 104

10, 414

20, 39

1, 218

7, 135

Example: delete(20)

S

N

E

11

15, 520

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

18, 916

5, 104

10, 414

20, 39

1, 218

7, 135

Example: delete(20)

S

N

E

12

10, 420

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

18, 916

5, 104

15, 514

20, 39

1, 218

7, 135

Example: delete(20)

S

N E

13

20, 320

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

18, 916

5, 104

15, 514

10, 49

1, 218

7, 135

Example: delete(20)

S

N E

14

4, 153

13, 610

16, 1115

3, 71

9, 127

14, 813

18, 916

5, 104

15, 514

10, 49

1, 218

7, 135

Example: delete(20)

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

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

5, 64

7, 39

Rotation

t1 t2

t3

5, 64

7, 39

t1

t2 t3

Bad news: Rotation can destroy the semi-heap property!

Good news: We can repair it!

18

2, 64

7, 39

1st case: xA ≤ sA and xB ≤ sB

t1 t2

t3

2, 64

7, 39

t1

t2 t3

A

B

No repair needed!

19

5, 64

7, 39

2nd case: xA ≤ sA and xB > sB

t1 t2

t3

7, 34

5, 69

t1

t2 t3

A

B

Swap!

20

3rd case: xA > sA and xB ≤ sB

2, 64

10, 39

t1 t2

t3

2, 64

10, 39

t1

t2 t3

2, 34

10, 69

t1 t2

t3

2, 34

10, 69

t1

t2 t3

Case 3ayA ≤ yB

Case 3byA > yB

A

B

A

B

No repair needed!

21

4th case: xA > sA and xB > sB

7, 64

10, 39

t1 t2

t3

10, 34

7, 69

t1

t2 t3

7, 34

10, 69

t1 t2

t3

7, 34

10, 69

t1

t2 t3

A

B

A

B

Case 4ayA ≤ yB

Case 4byA > yB

No repair needed!

Swap!

22

Rotations

• Right rotation: Condition for swapping: xB > sB and yA ≤ yB

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

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

xleft xright

ytop

24

MinXinRectangle

Sei Min ein Zeiger auf den besten bisher gefundenen Knoten (zu Beginn setze Min auf einen Dummyknoten D mit xD = xmax).

Beim Besuch von Knoten N:

1) Falls N der Suchbedingung genügt (x ≤ xN < xMin AND yN ≤ 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: sN ≥ x UND

b) gültige y-Werte enthalten kann:

yN ≤ y ODER N stammt aus seinem rechten Teilbaum (xN > sN)

3) Besuche den rechten Teilbaum, falls nötig, d.h. falls dieser existiert UND

d) gültige x-Werte enthalten kann: sN < xMin UND

e) gültige y-Werte enthalten kann:

yN ≤ y ODER N stammt aus seinem linken Teilbaum (xN ≤ sN)

Depth-first search (in preorder) with pruning

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

Analyse

Beobachtung 1: Wenn während minXinRectangle (x, x’, y) im linken Teilbaum eines potentiell verzweigenden Knotens N ein neuer Knoten L mit yL ≤ y und x ≤ xL ≤ xMin gefunden wird, so ist N nicht tatsächlich verzweigend.

Beobachtung 2: Für den Splitwert sN eines Knotens N, der bei einer Suchanfrage (x, x’, y) potentiell verzweigend ist, gilt beim ersten Besuch von N: x ≤ sN < kMin.

[Die Aussage ist die Zusammenfassung von (a) und (d).]

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 ≤ sN < xMin (s. Beobachtung 2).Annahme: R im rechten Teilbaum ist potentiell verzweigend,d.h. x ≤ sR < xMin

xN

sN

xR

sR

sN sR

x xMin

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 (xR ≤ sR)

xN

sN

xR

sR

sN sR

x xMin

xR

29

Beweis

xN

sN

xR

sR

sN sR

x xMin

xR

xW

xW

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 (xR > sR)

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 ≤ sN < xMin.Sei L im linken Teilbaum potentiell verzweigend,d.h. x ≤ sL < xMin

xN

sN

xL

sL

sNsLxL

x xMin

xW

xW

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

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

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

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.