问题
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:
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:
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 nthTerm){
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 nthTerm){
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.
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
回答3:
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.
回答4:
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?
回答5:
No.
Dynamic Programming follows Bottom-up strategy. i.e. Solving all the sub-problems and finally arriving at the result.
But in Dijkstra's algorithm, we are following the Top-down strategy. i.e. We solve the solution one by one, caching the local optimums. This process of caching is referred to as Memoization.
So, I would say, Dijkstra's algorithm uses Greedy + Memoization.
来源:https://stackoverflow.com/questions/24140623/is-dijkstras-algorithm-dynamic-programming