0

I know other people have asked similar questions but this one is slightly different.

I need to calculate distance as a combination of weight and number of nodes away from root.

So if the graph is A->20 ft->B->10 ft->C then normally you would calculate the distance between A->B to be 20 ft and A->C to be 30 feet, but I want to magnify the cost of hopping through another node.

So let's say every time you jump through another node, the cost is doubled, so A->B distance is 20 ft and B->C distance is 10 ft, but A->C is 20 ft + 2*10 ft = 40 ft

Is this possible with boost's Dijkstra's shortest path?

Community
  • 1
  • 1
Mike Manh
  • 333
  • 2
  • 11
  • I don't think the built in dijkstra algorithm can handle these types of multiple constraints. I could be wrong though. – pbible Feb 27 '15 at 22:46
  • 1
    I think this [paper](http://www.sciencedirect.com/science/article/pii/S0167637703000269) describes a similar problem. It may give you some ideas. – pbible Mar 02 '15 at 14:45
  • Thanks for that @pbible. I upvoted the paper, but it looks like there's no answer to this idea. It's an interesting question, just not straightforward in boost, I guess. – Mike Manh Mar 04 '15 at 00:14
  • No problem. Also found [this one](http://people.uwplatt.edu/~fengg/paper/tr-13.pdf). I'm not sure if it is exactly the same but suggests that it is NP-Hard. You may have to settle for a less than optimal solution. You my find Min. Spanning Tree heuristics helpful. – pbible Mar 04 '15 at 16:10
  • @pbible I'm considering writing my own visitor for djikstra, as boost allows you to. It just seems like a huuuge pain like all things BGL – Mike Manh Mar 06 '15 at 16:51
  • Agreed, it can be a pain to work with BGL, but it beats me trying to implement all of this stuff lol. It looks like a [Dikstra Visitor](http://www.boost.org/doc/libs/1_57_0/libs/graph/doc/DijkstraVisitor.html) to accomplish this goal would do most of its work in "edge_relaxed" but this may not be called unless that condition is met. You might need a way to dynamically modify the weights. I'm not sure how that would work. Be sure to post your solution back here if you figure it out. – pbible Mar 06 '15 at 18:14
  • "jumping through another node => cost is doubled" I doubt it. however if you can change that to an additive cost, i.e. "jumping through another node => pay additional cost", then you can rewrite your graph, replacing every vertex with a pair of vertices linked by an edge weighted by the additional cost to pay to hop through another node. – fferri Jan 20 '18 at 17:08

1 Answers1

1

You can do this with customizing your distance.

For example, you could double the cost of every edge except those originating at "source". It can be achieved "on the fly" with a simple inline class which has operator[](edge_descriptor e) const. You then pass this class as a distance map to Dijkstra or another shortest path algorithm.

For some applications you would like to have a fixed cost at every node (e.g. "expected weighting time at a stop-light"). Here again, you keep these node weights somewhere. Then you define an inline class which in its operator[](edge e) const adds the edge weight and this edge source weight unless the edge starts at the Dijkstra "source". Finally, you pass an object of this class as Dijkstra distance map to your favorite BGL routine.

Michael Simbirsky
  • 3,045
  • 1
  • 12
  • 24