1 / 76

מבני נתונים 08b

מבני נתונים 08b. ערמות;. מבוסס על מצגות של ליאור שפירא, חיים קפלן וחברים. תזכורת: Heaps. עץ בינארי מלא החוק הבסיסי אם צומת B צאצא של צומת A אזי Key(A)≤Key(B) הפעולות הנתמכות Find-min Delete-min Decrease-key Insert Merge. תזכורת: Heaps. הוספת צומת (עבור max-heap ). 15.

llaw
Download Presentation

מבני נתונים 08b

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. מבני נתונים 08b ערמות; מבוסס על מצגות של ליאור שפירא, חיים קפלן וחברים

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

  3. תזכורת: Heaps הוספת צומת (עבור max-heap) 15 מחיקת השורש 15 4

  4. תרגיל 1 • בהינתן מערך באורך n, נרצה ליצור min heap ע"י הכנסה סדרתית של ערכי המערך. הראו סדרת הפעולות לוקחת Ω(nlogn) במקרה הכי גרוע (worst case) • פתרון • נצטרך להראות דוגמה של סדרת ההכנסות שלוקחת Ω(nlogn) פעולות • נחפש סדרה ש"תקשה" כמה שיותר על ה-heap

  5. תרגיל 1 … n n-1 n-2 n-3 … 2 1 n n n-1 n-1 n n-2 n-1 n-2 n n n-1

  6. תרגיל 1 • כל ערך שנוסיף צריך לבעבע לראש העץ • n/2 ההכנסות האחרונות לוקחות לפחות log(n/2) כל אחת • מסקנה: W.C = Ω(nlogn)

  7. תרגיל 2 • בהינתן heap שתומך בפעולות extract-min ו-insert בזמן f(n) amortized, הראו שניתן למיין מערך מגודל n בזמן O(n∙f(n)) • פתרון • נבצע n פעולות הכנסה בזמן O(n∙f(n)) • מבצע n פעולות הוצאת מינימום בזמן O(n∙f(n)) • סה"כ O(n∙f(n)) • אלגוריתם מיון זה נקרא heap-sort • בשיעור תלמדו כי מיון n אברים הוא Ω(n∙logn)

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

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

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

  11. תרגיל 3 - פתרון • Find-median • If (size(minheap)>size(maxheap)) • return getmin(minheap) • Else • return getmax(maxheap) • Insert(x) • If (x<getmin(minheap)) • Insert(maxheap,x) • Else • 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)

  12. Heaps Chapter 6 in CLRS

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

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

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

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

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

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

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

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

  21. s4 ∞ 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 s2 3 4 ∞ 5 0 1 s1 Initially S =  and Q = V ∞

  22. 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 s2 3 4 ∞ 5 0 1 s1 ∞

  23. 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 s s2 3 4 15 ∞ v 5 0 1 s1 ∞ For every edge (v,w) where w in Q relax(v,w)

  24. 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 s s2 3 4 15 ∞ v 5 0 1 s1 ∞ For every edge (v,w) where w in Q relax(v,w)

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

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

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

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

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

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

  31. 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 s2 3 4 ∞ 5 0 1 s1 5

  32. 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 s2 3 4 ∞ 5 0 1 s1 5

  33. 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 s2 3 4 ∞ 5 0 1 s1 5

  34. 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 s2 3 4 ∞ 5 0 1 s1 5

  35. 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 s2 3 4 Relax(s1,s2) ∞ 5 0 1 s1 5

  36. 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 s2 3 4 Relax(s1,s2) 6 5 0 1 s1 5

  37. 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 s2 3 4 6 5 0 1 s1 5

  38. 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 s2 3 4 6 5 0 1 s1 5

  39. 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 s2 3 4 6 5 0 1 s1 5

  40. 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 s2 3 4 6 5 0 1 s1 5

  41. 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 s2 3 4 6 5 0 1 s1 5

  42. 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 s2 3 4 6 5 0 1 s1 5

  43. 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 s2 3 4 6 5 0 1 s1 5

  44. 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 s2 3 4 6 5 0 1 s1 5

  45. 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 s2 3 4 6 5 0 1 s1 5

  46. 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

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

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

  49. 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|

  50. 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

More Related