Presentation is loading. Please wait.

Presentation is loading. Please wait.

ליאור שפירא, חיים קפלן וחברים

Similar presentations


Presentation on theme: "ליאור שפירא, חיים קפלן וחברים"— 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 תרגיל 3 – Median Heap ממשו מבנה נתונים התומך בפעולות
insert בזמן O(logn) extract-median בזמן O(logn) find-median בזמן O(1) 2 4 5 7 8 12 14 15 20

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

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

11 תרגיל 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)

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 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)

17 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)

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 3 4 s2 5 1 s1

20 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

21 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

22 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

23 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)

24 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)

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

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

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

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

29 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)

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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

38 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

39 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

40 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

41 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

42 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

43 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

44 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

45 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

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

51 Heapsort (Williams, Floyd, 1964)
Put the elements in an array Make the array into a heap Do a deletemin and put the deleted element at the last position of the array

52 Put the elements in the heap
79 65 26 24 19 15 29 23 33 40 7 Q 79 65 26 24 19 15 29 23 33 40 7

53 Make the elements into a heap
79 65 26 24 19 15 29 23 33 40 7 Q 79 65 26 24 19 15 29 23 33 40 7

54 Make the elements into a heap
Heapify-down(Q,4) 79 65 26 24 19 15 29 23 33 40 7 Q 79 65 26 24 19 15 29 23 33 40 7

55 Heapify-down(Q,4) 79 65 26 24 7 15 29 23 33 40 19 Q 79 65 26 24 7 15 29 23 33 40 19

56 Heapify-down(Q,3) 79 65 26 24 7 15 29 23 33 40 19 Q 79 65 26 24 7 15 29 23 33 40 19

57 Heapify-down(Q,3) 79 65 26 23 7 15 29 24 33 40 19 Q 79 65 26 23 7 15 29 24 33 40 19

58 Heapify-down(Q,2) 79 65 26 23 7 15 29 24 33 40 19 Q 79 65 26 23 7 15 29 24 33 40 19

59 Heapify-down(Q,2) 79 65 15 23 7 26 29 24 33 40 19 Q 79 65 15 23 7 26 29 24 33 40 19

60 Heapify-down(Q,1) 79 65 15 23 7 26 29 24 33 40 19 Q 79 65 15 23 7 26 29 24 33 40 19

61 Heapify-down(Q,1) 79 7 15 23 65 26 29 24 33 40 19 Q 79 7 15 23 65 26 29 24 33 40 19

62 Heapify-down(Q,1) 79 7 15 23 19 26 29 24 33 40 65 Q 79 7 15 23 19 26 29 24 33 40 65

63 Heapify-down(Q,0) 79 7 15 23 19 26 29 24 33 40 65 Q 79 7 15 23 19 26 29 24 33 40 65

64 Heapify-down(Q,0) 7 79 15 23 19 26 29 24 33 40 65 Q 7 79 15 23 19 26 29 24 33 40 65

65 Heapify-down(Q,0) 7 19 15 23 79 26 29 24 33 40 65 Q 7 19 15 23 79 26 29 24 33 40 65

66 Heapify-down(Q,0) 7 19 15 23 40 26 29 24 33 79 65 Q 7 19 15 23 40 26 29 24 33 79 65

67 How much time it takes to build the heap this way ?
23 7 15 33 79 29 24 26 19 65 40 We have at most n/2 nodes heapified at height 1 We have at most n/4 nodes heapified at height 2 We have at most n/8 nodes heapified at height 3

68 Summary We can build the heap in linear time (we already did this analysis) We still have to deletemin the elements one by one in order to sort that will take O(nlog(n))

69 שאלה 1 בערימת מקסימום, החציון נמצא בהכרח: א. בשורש.
בערימת מקסימום, החציון נמצא בהכרח: א. בשורש. ב. בעומק לכל היותר . ג. בעומק לכל היותר ד. בשתי השכבות הנמוכות ביותר. ה. אף אחד מהנ"ל.

70 תשובה 1 ה. החציון יכול להיות בן ישיר של השורש (למשל אם כל תת עץ ימין גדולים מכל תת עץ שמאל) ויכול גם להיות באחד העלים (אם חצי הערכים הקטנים ביותר נמצאים בעלים). המקסימום חייב להימצא בשכבה התחתונה.

71 שאלה+תשובה 2 האם מערך הממוין בסדר הפוך הוא ערימה? תשובה: כן.
האיבר במקום ה-i גדול מהאיבר ה-2i+1 ו-2i (שהם בניו) לכל i.

72 שאלה 3

73 תשובה 3 ב. חסם עליון: עומק כל הצמתים הוא O(lg n). לכן הסכום הוא O(n lg n). חסם תחתון: עומק כל העלים הוא Ω(lg n) ויש Ω(n) עלים לכן הסכום הוא גם Ω(n lg n)

74 שאלה 4

75 תשובה 4 א. החישוב בדומה לחישוב זמן הריצה של Build-Heap.

76 הסוף


Download ppt "ליאור שפירא, חיים קפלן וחברים"

Similar presentations


Ads by Google