retagged by
5,484 views
1 votes
1 votes
Can someone share some multistage graph where dijkstra will fail but DP works correct?
retagged by

1 Answer

1 votes
1 votes

Dijkstra Algorithm will fail only if there is NEGATIVE WEIGHT EDGE CYCLE .  We use dynamic programming in Multi Stage graph because of the time complexity O(E) rather than  Dijkstra: O(ElogV)  and moreover if we apply greedy approach at every vertex for example there is graph of 4 vertices.

Firstly there is negative edge from u->w but it does not form any cycle.Dijsktra will also give correct answer. And let us say we want to find the shortest distance from S -> W . What greedy approach says is find optimal solution LOCALLY.  

1) s -> v   (Because we chose minimum of (  s->u , s->v ) and minimum distance = 1

2) v -> w ( Because only path leading to w , so no option)  and minimum distance = 1 + 1 =  2

But if we apply Dynamic approach , it will explore all the edges and you can clearly see S->U->W is the least cost path of = 3 - 2 = 1. 

Note: Now one must be in doubt then is Dijkstra greedy or dynamic . You can read Wikipedia article :

1)  https://www.quora.com/Is-Dijkstras-Algorithm-a-greedy-algorithm-or-a-dynamic-programming-algorithm#

2)  https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm#Dynamic_programming_perspective

3) https://stackoverflow.com/questions/14038011/dijkstras-algorithm-a-greedy-or-dynamic-programming-algorithm

Related questions

1 votes
1 votes
1 answer
1
3 votes
3 votes
2 answers
2
iarnav asked Apr 21, 2018
756 views
Please give some example regarding number of edges in dense graph is - |E| < |V2|I get that when we take log both sides we get O(ElogV), but I can't get this |E| < |V2|
0 votes
0 votes
1 answer
4
Alakhator asked Oct 11, 2018
1,171 views
What is the time complexity of Prim algorithm without using min heap?