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
הסוף