Is Dijkstra’s algorithm a Greedy or Dynamic Programming algorithm?

Technology CommunityCategory: Dynamic ProgrammingIs Dijkstra’s algorithm a Greedy or Dynamic Programming algorithm?
VietMX Staff asked 3 years ago

Both.

  • It’s greedy because you always mark the closest vertex.
  • It’s dynamic because distances are updated using previously calculated values.

But would say it’s definitely closer to dynamic programming than to a greedy algorithm. To find the shortest distance from A to B, it does not decide which way to go step by step. Instead, it finds all places that one can go from A, and marks the distance to the nearest place. Marking that place, however, does not mean you’ll go there. It only means that distance can no longer be made shorter assuming all edges of the graph are positive. The algorithm itself does not have a good sense of direction as to which way will get you to place B faster. The optimal decisions are not made greedily, but are made by exhausting all possible routes that can make a distance shorter. Therefore, it’s a dynamic programming algorithm, the only variation being that the stages are not known in advance, but are dynamically determined during the course of the algorithm. You can call it a “dynamic” dynamic programming algorithm, if you like, to tell it apart from other dynamic programming algorithms with predetermined stages of decision making to go through