Question

while programing bellman-ford algorithm I encountered one problem, it's question more theoretical than technical but here it is:

I have 4 points A,B,C,D, cost from A to B equals 3 etc. here is the graph:

B--3--C
|     |
3     9
|     |
A--1--D

Let's say I want to know what cost is from A to C via D, will it be: 10 ? or it will go from A to D (which cost is 1) then back from D to A (total cost is 1+1=2) then from A to B (1+1+3=5) and from B to C (1+1+3+3=8) so it will be 8 not 10 ?

I searched everywhere but I couldn't find any rational answer to my question.

EDIT:
let's say that for A->D and D->C paths count will be 2 and for A->D then D->A then A->B then B->C there will be total paths count equals 4 so will it choose the way with shortest paths count (paths count = 2) or the longer one (paths count = 4)?

Was it helpful?

Solution

For the via problem, I wouldn't use single go of the algorithm; I would call it twice. First, for A -> D, then for D -> C, as that is practically the problem; then, final path is sum of those two.

Note: I'm not familiar with Bellman-Ford algorithm; this answer is just a general remark regarding pathfinding.

OTHER TIPS

If there are no negative-weight cycles, then every shortest path visits each vertex at most once.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top