So my problem is that I have a directed graph G with non-negative edge lengths and I wish to find the shortest path between two nodes u and v such that they only pass through one marked node in the graph.
If we did not have the condition involving the marked nodes this problem could easily be solved using Dijkstra's algorithm.
procedure dijkstra(G, l, s)
Input: Graph G = (V, E), directed or undirected;
positive edge lengths {le : e ∈ E}; vertex s ∈ V
Output: For all vertices u reachable from s, dist(u) is set to the distance from s to u.
for all u ∈ V :
dist(u) = ∞
prev(u) = nil
dist(s) = 0
H = makequeue(V ) (using dist-values as keys)
while H is not empty:
u = deletemin(H)
for all edges (u, v) ∈ E:
if dist(v) > dist(u) + l(u, v):
dist(v) = dist(u) + l(u, v)
prev(v) = u
decreasekey(H, v)
Additionally, to handle the condition I was considering adding a value which gave the current number of nodes in the current best path from s to u (this would be updated whenever dist(u) was updated). But this doesn't seem to work since the algorithm is not keeping track of all possible paths that we've seen with one or less nodes , but rather just the lowest distance path.
My question is am I on the right track and just need to modify the algorithm additonally? Or is there another algorithm that would accomplish this easier?
Also, this is for a homework problem so please do not post an entire solution, I'm just looking for guidance.
As you don't want the entire solution, I will give you some hints. Stop reading each paragraph and then try to solve the problem, I'll try to go from more general hints to more specific ones.
First, I don't think your current idea can solve the problem. So I will try to guide you to a different approach. It is a good idea to think of Dijkstra, but instead of modifying Dijkstra, consider transforming the graph so to achieve that running Dijkstra in your new graph solves the original problem.
How to remove the original restriction of the set P? Well, it is important that the graph is directed (or at least for my idea). Think of a way of transforming the graph so as to force that if you enter one node of P, you cannot enter another one of P again.
Last idea, without still giving the solution. Consider replicating the graph, maybe deleting some edges and connecting the nodes from the two copies in some way.
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