简体   繁体   中英

Custom graph distance in Boost dijkstra_shortest_paths

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?

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 (eg "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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM