800 likes | 914 Views
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).
E N D
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) 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
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)
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)
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)
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 s2 3 4 5 1 s1
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 ∞
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 ∞
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 ∞
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)
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)
S = {s} s4 ∞ ∞ 8 15 s3 2 10 Relax(s,s4) s s2 3 4 ∞ 5 0 1 s1 ∞
S = {s} s4 15 ∞ 8 15 s3 2 10 Relax(s,s4) s s2 3 4 ∞ 5 0 1 s1 ∞
S = {s} s4 15 ∞ 8 15 s3 2 10 Relax(s,s4) s s2 3 4 Relax(s,s3) ∞ 5 0 1 s1 ∞
S = {s} s4 15 10 8 15 s3 2 10 Relax(s,s4) s s2 3 4 Relax(s,s3) ∞ 5 0 1 s1 ∞
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) ∞
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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| Total running time: O(m log n) → O(m + n log n)
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
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
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
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
Heap Representation II Q array len 11 n 9 key 29 info Store pointersto associated information
Heap Representation III Q Allow decrease-key operations array len 11 n 9 4 key 29 x info pos 4
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
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
Delete the minimum 23 17 29 26 19 24 65 33 40 79 23 17 24 29 26 19 65 33 40 79 Q
Delete the minimum 79 23 17 29 26 19 24 65 33 40 79 23 17 24 29 26 19 65 33 40 Q