Sunday, April 27, 2014

Dijkstra’s shortest path algorithm



Pseudo code (from Algorithm Class):

for each Vertex v
    if(v is source)  Distance(v) =0, v.parent = null;
    else Distance[v]=INT_MAX;
Create an empty priority queue PQ;
for each Vertex V
    PQ.insert(v, Distance(v));

while(PQ not empty){
    V = PQ.removeMin();
    for each neighbor  U of V
        if(Distance(V) + EdgeLenghth(U, V) < Distance(U)){
            Distance(U) = Distance(V) + EdgeLenghth(U, V) ;
            U.parent = V;
        }
}

Reference
http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/

No comments:

Post a Comment