760 likes | 769 Vues
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).
E N D
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) 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) π(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)
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|
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
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
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 7 23 17 24 29 26 19 65 33 40 79 Q
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
Delete the minimum 17 23 79 29 26 19 24 65 33 40 17 23 79 24 29 26 19 65 33 40 Q
Delete the minimum 17 23 19 29 26 79 24 65 33 40 17 23 19 24 29 26 79 65 33 40 Q
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
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)