Dijkstra(G,w,s) {
ISS(G,s);
let S be an empty set
let Q be a priority queue, initialized with V[G]
while Q is not Empty:
u<-extractMin(Q);
add u to S
for each vertex v neighbor of u
Relax(u,v,w);
}
my question is, why is it important to choose the MINIMUM d[v] of all v in Q in every step of the algorithm in the while loop, whats gonna heppen if we dont choose the minimum?
i mean from the way i see it, all edges (u,v) are gonna get relaxed in a breadth first order(means that if – s->u->v and (s,v) not in E then (s,u) would get relaxed before (u,v)),
so why is it important to choose the minimal d[v] every time?
assume there exists a function extractMaxFiniteD(Q) that returns vertex v such that it has max d[v] that is finite in Q
lets assume we change that line to u<-extractMaxFiniteD(Q); can any one draw me a graph in which the modified alg would fail – or even better – what property of shortest path would get violeted?
I know this question might be pretty hard and abstract, but it would be great if some1 could help me with that.
example:
nodes: a,b,c
edges (and weights): (a,b,1) (a,c,10) (b,c,1)
try your algorithm on this. you’ll find that the least cost path to c is 10 when its obviously 2.
when you remove a node from Q you don’t relax it ever again, if you remove a node with maximal cost then you don’t consider less expesive ways to reach that node.
if you don’t want to select the minimal node from Q then you can’t remove it from Q either, you must keep it in the set so it can be relaxed in future iterations. that’s basically what the bellman-ford algorithm does.