1 / 42

מבני נתונים 08

ערמות בינומיות ופיבונצ'י. מבני נתונים 08. ערמות. 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.

sbarrett
Download Presentation

מבני נתונים 08

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ערמות בינומיות ופיבונצ'י מבני נתונים 08

  2. ערמות Operation LinkedList BinaryHeap BinomialHeap FibonacciHeap † RelaxedHeap 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

  3. תזכורת: Heaps עץ בינארי מלא החוק הבסיסי אם צומת B צאצא של צומת A אזי Key(A)≤Key(B) הפעולות הנתמכות Find-min Delete-min Decrease-key Insert Merge

  4. תזכורת: Binomial Heaps תחילה נגדיר Binomial Tree: עץ בינומי מדרגה 0 מכיל צומת יחידה עץ בינומי מדרגה k הוא בעל שורש מדרגה k ילדיו הינם עצים בינומיים מדרגות k-1,k-2,…,0 (בסדר זה) עץ בינומי מדרגה k מכיל 2k צמתים והינו בגובה k

  5. תזכורת: Binomial Heaps איחוד שני עצים בינומיים מסדר k-1 ניצור עץ בינומי מסדר k ע"י תליית אחד העצים כבן השמאלי ביותר של העץ השני

  6. תזכורת: Binomial Heaps הגדרת Binomial Heap סט עצים בינומיים המקיימים את התכונות הבאות כל עץ מקיים את תכונת minimum-heap (כל צאצא גדול מההורה שלו) עבור כל סדר k של עץ בינומי, יש 0 או 1 עצים כאלו ב-heap

  7. תזכורת: Binomial Heaps פעולת Merge של שני עצים מדרגה k function mergeTree(p, q) if p.root <= q.root return p.addSubTree(q) else return q.addSubTree(p) end

  8. תזכורת: 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:

  9. Properties of binomial trees 1) | Bk | = 2k 2) degree(root(Bk))= k 3) depth(Bk)= 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

  10. Binomial heaps (ops cont.) Basic operation is meld(h1,h2): Like addition of binary numbers. B5 B4 B2 B1 h1: B4 B3 B1 B0 + h2: B4 B3 B0 B5 B4 B2

  11. נגדיר עצים בינומיים "שמנים" בצורה הבאה: • עץ בינומי "שמן" מדרגה 0 מכיל צומת אחד בלבד. • עץ בינומי "שמן" מדרגה k ניתן לבנות משלושה עצים בינומיים "שמנים" מדרגה k-1 כאשר נתלה שניים מהם על העץ השלישי. תארו מבנה נתונים אנלוגי לערמה בינומית (binomial heap) המשתמש בעצים "שמנים".

  12. ערמה בינומית "שמנה" מוגדרת בצורה הבאה: בערמה יש n עצים בינומיים "שמנים" כאשר יש לכל היותר 2 עצים מדרגה k, לכל k. שורשי העצים מחוברים ביניהם ברשימה מקושרת. הפעולות מוגדרות בדומה לערמה בינומית רגילה וכאשר יש צורך לעשות merge בין העצים (יש יותר משני עצים מדרגה k) בונים משלושה עצים מדרגה k עץ יחיד מדרגה k+1.

  13. תארו פעולת meld של שתי ערמות בינומיות "שמנות" שהגדרתם בסעיף הקודם. פעולת meld הינה מיזוג בין שתי ערמות בינומיות "שמנות". הדבר מקביל לחיבור שני מספרים בבסיס 3 (כמו שבערמות בינומיות הדבר היה מקביל לחיבור מס' בבסיס 2). נתחיל מדרגה 0 ונוסיף את העצים בערמה אחת לשנייה. אם יש סה"כ 3 או יותר עצים מדרגה 0 בערמה החדשה נמזג שלושה ליצירת עץ יחיד מדרגה 1. כעת נעבור לדרגה הבאה (1) ונמשיך כך...

  14. ערמות פיבונאצ'י Operation LinkedList BinaryHeap BinomialHeap FibonacciHeap † RelaxedHeap 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

  15. Fibonacci heap. Set of heap-ordered trees. Maintain pointer to minimum element. Set of marked nodes. ערימות פיבונאצ'י - מבנה each parent larger than its children roots heap-ordered tree 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39

  16. Fibonacci heap. Set of heap-ordered trees. Maintain pointer to minimum element. Set of marked nodes. ערימות פיבונאצ'י - מבנה find-min takes O(1) time min 17 24 23 7 3 30 26 46 18 52 41 35 Heap H 44 39

  17. 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 24 23 7 3 30 26 46 18 52 41 35 Heap H marked 44 39

  18. Cascading cuts & Successive linking min 7 24 23 17 3 30 26 46 18 52 41 35 39 44 • פעולת delete-min • יודעים מי המינימום בערימה • נתלה את הבנים שלו כעצים בערימה • נבצע successive linking – מכל דרגה עץ יחיד!

  19. Cascading cuts & Successive linking min 7 24 23 17 18 52 41 39 44 30 26 46 35 • פעולת delete-min • יודעים מי המינימום בערימה • נתלה את הבנים שלו כעצים בערימה • נבצע successive linking – מכל דרגה עץ יחיד!

  20. Cascading cuts & Successive linking עלות הפעולה: Amortized O(rank(H)) min 7 52 18 41 24 17 30 39 26 46 23 44 • פעולת delete-min • יודעים מי המינימום בערימה • נתלה את הבנים שלו כעצים בערימה • נבצע successive linking – מכל דרגה עץ יחיד! 35

  21. Cascading cuts & Successive linking min 7 18 38 marked node:one child already cut 24 17 23 21 39 41 26 46 30 52 • פעולת decrease-key • אינטואיציה: • אם חוק הערמה לא מופר, פשוט נשנה את ערך הצומת • אחרת נחתוך את תת העץ של הצומת ונתלה כעץ חדש • בכדי לשמור על עצים שטוחים יחסית, ברגע שחותכים בן שני לצומת מסוים, גם הוא נתלה כעץ חדש 35 88 72

  22. Case 1. [heap order not violated] Decrease key of x. Change heap min pointer (if necessary). Fibonacci Heaps: Decrease Key min 7 18 38 24 17 23 21 39 41 26 46 29 30 52 x decrease-key of x from 46 to 29 35 88 72

  23. Case 1. [heap order not violated] Decrease key of x. Change heap min pointer (if necessary). Fibonacci Heaps: Decrease Key min 7 18 38 24 17 23 21 39 41 26 29 30 52 x decrease-key of x from 46 to 29 35 88 72

  24. 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). Fibonacci Heaps: Decrease Key min 7 18 38 24 17 23 21 39 41 p 26 29 30 52 15 x decrease-key of x from 29 to 15 35 88 72

  25. 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). Fibonacci Heaps: Decrease Key min 7 18 38 24 17 23 21 39 41 p 26 15 30 52 x decrease-key of x from 29 to 15 35 88 72

  26. 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). Fibonacci Heaps: Decrease Key x min 15 7 18 38 72 24 17 23 21 39 41 p 26 30 52 decrease-key of x from 29 to 15 35 88

  27. 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). Fibonacci Heaps: Decrease Key x min 15 7 18 38 72 24 24 17 23 21 39 41 p mark parent 26 30 52 decrease-key of x from 29 to 15 35 88

  28. 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 18 38 72 24 24 17 23 21 39 41 26 30 52 p decrease-key of x from 35 to 5 35 88 5 x

  29. 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 18 38 72 24 24 17 23 21 39 41 26 30 52 p decrease-key of x from 35 to 5 5 88 x

  30. 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 x 15 5 7 18 38 72 24 24 17 23 21 39 41 26 30 52 p decrease-key of x from 35 to 5 88

  31. 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). Fibonacci Heaps: Decrease Key min x 15 5 7 18 38 72 24 24 17 23 21 39 41 second child cut 26 30 52 p decrease-key of x from 35 to 5 88

  32. 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). Fibonacci Heaps: Decrease Key min x p 15 5 26 7 18 38 88 24 24 17 23 21 39 41 72 30 52 decrease-key of x from 35 to 5

  33. 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). Fibonacci Heaps: Decrease Key min x p 15 5 26 7 18 38 88 24 24 17 23 21 39 41 72 p' 30 52 second child cut decrease-key of x from 35 to 5

  34. 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). Fibonacci Heaps: Decrease Key min x p p' p'' 15 5 26 24 7 18 38 88 17 23 21 39 41 72 don't markparent ifit's a root 30 52 decrease-key of x from 35 to 5

  35. ערמות פיבונצ'י • Rank(H)<logФ(n) כמו שראיתם בכיתה:

  36. תרגיל 1 – ערמות פיבונאצ'י 5 7 9 20 25 30 35 • האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? • פתרון • נניח שאנו יודעים לפתור עבור n=k • נפתור עבור n=k+1 • נגדיר x’,y’,z’ כך ש- • נכניס אותם לתוך הערמה • נפעיל extract-min

  37. תרגיל 1 – ערמות פיבונאצ'י 7 20 9 25 30 35 • האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? • פתרון • נניח שאנו יודעים לפתור עבור n=k • נפתור עבור n=k+1 • נגדיר x’,y’,z’ כך ש- • נכניס אותם לתוך הערמה • נפעיל extract-min

  38. תרגיל 1 – ערמות פיבונאצ'י 7 20 9 25 30 • האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? • פתרון • נניח שאנו יודעים לפתור עבור n=k • נפתור עבור n=k+1 • נגדיר x’,y’,z’ כך ש- • נכניס אותם לתוך הערמה • נפעיל extract-min • נמחק את x’ (9) 35

  39. תרגיל 2 • בערמות פיבונצ'י, אנחנו מבצעים cascading cuts בצומת v אם הוא איבד צומת בן מאז הפעם האחרונה שהוא נתלה על צומת אחרת. נניח שנבצע CC רק אם v איבד שני בנים מאז, כיצד משתנה הלמה: • x צומת בערמת פיב', y1,…,yn בנים של x, מסודרים לפי הסדר בו נתלו על x (הכי ישן ועד הכי חדש). אזי rank(yi)≥i-2 לכל i.

  40. תרגיל 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

  41. הסוף

More Related