Binomial and Fibonacci Heaps

Download Report

Transcript Binomial and Fibonacci Heaps

‫מבני נתונים ‪08‬‬
‫ערמות בינומיות‬
‫ופיבונצ'י‬
‫ערמות‬
Operation
Linked
List
Binary
Heap
make-heap
1
1
1
1
1
is-empty
1
1
1
1
1
insert
1
log n
log n
1
1
delete-min
n
log n
log n
log n
log n
decrease-key
n
log n
log n
1
1
delete
n
log n
log n
log n
log n
union
1
n
log n
1
1
find-min
n
1
log n
1
1
n = number of elements in priority queue
Binomial Fibonacci
Heap
Heap †
†
amortized
Relaxed
Heap
Heaps :‫תזכורת‬
‫עץ בינארי מלא‬
‫החוק הבסיסי‬
Key(A)≤Key(B) ‫ אזי‬A ‫ צאצא של צומת‬B ‫אם צומת‬


‫הפעולות הנתמכות‬
Find-min
Delete-min
Decrease-key
Insert
Merge







‫תזכורת‪Binomial Heaps :‬‬
‫‪‬‬
‫תחילה נגדיר ‪:Binomial Tree‬‬
‫‪‬‬
‫‪‬‬
‫עץ בינומי מדרגה ‪ 0‬מכיל צומת יחידה‬
‫עץ בינומי מדרגה ‪ k‬הוא‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫בעל שורש מדרגה ‪k‬‬
‫ילדיו הינם עצים בינומיים מדרגות ‪) k-1,k-2,…,0‬בסדר זה(‬
‫עץ בינומי מדרגה ‪ k‬מכיל ‪ 2k‬צמתים והינו בגובה ‪k‬‬
‫תזכורת‪Binomial Heaps :‬‬
‫‪‬‬
‫איחוד שני עצים בינומיים מסדר ‪k-1‬‬
‫‪‬‬
‫ניצור עץ בינומי מסדר ‪ k‬ע"י תליית אחד העצים כבן‬
‫השמאלי ביותר של העץ השני‬
‫תזכורת‪Binomial Heaps :‬‬
‫‪‬‬
‫הגדרת ‪Binomial Heap‬‬
‫‪‬‬
‫סט עצים בינומיים המקיימים את התכונות הבאות‬
‫‪‬‬
‫‪‬‬
‫כל עץ מקיים את תכונת ‪) minimum-heap‬כל צאצא גדול מההורה‬
‫שלו(‬
‫עבור כל סדר ‪ k‬של עץ בינומי‪ ,‬יש ‪ 0‬או ‪ 1‬עצים כאלו ב‪heap-‬‬
Binomial Heaps :‫תזכורת‬
k ‫ של שני עצים מדרגה‬Merge ‫פעולת‬
function mergeTree(p, q)
if p.root <= q.root
return p.addSubTree(q)
else
return q.addSubTree(p)
end

‫תזכורת‪Binomial Heaps :‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫פעולת ‪Insert‬‬
‫‪ ‬ניצור ‪ heap‬חדש המכיל את האבר החדש‪ ,‬ונבצע ‪ merge‬בין שני ה‪heaps-‬‬
‫פעולת ‪minimum‬‬
‫‪ ‬עלינו לחפש את הערך המינימלי מבין שורשי העצים ב‪heap‬‬
‫פעולת ‪delete-min‬‬
‫‪ ‬מצא את האבר ומחק אותו‬
‫‪ ‬הפוך את בניו ל‪ binomial heap-‬ומזג את שני ה‪heaps-‬‬
‫פעולת ‪Decrease-min‬‬
‫‪ ‬בדומה לפעולות ב‪ heap-‬רגיל‬
‫פעולת ‪Delete‬‬
‫‪ ‬שנה את הערך ל∞‪ -‬ובצע ‪delete-min‬‬
‫הדגמה באנימציה של ‪http://www.cse.yorku.ca/~aaw/Sotirios/BinomialHeap.html :Binomial Heaps‬‬
Properties of binomial trees
1) | Bk | = 2k
2) degree(root(Bk))
3) depth(Bk)
=
=
k
k
==> The degree and depth of a binomial tree with at most n
nodes is at most log(n).
Define the rank of Bk to be k
Binomial heaps (ops cont.)
Basic operation is meld(h1,h2):
Like addition of binary numbers.
B5
B4
B2 B1
h1:
B4
B3
h2:
B4
B3
B5
B4
B1
B0
B0
B2
+
‫נגדיר עצים בינומיים "שמנים" בצורה הבאה‪:‬‬
‫‪ ‬עץ בינומי "שמן" מדרגה ‪ 0‬מכיל צומת אחד בלבד‪.‬‬
‫‪ ‬עץ בינומי "שמן" מדרגה ‪ k‬ניתן לבנות משלושה עצים‬
‫בינומיים "שמנים" מדרגה ‪ k-1‬כאשר נתלה שניים מהם על‬
‫העץ השלישי‪.‬‬
‫תארו מבנה נתונים אנלוגי לערמה בינומית (‪)binomial heap‬‬
‫המשתמש בעצים "שמנים"‪.‬‬
‫ערמה בינומית "שמנה" מוגדרת בצורה הבאה‪:‬‬
‫בערמה יש ‪ n‬עצים בינומיים "שמנים" כאשר יש‬
‫לכל היותר ‪ 2‬עצים מדרגה ‪ ,k‬לכל ‪ .k‬שורשי העצים‬
‫מחוברים ביניהם ברשימה מקושרת‪ .‬הפעולות‬
‫מוגדרות בדומה לערמה בינומית רגילה וכאשר יש‬
‫צורך לעשות ‪ merge‬בין העצים )יש יותר משני‬
‫עצים מדרגה ‪ (k‬בונים משלושה עצים מדרגה ‪ k‬עץ‬
‫יחיד מדרגה ‪.k+1‬‬
‫תארו פעולת ‪ meld‬של שתי ערמות בינומיות‬
‫"שמנות" שהגדרתם בסעיף הקודם‪.‬‬
‫פעולת ‪ meld‬הינה מיזוג בין שתי ערמות בינומיות "שמנות"‪.‬‬
‫הדבר מקביל לחיבור שני מספרים בבסיס ‪( 3‬כמו שבערמות‬
‫בינומיות הדבר היה מקביל לחיבור מס' בבסיס ‪ .)2‬נתחיל‬
‫מדרגה ‪ 0‬ונוסיף את העצים בערמה אחת לשנייה‪ .‬אם יש סה"כ‬
‫‪ 3‬או יותר עצים מדרגה ‪ 0‬בערמה החדשה נמזג שלושה ליצירת‬
‫עץ יחיד מדרגה ‪ .1‬כעת נעבור לדרגה הבאה (‪ )1‬ונמשיך כך‪...‬‬
‫ערמות פיבונאצ'י‬
Operation
Linked
List
Binary
Heap
Binomial
Heap
Fibonacci
Heap †
Relaxed
Heap
make-heap
1
1
1
1
1
is-empty
1
1
1
1
1
insert
1
log n
log n
1
1
delete-min
n
log n
log n
log n
log n
decrease-key
n
log n
log n
1
1
delete
n
log n
log n
log n
log n
union
1
n
log n
1
1
find-min
n
1
log n
1
1
n = number of elements in priority queue
† amortized
‫ מבנה‬- ‫ערימות פיבונאצ'י‬

Fibonacci heap.



each parent larger than its children
Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
roots
17
30
Heap
16 H
24
26
35
46
heap-ordered tree
23
7
3
18
39
52
41
44
‫ מבנה‬- ‫ערימות פיבונאצ'י‬

Fibonacci heap.



Set of heap-ordered trees.
Maintain pointer to minimum element.
find-min takes O(1) time
Set of marked nodes.
min
17
30
Heap
17 H
24
26
35
46
23
7
3
18
39
52
41
44
‫ מבנה‬- ‫ערימות פיבונאצ'י‬

Fibonacci heap.



Set of heap-ordered trees.
Maintain pointer to minimum element.
Set of marked nodes.
use to keep heaps flat (stay tuned)
min
17
30
Heap
18 H
24
26
35
23
7
3
46
18
marked
39
52
41
44
‫‪Cascading cuts & Successive linking‬‬
‫‪‬‬
‫פעולת ‪delete-min‬‬
‫‪ ‬יודעים מי המינימום בערימה‬
‫‪ ‬נתלה את הבנים שלו כעצים בערימה‬
‫‪ ‬נבצע ‪ – successive linking‬מכל דרגה עץ יחיד!‬
‫‪min‬‬
‫‪3‬‬
‫‪41‬‬
‫‪44‬‬
‫‪52‬‬
‫‪17‬‬
‫‪18‬‬
‫‪39‬‬
‫‪23‬‬
‫‪24‬‬
‫‪46‬‬
‫‪7‬‬
‫‪26‬‬
‫‪35‬‬
‫‪30‬‬
‫‪Cascading cuts & Successive linking‬‬
‫‪‬‬
‫פעולת ‪delete-min‬‬
‫‪ ‬יודעים מי המינימום בערימה‬
‫‪ ‬נתלה את הבנים שלו כעצים בערימה‬
‫‪ ‬נבצע ‪ – successive linking‬מכל דרגה עץ יחיד!‬
‫‪min‬‬
‫‪41‬‬
‫‪44‬‬
‫‪52‬‬
‫‪18‬‬
‫‪39‬‬
‫‪17‬‬
‫‪23‬‬
‫‪24‬‬
‫‪46‬‬
‫‪7‬‬
‫‪26‬‬
‫‪35‬‬
‫‪30‬‬
‫‪Cascading cuts & Successive linking‬‬
‫‪‬‬
‫פעולת ‪delete-min‬‬
‫‪ ‬יודעים מי המינימום בערימה‬
‫‪ ‬נתלה את הבנים שלו כעצים בערימה‬
‫‪ ‬נבצע ‪ – successive linking‬מכל דרגה עץ יחיד!‬
‫‪18‬‬
‫‪39‬‬
‫‪52‬‬
‫‪41‬‬
‫‪44‬‬
‫‪7‬‬
‫‪30‬‬
‫עלות הפעולה‪:‬‬
‫))‪Amortized O(rank(H‬‬
‫‪min‬‬
‫‪17‬‬
‫‪24‬‬
‫‪23‬‬
‫‪46‬‬
‫‪26‬‬
‫‪35‬‬
‫‪Cascading cuts & Successive linking‬‬
‫‪‬‬
‫פעולת ‪decrease-key‬‬
‫‪‬‬
‫אינטואיציה‪:‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫אם חוק הערמה לא מופר‪ ,‬פשוט נשנה את ערך הצומת‬
‫אחרת נחתוך את תת העץ של הצומת ונתלה כעץ חדש‬
‫בכדי לשמור על עצים שטוחים יחסית‪ ,‬ברגע שחותכים בן שני לצומת מסוים‪ ,‬גם הוא נתלה כעץ‬
‫חדש‬
‫‪min‬‬
‫‪38‬‬
‫‪18‬‬
‫‪41‬‬
‫‪39‬‬
‫‪7‬‬
‫‪21‬‬
‫‪52‬‬
‫‪23‬‬
‫‪marked node:‬‬
‫‪one child already cut‬‬
‫‪17‬‬
‫‪24‬‬
‫‪30‬‬
‫‪46‬‬
‫‪26‬‬
‫‪72‬‬
‫‪88‬‬
‫‪35‬‬
Fibonacci Heaps: Decrease Key

Case 1. [heap order not violated]


Decrease key of x.
Change heap min pointer (if necessary).
min
7
26
24
17
46
29
30
23
21
18
38
39
41
52
x
23
35
88
72
decrease-key of x from 46 to 29
Fibonacci Heaps: Decrease Key

Case 1. [heap order not violated]


Decrease key of x.
Change heap min pointer (if necessary).
min
7
26
24
17
29
30
23
21
18
38
39
41
52
x
24
35
88
72
decrease-key of x from 46 to 29
Fibonacci Heaps: Decrease Key

Case 2a. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
7
24
17
23
21
18
38
39
41
p
26
29
15
30
52
x
25
35
88
72
decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key

Case 2a. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
7
24
17
23
21
18
38
39
41
p
26
15
30
52
x
26
35
88
72
decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key

Case 2a. [heap order violated]



x
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
7
72
24
17
23
21
18
38
39
41
p
26
27
35
88
30
52
decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key

Case 2a. [heap order violated]



x
Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
7
72
24
26
35
23
21
38
39
41
p
mark parent
28
17
18
88
30
52
decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
7
72
24
p
29
x
35
5
26
88
17
30
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
7
72
24
p
30
x
5
26
88
17
30
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
x
5
7
72
24
p
31
26
88
17
30
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
72
x
5
7
second child cut
p
32
26
88
24
17
30
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
72
x
p
5
26
88
7
24
17
30
33
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
72
x
p
5
26
88
7
p' 24
second child cut
34
17
30
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key

Case 2b. [heap order violated]



Decrease key of x.
Cut tree rooted at x, meld into root list, and unmark.
If parent p of x is unmarked (hasn't yet lost a child), mark it;
Otherwise, cut p, meld into root list, and unmark
(and do so recursively for all ancestors that lose a second child).
min
15
72
x
p
p'
p''
5
26
24
7
88
don't mark
parent if
it's a root
17
30
35
23
21
18
38
39
41
52
decrease-key of x from 35 to 5
‫ערמות פיבונצ'י‬
‫‪‬‬
‫כמו שראיתם בכיתה‪:‬‬
‫)‪Rank(H)<logФ(n‬‬
‫תרגיל ‪ – 1‬ערמות פיבונאצ'י‬
‫‪‬‬
‫‪‬‬
‫האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק ‪?n‬‬
‫פתרון‬
‫‪‬‬
‫‪‬‬
‫נניח שאנו יודעים לפתור עבור ‪n=k‬‬
‫נפתור עבור ‪n=k+1‬‬
‫] ‪key[ z ']  k ey[ y ']  key[ x ']  min[ H‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫נגדיר ’‪ x’,y’,z‬כך ש‪-‬‬
‫נכניס אותם לתוך הערמה‬
‫נפעיל ‪extract-min‬‬
‫‪20‬‬
‫‪25‬‬
‫‪30‬‬
‫‪35‬‬
‫‪9‬‬
‫‪7‬‬
‫‪5‬‬
‫תרגיל ‪ – 1‬ערמות פיבונאצ'י‬
‫‪‬‬
‫‪‬‬
‫האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק ‪?n‬‬
‫פתרון‬
‫‪‬‬
‫‪‬‬
‫נניח שאנו יודעים לפתור עבור ‪n=k‬‬
‫נפתור עבור ‪n=k+1‬‬
‫] ‪key[ z ']  k ey[ y ']  key[ x ']  min[ H‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫נגדיר ’‪ x’,y’,z‬כך ש‪-‬‬
‫נכניס אותם לתוך הערמה‬
‫נפעיל ‪extract-min‬‬
‫‪20‬‬
‫‪25‬‬
‫‪30‬‬
‫‪35‬‬
‫‪7‬‬
‫‪9‬‬
‫תרגיל ‪ – 1‬ערמות פיבונאצ'י‬
‫‪‬‬
‫‪‬‬
‫האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק ‪?n‬‬
‫פתרון‬
‫‪‬‬
‫‪‬‬
‫נניח שאנו יודעים לפתור עבור ‪n=k‬‬
‫נפתור עבור ‪n=k+1‬‬
‫] ‪key[ z ']  k ey[ y ']  key[ x ']  min[ H‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫‪‬‬
‫נגדיר ’‪ x’,y’,z‬כך ש‪-‬‬
‫נכניס אותם לתוך הערמה‬
‫נפעיל ‪extract-min‬‬
‫נמחק את ’‪(9) x‬‬
‫‪7‬‬
‫‪20‬‬
‫‪9‬‬
‫‪25‬‬
‫‪30‬‬
‫‪35‬‬
‫תרגיל ‪2‬‬
‫‪‬‬
‫בערמות פיבונצ'י‪ ,‬אנחנו מבצעים ‪cascading cuts‬‬
‫בצומת ‪ v‬אם הוא איבד צומת בן מאז הפעם האחרונה‬
‫שהוא נתלה על צומת אחרת‪ .‬נניח שנבצע ‪ CC‬רק אם‬
‫‪ v‬איבד שני בנים מאז‪ ,‬כיצד משתנה הלמה‪:‬‬
‫‪‬‬
‫‪ x‬צומת בערמת פיב'‪ y1,…,yn ,‬בנים של ‪ ,x‬מסודרים לפי הסדר בו‬
‫נתלו על ‪) x‬הכי ישן ועד הכי חדש(‪ .‬אזי ‪ rank(yi)≥i-2‬לכל ‪.i‬‬
2 ‫תרגיל‬

Answer




rank(yi)≥i-3
Since yi had the same rank as x when it became a
child of x
x must have had at least i-1 children at that time,
so yi had at least i-1 rank.
It could have lost at most two children since then,
therefore rank at least i-3
‫הסוף‬