1 / 76

Heaps

Heaps. Chapter 6 in CLRS. Motivation. Router: 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).

prude
Télécharger la présentation

Heaps

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. Heaps Chapter 6 in CLRS

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

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

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

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

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

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

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

  9. 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 ∞

  10. 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 ∞

  11. 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 ∞

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

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

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

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

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

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

  18. 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) ∞

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  40. Heap (min) • Heap is • A complete binary tree • For any node v, both children’s keys are larger than its key 7 23 17 29 26 19 24 65 33 40 79 Heap-ordered tree

  41. Array Representation • Representing a heap with an array • Get the elements from top to bottom, from left to right 7 23 17 29 26 19 24 65 33 40 79 7 23 17 24 29 26 19 65 33 40 79 Q

  42. Array Representation • Left(i): 2i+1 • Right(i): 2i+2 • Parent(i): (i-1)/2 7 23 17 29 26 19 24 65 33 40 79 7 23 17 24 29 26 19 65 33 40 79 Q

  43. Find the minimum • Return Q[0] 7 23 17 29 26 19 24 65 33 40 79 7 23 17 24 29 26 19 65 33 40 79 Q

  44. Delete the minimum 7 23 17 29 26 19 24 65 33 40 79 7 23 17 24 29 26 19 65 33 40 79 Q

  45. Delete the minimum 23 17 29 26 19 24 65 33 40 79 23 17 24 29 26 19 65 33 40 79 Q

  46. Delete the minimum 79 23 17 29 26 19 24 65 33 40 79 23 17 24 29 26 19 65 33 40 Q

  47. Delete the minimum 17 23 79 29 26 19 24 65 33 40 17 23 79 24 29 26 19 65 33 40 Q

  48. Delete the minimum 17 23 19 29 26 79 24 65 33 40 17 23 19 24 29 26 79 65 33 40 Q

  49. Delete the minimum Q[0] ← Q[size(Q)-1] size(Q) ← size(Q)-1 Heapify-down(Q,0) 17 23 19 29 26 79 24 65 33 40 17 23 19 24 29 26 79 65 33 40 Q

  50. Heapify-down(Q,i) • Heapify-down(Q, i) • l ← left(i) • r ← right(i) • smallest ← i • if l < size(Q) and Q[l] < Q[smallest] • then smallest ← l • if r < size(Q) and Q[r] < Q[smallest] • then smallest ← r • if smallest > i then Q[i] ↔ Q[smallest] • Heapify-down(Q, smallest)

More Related