views:

338

answers:

9

I have a graph, with X nodes and Y edges. Weighted edges. The point is to start at one node, and stop at another. Now here comes the problem;

Visualize the problem. The edges are roads, and the edge weights are the max weight limits for vehicles driving on the roads. We would like to drive the biggest truck possible from A to B. So the maximum allowed weight for a truck taking a given path is the smallest weight of all of the edges in that path. I want the largest maximum allowed weight for all paths from A to B.

Can I use some sort of Dijkstra's algorithm for this problem? I'm not sure how to express this problem in the form of an algorithm that I can implement. Any help is much appreciated.

Update: I tested out somethings that didn't work for me. A node would have to have one max truck for every incoming edge.

+1  A: 

This sounds (almost) exactly like the maximum flow problem which can be solved efficiently using the Ford-Fulkerson algorithm.

As Keith has pointed out in a comment, maximum the algorithm has to be varied slightly to only find one path with maximized minimum path segment, since the truck can’t be split into multiple parts.

Konrad Rudolph
Maximum flow isn't quite right, that will allow you to chop up the truck into pieces and send each a different way.
Keith Randall
@Keith: true, not *exactly* like maximum flow. Although I like the idea of the chopped-up truck. ;-)
Konrad Rudolph
A: 

I think you're looking for this

Shortest path

edit: actually no you arent, and the maximum flow link is correct

dagoof
A: 

So if I understand this correctly, you're asking "What is the biggest truck that can drive from A to B"

To apply Dijkstra's algorithm, you'll need a way to model "cost". If you want to use a standard implementation, you could assign the inverse of the weight to the cost. Thus, the highest cost edge is the one with the lowest maximum weight. Of course, since you're probably wanting to use nice integers, you can simply modify the comparisons instead of actually using the inverses.

McPherrinM
A: 

You are looking to optimise a [http://en.wikipedia.org/wiki/Flow%5Fnetwork%5D. The capacity is the road's max weight limit; and the flow is the weight of the truck.

Edmund
+3  A: 

Dijkstra's algorithm should work, but your "distance" in this case is a bit weird. Your "distance" is the maximum sized truck you can get to a node. Let's call that M[v] for a node v. You need to process nodes in order from largest M[v] to smallest M[v] (opposite of normal Dijkstra), and calculate for each edge e from v to w:

M[w] = max(M[w], min(e.weight, M[v]))
Keith Randall
Nice, now every nodes cost is init to integer.max. What whould be default cost when I do it like this?
Algific
All nodes should default to 0 except the source node which starts at infinity.
Keith Randall
Perfect, I'll get back to eclipse now. Thank you very much!
Algific
A thing came to mind, a node has more than one incoming edge. Each edge would "bring" a max edge with itself. So that means that this won't work if there are more than one way to B?
Algific
Right, and one of those incoming edges would win. The one which last increases M[w] in the formula above (assuming no ties). The resulting path can be reconstructed in reverse. Starting at w=destination, for each w, checking all edges e:v->w and taking v such that min(e.weight, M[v]) == M[w]
Keith Randall
A: 

You could take a sort of minimum spanning tree approach. Connect nodes one edge at a time, highest-flow edges first, until A and B are connected. The last edge you added to the graph is the lowest-flow edge that you'll have to cross to get from A to B. Probably not the most efficient solution (O(N2) worst case), but at least it's straightforward.

Corey Porter
A: 

This is neither shortest path problem, nor a max flow problem. There is actually no problem. He stated - want max weight for all paths A to B. So go generate all paths from A by BFS. For all paths ending at B get find min weight of path's edges.

zufar
How would you actually store the path fragments as you do the BFS?
Algific
Then it means a graph is not small, and a number of paths can be large? I'd guess you are interested in a single path from A to B with max allowed weight? Do this: find A-B path by any algorithm, e.g. Dijkstra. Then remove an edge with smallest weight. Find A-B paths again. Keep doing it until there will be no A-B path. Last path that existed before removing edges will be the path with max weight.
zufar
A: 

Use Dijkstra's algorithm with the inverse of the maximum truck weight as edge cost, and the maximum of individual edge weights as total route cost.

i.e. edge_cost equals 1/(maximum truck weight allowed) the total_cost of a given route is the maximum of the individual edge_cost's of all the edges in the route.

jilles de wit
A: 

Various answers above propose simply Dijkstra algorithm with a modified weight function. For example, w(e) = -c(e), or 'w(e) = 1/c(e)', where w(e) is the weight of an edge, used by the algorithm, and c(e) is the capacity of the edge in the original problem formulation.

These don't work.

One can easily create counter-examples that this would yield incorrect results. For example, consider the graph:

a---3-->b---3--->c---3-->d
 \                       ^
  \                      |
   \----------3----------|

Assume the value of a ('distance of node a in the algorithm formulation) is zero. The two paths from a to d are equivalent in this problem. Yet, if we just negate the edge capacity, distance(d), using the long path, is (-3)+(-3)+(-3) = -9 while using the short path it would be -3. If we inverse the edge capacity, distance(d) using the long path would be (1/3)+(1/3)+(1/3)=1, while it would just be (1/3) using the short one.

What will work is modifying the relaxation step of the algorithm, i.e. instead of using the functions of addition (+) and less-than (<), using respectively functions min and greater-than (>), and use a max-heap instead of a min-heap. We could avoid the last two modifications if we negate the edge weights and use less-than, but no way we can avoid replacing +, since we need some operator @ where a @ a = a for all a values, whereas + only works for a=0.

So, the only correct answer I see is the very first one given, by Keith Randall.

A nice exercise would be to formally prove that the modified algorithm is correct. What needs to be proved is: - if u is the node with maximum value d(u) at any loop iteration, then no path involving unmarked nodes can create a path to u yield a distance larger than d(u).

Intuitively it is easy to see, since all other unmarked nodes have distance less than (or equal to) the distance of 'u' (because we choose u as the one with maximum distance), and the distance of the generated paths is produced by successive invocations of min, thus it can only grow smaller, not larger.

Dimitris Andreou