ערמות make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary Heap log n n Binomial Heap Fibonacci Heap † Relaxed Heap Linked.

Slides:



Advertisements
Similar presentations
תרגול 9 Heaps Lempel-Ziv. Heaps Heap A binary heap can be considered as a complete binary tree, (the last level is full from the left to a certain point).
Advertisements

Single Source Shortest Paths
Dynamic Graph Algorithms - I
Priority Queues  MakeQueuecreate new empty queue  Insert(Q,k,p)insert key k with priority p  Delete(Q,k)delete key k (given a pointer)  DeleteMin(Q)delete.
By Amber McKenzie and Laura Boccanfuso. Dijkstra’s Algorithm Question: How do you know that Dijkstra’s algorithm finds the shortest path and is optimal.
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
Heaps Chapter 6 in CLRS. Motivation Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
ערמות ; מבני נתונים 09 מבוסס על מצגות של ליאור שפירא, חיים קפלן, דני פלדמן וחברים.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Dr. Andrew Wallace PhD BEng(hons) EurIng
Prim’s Algorithm and an MST Speed-Up
עצים בינאריים - תזכורת דרגת צומת שורש עלה צומת פנימי מרחק בין 2 צמתים
1 Heaps Chapter 6 in CLRS. 2 Motivation Router: Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees.
תרגול 4 21/3/2007 מבני נתונים 07b ליאור שפירא. תזכורת – B-trees  לכל צומת x יש השדות הבאים n[x] מס ' מפתחות ב -x המפתחות עצמם בסדר לא יורד כל צומת פנימי.
Chapter 9: Graphs Spanning Trees Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Eddie Bortnikov/Aran Bergman, Principles of Reliable Distributed Systems, Technion EE, Spring Principles of Reliable Distributed Systems Recitation.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 Binomial heaps, Fibonacci heaps, and applications.
9/10/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Adam Smith Algorithm Design and Analysis L ECTURE 8 Greedy Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Binomial heaps, Fibonacci heaps, and applications
Data Structures Haim Kaplan & Uri Zwick December 2013 Binary Heaps 1.
Change Keys in heaps Fibonacci heap Zhao Xiaobin.
Data Structures Hanoch Levi and Uri Zwick March 2011 Lecture 3 Dynamic Sets / Dictionaries Binary Search Trees.
1 Fibonacci heaps: idea List of multiway trees which are all heap-ordered. Definition: A tree is called heap-ordered if the key of each node is greater.
Fibonacci Heaps. Fibonacci Binary insert O(1) O(log(n)) find O(1) N/A union O(1) N/A minimum O(1) O(1) decrease key O(1) O(log(n)) delete O(log(n) O(log(n))
מספרים אקראיים ניתן לייצר מספרים אקראיים ע"י הפונקציה int rand(void);
Minimum Spanning Trees
Tirgul 12 Trees 1.
ליאור שפירא, חיים קפלן וחברים
Single-Source Shortest Path
Graph Algorithms BFS, DFS, Dijkstra’s.
Binomial heaps, Fibonacci heaps, and applications
Heaps Binomial Heaps Lazy Binomial Heaps 1.
Binomial heaps, Fibonacci heaps, and applications
Binomial Tree Adapted from: Kevin Wayne Bk-1 B0 Bk
Priority Queues MakeQueue create new empty queue
Fibonacci Heaps Remove arbitrary is useful in (for example) correspondence structures and may also be used to do an increase key in a min structure (remove.
Minimum Spanning Trees
תירגול 14: מבני נתונים דינאמיים
Marina Kogan Sadetsky –
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
SINGLE-SOURCE SHORTEST PATHS
ערמות בינומיות ופיבונצ'י
Data Structures – LECTURE 13 Minumum spanning trees
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 12 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Binary and Binomial Heaps
Binomial heaps, Fibonacci heaps, and applications
Binomial heaps, Fibonacci heaps, and applications
Weighted Graphs & Shortest Paths
Autumn 2016 Lecture 10 Minimum Spanning Trees
Brad Karp UCL Computer Science
CSC 380: Design and Analysis of Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Implementation of Dijkstra’s Algorithm
Binomial heaps, Fibonacci heaps, and applications
Priority Queues Supports the following operations. Insert element x.
Winter 2019 Lecture 10 Minimum Spanning Trees
Fundamental Structures of Computer Science II
Chapter 9 Graph algorithms
Chapter 9: Graphs Spanning Trees
Presentation transcript:

ערמות make-heap Operation insert find-min delete-min union decrease-key delete 1 Binary Heap log n n Binomial Heap Fibonacci Heap † Relaxed Heap Linked List is-empty † amortized n = number of elements in priority queue 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 סט עצים בינומיים המקיימים את התכונות הבאות כל עץ מקיים את תכונת 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

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

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

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

תארו פעולת meld של שתי ערמות בינומיות "שמנות" שהגדרתם בסעיף הקודם.

למדנו כי מיון n אברים הוא Ω(n∙logn), סתירה הראו שאין מימוש heap שתומך בפעולות extract-min ו-insert בזמן o(logn) תחת הנחות מודל ההשוואות. פתרון נבצע n פעולות הכנסה בזמן n∙o(logn) נבצע n פעולות הוצאת מינימום בזמן n∙o(logn) סה"כ o(n∙logn) למדנו כי מיון n אברים הוא Ω(n∙logn), סתירה 15

תרגיל 3 – Median Heap ממשו מבנה נתונים התומך בפעולות insert בזמן O(logn) extract-median בזמן O(logn) find-median בזמן O(1) 2 4 5 7 8 12 14 15 20 16

תרגיל 3 - פתרון + Max-heap Min-heap האברים הגדולים (מהחציון) לסובב את מבני הנתונים שייראה כמו שני משולשים שהקדקודים נוגעים... האברים הגדולים (מהחציון) האברים הקטנים (עד החציון) 17

תרגיל 3 - פתרון + נשתמש ב-max-heap ו-min-heap n/2 הערכים הגדולים ביותר יישמרו ב-max-heap השאר יישמרו ב-min-heap החציון תמיד נמצא בשורש של אחד מהם לסובב את מבני הנתונים שייראה כמו שני משולשים שהקדקודים נוגעים... Max-Heap Min-Heap 2 4 5 7 8 12 14 15 20 18

תרגיל 3 - פתרון O(1) O(logn) O(logn) Find-median Insert(x) If (size(minheap)>size(maxheap)) return getmin(minheap) Else return getmax(maxheap) Insert(x) If (x<getmin(minheap)) Insert(maxheap,x) Insert(minheap,x) If (abs(size(minheap)-size(maxheap))>1) Balance heaps (move root from bigger heap to smaller heap) Extract-Median Extract median from the max-heap or min-heap… O(1) O(logn) O(logn) 19

תארו מבנה נתונים התומך בפעולות הבאות על קבוצה S מתחום סדור מלא Median(S): מחזיר את החציון ב-S (אם מס' אברים זוגי – חציון תחתון) Min(S) : מחזיר את האבר הקטן ביותר ב-S Max(S) : מחזיר את האבר הגדול ביותר ב-S Insert(x,S) : מוסיף אבר x ל-S Delete(x,S) מניחים ש-x נמצא לפני הפעולה ב-S. הפעולה מוציאה את x מ-S על הפעולות median,min,max לקחת O(1) זמן במקרה הגרוע, ועל הפעולות insert ו-delete לקחת O(logn) זמן במקרה הגרוע.

שומרים median-heap כמו שלמדנו בכתה שומרים median-heap כמו שלמדנו בכתה. כלומר, שתי ערימות: אחת ערימת-מקסימום ואחת ערימת מינימום, כאשר כל איבר בערימת המינימום גדול מכל איבר בערימת המקסימום, ובנוסף הגודל של ערימת המקסימום שווה או קטן בבדיוק 1 מהגודל של ערימת המינימום. בפעולת insert או delete מכניסים/מוציאים את האיבר מ-/ל- ערימה המתאימה, ואם צריך מעבירים איבר מערימה אחת לאחרת כדי לאזן את הגדלים. ה-median תמיד יהיה האיבר המינימלי בערימת המינימום. בנוסף, מחזיקים ערימת מינימום וערימת מקסימום שמחזיקות את כל האיברים, כדי לענות על שאילתות ה-min וה-max

Motivation Dijkstra’s algorithm for single source shortest path Prim’s algorithm for minimum spanning trees

Motivation Want to find the shortest route from New York to San Francisco Model the road-map with a graph

A Graph G=(V,E) V is a set of vertices E is a set of edges (pairs of vertices)

Model driving distances by weights on the edges 2 19 9 1 5 13 17 10 14 8 21 V is a set of vertices E is a set of edges (pairs of vertices)

Source and destination 2 19 9 1 5 13 17 10 14 8 21 V is a set of vertices E is a set of edges (pairs of vertices)

Dijkstra’s algorithm Assume all weights are non-negative Finds the shortest path from some fixed vertex s to every other vertex

Example s4 8 15 s3 2 10 s 3 4 s2 5 1 s1

Example Maintain an upper bound d(v) on the shortest path to v s4 ∞ ∞ 8 15 s3 2 10 s 3 4 s2 ∞ 5 1 s1 ∞

Maintain an upper bound d(v) on the shortest path to v ∞ Maintain an upper bound d(v) on the shortest path to v ∞ 8 15 s3 2 A node is either scanned (in S) or labeled (in Q) 10 s 3 4 s2 ∞ 5 1 s1 Initially S =  and Q = V ∞

Pick a vertex v in Q with minimum d(v) and add it to S Initially S =  and Q = V s4 ∞ Pick a vertex v in Q with minimum d(v) and add it to S ∞ 8 15 s3 2 10 s 3 4 s2 ∞ 5 1 s1 ∞

Pick a vertex v in Q with minimum d(v) and add it to S Initially S =  and Q = V s4 ∞ Pick a vertex v in Q with minimum d(v) and add it to S ∞ 8 15 s3 2 w 10 15 s 3 4 s2 v ∞ 5 1 s1 ∞ For every edge (v,w) where w in Q relax(v,w)

If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(v) ← w Relax(v,w) If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(v) ← w s4 ∞ ∞ 8 15 s3 2 w 10 15 s 3 4 s2 v ∞ 5 1 s1 ∞ For every edge (v,w) where w in Q relax(v,w)

S = {s} s4 ∞ ∞ 8 15 s3 2 10 Relax(s,s4) s 3 4 s2 ∞ 5 1 s1 ∞

S = {s} s4 15 ∞ 8 15 s3 2 10 Relax(s,s4) s 3 4 s2 ∞ 5 1 s1 ∞

S = {s} Relax(s,s4) Relax(s,s3) s4 15 ∞ s3 s2 s ∞ s1 ∞ 8 15 2 10 3 4 5 5 1 s1 ∞

S = {s} Relax(s,s4) Relax(s,s3) s4 15 10 s3 s2 s ∞ s1 ∞ 8 15 2 10 3 4 5 1 s1 ∞

S = {s} Relax(s,s4) Relax(s,s3) Relax(s,s1) s4 15 10 s3 s2 s ∞ s1 ∞ 8 5 1 s1 Relax(s,s1) ∞

S = {s} Relax(s,s4) Relax(s,s3) Relax(s,s1) s4 15 10 s3 s2 s ∞ s1 5 8 5 1 s1 Relax(s,s1) 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 10 8 15 s3 2 10 s 3 4 s2 ∞ 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 10 8 15 s3 2 10 s 3 4 s2 ∞ 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 10 8 15 s3 2 10 Relax(s1,s3) s 3 4 s2 ∞ 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 Relax(s1,s3) s 3 4 s2 ∞ 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 Relax(s1,s3) s 3 4 s2 Relax(s1,s2) ∞ 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 Relax(s1,s3) s 3 4 s2 Relax(s1,s2) 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 9 8 15 s3 2 10 Relax(s2,s3) s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 8 8 15 s3 2 10 Relax(s2,s3) s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 8 8 15 s3 2 10 s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2,s3} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 8 8 15 s3 2 10 s 3 4 s2 6 5 1 s1 5

Pick a vertex v in Q with minimum d(v) and add it to S S = {s,s1,s2,s3,s4} s4 15 Pick a vertex v in Q with minimum d(v) and add it to S 8 8 15 s3 2 10 s 3 4 s2 6 5 1 s1 5

When Q =  then the d() values are the distances from s S = {s,s1,s2,s3,s4} s4 15 When Q =  then the d() values are the distances from s 8 8 15 s3 2 10 The π function gives the shortest path tree s 3 4 s2 6 5 1 s1 5

When Q =  then the d() values are the distances from s S = {s,s1,s2,s3,s4} s4 15 When Q =  then the d() values are the distances from s 8 8 15 s3 2 10 The π function gives the shortest path tree s 3 4 s2 6 5 1 s1 5

Implementation of Dijkstra’s algorithm We need to find efficiently the vertex with minimum d() in Q We need to update d() values of vertices in Q

Required ADT Maintain items with keys subject to Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ)

Required ADT Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q)

How many times we do these operations ? Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) n = |V| n n m = |E|

Do we know an algorithm for this ADT ? Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) Yes! Use binary search trees, we had insert and delete and also min in the dictionary data type. However Heap is better W.C.