70 likes | 199 Views
Dijkstra’s Algorithm. Demo. Dijkstra’s Algorithm: Implementation. Dijkstra ( G , s ) Initialize the attributes (Distance( s ) = 0, for all others) put all vertices into a queue While the queue is not empty Dequeue , let v be the element dequeued with minimal distance,
E N D
Dijkstra’s Algorithm: Implementation Dijkstra(G, s) Initialize the attributes (Distance(s) = 0, for all others) put all vertices into a queue While the queue is not empty Dequeue, let v be the element dequeued with minimal distance, For each neighbor u of v that is still in the queue If Distance(u) > Distance(v) + weight((v,u)) Distance(u) = Distance(v) + weight((v,u)) Parent(u)=v
Complexity • O(|V|2) • Can it be done better • Use min-heap to improve efficiency
Dijkstra’s Algorithm: Implementation Dijkstra(G, s) Initialize the attributes (Distance(s) = 0, for all others) Enqueue all vertices into min-distance priority queue While the queue is not empty Dequeue, let v be the element dequeued For each neighbor u of v that is still in the queue If Distance(u) > Distance(v) + weight((v,u)) Distance(u) = Distance(v) + weight((v,u)) Parent(u)=v
Complexity changed to • O(|V|+|E|)log|V| • Why?