I'm looking for an algorithm that can find the shortest path between two nodes in an undirected graph with a cost which is dynamic. By dynamic, I mean that the cost on edge is dependent on the next (future) step. For example, in a graph like that:
I'm looking for the shortest path from "a" to "e" but the cost of "a" to "b" depends on next step. If I go to c, it is 7, and if I go to d, it is 9.
My question is: Is there an algorithm which solves that problem?
The Floyd Warshall Algorithm is for solving all pairs shortest path problems.
Dynamic Programming approach We fill the table in bottom up manner, we start from e=0 and fill the table till e=k . Then we have our shortest path cost stored in dp[u][v][k] where u is source, v is destination and k is number edges between path from source to destination.
Dijkstra's Algorithm finds the shortest path between a given node (which is called the "source node") and all other nodes in a graph.
The most important algorithms for solving this problem are: Dijkstra's algorithm solves the single-source shortest path problem with non-negative edge weight. Bellman–Ford algorithm solves the single-source problem if edge weights may be negative.
Reduce the problem to 'regular' shortest path problem
Create dummy vertices b1,b2 (instead of b
), and the edges:
(a,b1,7), (b1,c,6), (a,b2,9), (b2,d,5)
The rest of the edges and vertices remain as they originally were.
Now, run regular shortest path algorithm (Dijkstra / Bellman Ford) from the source to the target.
(Repeat the process for any 'conditional' weight edges).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With