Dijkstra's Shortest-Path Algorithm

Question: doesn’t BFS already compute shortest paths in linear time?
Answer:yes,IF l e = 1 for every edge e.
Question: why not just replace each edge e by directed path of l e unit length edges.
Answer: blows up graph too much.
Solution: Dijkstra’s shortest path algorithm.
Dijkstra’s Algorithm
Initialize:
X = [s] [vertices processed so far]
A[s] = 0 [computed shortest path distances]
B[s] = empty path[computed shortest paths]
B[s] only to help explanation.
Main Loop
while X V :
Main Loop cont’ d:
among all edges ( v , w ) E
with v X , w X
pick the one that minimizes
A [ v ] + l v w
[call it (v*, w*)]
add w* to X
A[ w ]= A[ v ] + l v w
B[ w ] = B[ v ] u ( v , w )
Heap Operations
Recall: perform Insert,Extract-Min in O(log n) time.
(1) a perfectly balanced binary tree
(2)Heap property: at every node, key <= children’s keys
(3)extract-min by swapping up last leaf, bubbling down
(4) insert via bubbling up.
Also: will need ability to delete from middle of heap.(bubble up or bubble down)
Two Invariants
Invariant #1: elements in heap = vertices of V-X
Invariant #2: for v X
Key[v] = smallest Dijstra greedy score of an edge(u,v) in E with u in X
if( + if no such edges exist)
Point: by invariants,
Maintaining the Invariants
To maintain Invariant #2:
[that v X ]: Key[v] = smallest Dijkstra greedy score of edge(u,v) with u in X.
When w extracted from heap(added to X)
for each edge(w,v) in E:
if v in V-X (in heap)
delete v from heap
recompute key[v] = min{key[v],A[w] + l w v }
re-Insert v into heap.

猜你喜欢

转载自blog.csdn.net/qq_31805127/article/details/80232861
今日推荐