1 / 80

Data Structures

Data Structures. Binary Heaps. Haim Kaplan & Uri Zwick December 2013. Required ADT. Maintain items with keys subject to Insert(x,Q) min(Q) Deletemin(Q) Decrease-key(x,Q, Δ ) Delete(x,Q). Required ADT. Decrease-key(x,Q, Δ ): Can simulate by Delete(x,Q), insert(x- Δ ,Q).

steel-hunt
Download Presentation

Data Structures

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. Data Structures Binary Heaps Haim Kaplan & Uri Zwick December 2013

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

  3. Required ADT • Decrease-key(x,Q,Δ): Can simulate by Delete(x,Q), insert(x-Δ,Q) We can use Red-Black trees to implement all operations in O(log n) time We want to implement Decrease-key in O(1) (amortized) time

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

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

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

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

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

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

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

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

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

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

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

  15. Relax(v,w) If d(v) + w(v,w) < d(w) then d(w) ← d(v) + w(v,w) π(w) ← v 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)

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

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

  18. S = {s} s4 15 ∞ 8 15 s3 2 10 Relax(s,s4) s s2 3 4 Relax(s,s3) ∞ 5 0 1 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 ∞

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  40. 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| Total running time: O(m log n) → O(m + n log n)

  41. Heap • Heap is • An almost complete binary tree • The items at the children of v are greater than the one at v 7 23 17 29 26 19 24 65 33 40 79 Heap-ordered tree

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

  43. 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 0 1 2 3 4 5 6 7 8 9 10 7 23 17 24 29 26 19 65 33 40 79 Q

  44. Heap Representation I Q array len 11 n 9 key 29 info john Store associated information directly in the array Should only be used only for succinct information

  45. Heap Representation II Q array len 11 n 9 key 29 info Store pointersto associated information

  46. Heap Representation III Q Allow decrease-key operations array len 11 n 9 4 key 29 x info pos 4

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

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

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

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

More Related