24

All implementation of Dijkstra's algorithms I have seen do not have a recursive function, but I have also read that by definition dynamic programming is an algorithm with a recursive function and "memory" of things already calculated.

So is Dijkstra's algorithm with a loop qualified as dynamic programming?
Or in order to qualify as dynamic algorithm I have to change a loop into a recursive function.

1
  • 7
    You can have dynamic programming with recursive or iterative solutions. It's the approach of simplifying the problem into subproblems that is dynamic, not whether or not you solve it recursively or iteratively. Commented Jun 10, 2014 at 12:16

5 Answers 5

39

All implementation of Dijkstra's algorithms I have seen do not have a recursive function

Recursion gives us a stack. But we don't need a stack here. We need a priority queue. The efficient way to implement Dijkstra's algorithm uses a heap (stl priority_queue in c++).

but I have also read that by definition dynamic programming is an algorithm with a recursive function and "memory" of things already calculated.

Dynamic Programming need not be written in a recursive way though most people prefer to write it in a recursive way.

For example:

int dp[MAX]={-1,-1,...};
find fibonacci(int n){
   if(n <= 1) return n;
   if(dp[n]!=-1) return dp[n];
   return dp[n]=fibonacci(n-1)+fibonacci(n-2);
}

is a recursive implementation of DP

and

int dp[MAX]={0,1,-1,-1,-1,..};
int lastFound = 1;
int fibonacci(int n){
    for(int i=lastFound+1;i<=n;i++){
       dp[i]=dp[i-1]+dp[i-2];
    }
    return dp[n];
}

is an iterative way of writing it to save stack memory.

Remember that any algorithm

  1. that does not recalculate the result that is already found and

  2. uses the existing result to find the required result

can be called as a DP. Even using BFS to find the shortest path in an unweighted graph can be considered as DP in a way as it passes the above 2 conditions.

So is Dijkstra's algorithm with a loop qualified as dynamic programming?

Dijkstra is DP!

Or in order to qualify as dynamic algorithm I have to change a loop into a recursive function.

No

Sign up to request clarification or add additional context in comments.

1 Comment

As iterative functions save stack memory (as described in above ans) , some modern languages like kotlin automatically convert recursive functions into non recursive ones. softwareengineering.stackexchange.com/questions/202983/… Infact every recursive can be converted into non recursive functions and vice versa. stackoverflow.com/questions/931762/…
5

There is a paper about it entitled "Dijkstra’s algorithm revisited: the dynamic programming connexion" by Moshe Sniedovich. http://matwbn.icm.edu.pl/ksiazki/cc/cc35/cc3536.pdf

The paper claim that Dijkstra’s algorithm is strongly inspired by Bellman’s Principle of Optimality and that both conceptually and technically it constitutes a dynamic programming successive approximation procedure par excellence.

Comments

3

Dijkstra's Algorithm is like a water filling algorithm. At each step it chooses local minima that's the reason why many consider it as Greedy Algorithm. If you will try this same algorithm with choosing any arbitrary path not the local minima, then you will come to know that it's still working. Choosing a local minima is just because of filling water optimally as I mentioned earlier that it's like water filling algorithm. So, the main concept behind Dijkstra's Algorithm is to store the previous result to predict the upcoming result and that is what Dynamic Approach is.

For more details refer below link

Why does Dijkstra's algorithm work?

1 Comment

you have to explain what is water filling problem :P
0

I had the same question after reading CLRS for college. If you are studying purely for class/tech interviews, CLRS labels Dijkstra's as a greedy algorithm and Bellman-Ford as a dynamic programming algorithm based upon all the reasons listed by commenters above.

IMO, It's more important to understand why Dijkstra's is the classic greedy algorithm example than why it is not a dynamic algorithm even though it has some dynamic programming like logic.

Once you understand how Dijkstra's work and why its runtime is better in most cases than Bellman-Ford's, than approach Bellman-Ford. Try to understand why if a graph has a negative edge weight we can use BF and the dynamic programming part will make sense.

I think sometimes CLRS/Youtube throws out these definitions and we try to fit these algorithms into these label buckets. I also think the slides for CLRS don't do a great job of explaining the reason we are learning certain algos versus others and their similarities. You really have to spend a lot of time piecing it all together yourself.

Botton line is that academically, Dijkstra's Single Source Shortest Path Algorithm is the classic greedy algorithm students learn and do not confuse it with Dynamic Programming Algos like Bellman-Ford.

Comments

-3

You address two questions:

Dynamic Algorithms mean breaking a procedure down into simpler tasks. Several dynamic algorithms iclude the idea of recursion but are not limited too..

Considering Dijkstra's algorithm the clasic solution is given by a for loop and is not a dynamic algorithm solution.

However, From a dynamic programming point of view, Dijkstra's algorithm is a successive approximation scheme that solves the dynamic programming functional equation for the shortest path problem by the Reaching method.

In fact, Dijkstra's explanation of the logic behind the algorithm, namely:

Problem 2. Find the path of minimum total length between two given nodes P and Q.

Note: taken from Wikipedia

2 Comments

Also from wikipedia: Sometimes, applying memoization to a naive basic recursive solution already results in an optimal dynamic programming solution; however, many problems require more sophisticated dynamic programming algorithms. Some of these may be recursive as well but parametrized differently from the naive solution. Others can be more complicated and cannot be implemented as a recursive function with memoization. Examples of these are the two solutions to the Egg Dropping puzzle below.
Dijkstra's is not an approximation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.