4

Suppose there's an undirected graph and each edge that connects any two nodes has two weights (i.e. distance and cost). I want to get the shortest path, but also make sure I do not go beyond a certain cost.

I've tried implementing Djikstra's and simply backtracking (for lack of a better term) if I exceed the cost, until I traverse the entire graph. However, I'm looking for a faster solution than this. I've also attempted to use a function that creates a weight given the distance and cost of an edge, but I don't think this will return the optimal solution.

Any thoughts?

nbro
  • 15,395
  • 32
  • 113
  • 196
user3646937
  • 161
  • 1
  • 1
  • 3
  • There is an answer [here](http://stackoverflow.com/questions/2710516/dijkstra-shortest-path-algorithm-with-edge-cost). – G. Bach Feb 04 '16 at 14:59

2 Answers2

1

We can convert your graph from the original graph with E edges and V vertices (E,V) to another graph (E, V') with each node v'xy in V' is the minimum distance to travel from start to node x with cost y.

So, starting at the start node 0, assuming that we travel to node 1 with distance a and cost b, now, we have our distance matrix:

dist[0][0] = 0, and dist[1][b] = a;

Thus, this problem become a normal Shortest path problem.

Pham Trung
  • 11,204
  • 2
  • 24
  • 43
-1

Take an weighted average of cost and distance and take it as parameter.

Say average, p=w*cost+(1-w)*distance

Now select w as per your cost limit.

Now in any shortest path algorithm will work comparing p.

0x5050
  • 1,221
  • 1
  • 17
  • 32