7

I have a graph, with X nodes and Y edges. Weighted edges. The point is to start at one node, and stop at another node which is the last location. 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 F. I want the largest maximum allowed weight for all paths from A to F.

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. I'm confused because Dijkstra's algorithm just only view on shortest path.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
noor asiah
  • 71
  • 1
  • 2
  • 3
    this sounds more like a Flow problem: http://en.wikipedia.org/wiki/Flow_network – BrokenGlass Sep 09 '11 at 02:09
  • This is simple min max search algorithm on an undirected graph. Find max route weight from all available routes from start node to end node where for all edges in route path each edge weight is not less then given track height – Alan Turing Sep 09 '11 at 05:17
  • @Artur Mustafin - your edit completely changed the problem. Please don't edit questions to what you want them to mean, the original meaning is perfectly clear, even if you don't understand it. – IVlad Sep 09 '11 at 10:43
  • Ok, i do not understand the phrase "I want the largest maximum allowed weight for all paths from A to F", please, describe what do you mean by saying "weight for all paths", and please be more patient in all meanings, if you understand what i mean. – Alan Turing Sep 10 '11 at 12:18
  • @Artur Mustafin - it means that you want to maximize a `k` such that there exists a path from `A` to `F` which has all of its edge weights `>= k`. – IVlad Sep 10 '11 at 13:30

4 Answers4

7

If I understand correctly, you want to find the path between some nodes that has the maximum bottleneck edge. That is, you want the path whose smallest edge is as large as possible. If this is what you want to solve, then there is a very straightforward modification of Dijkstra's algorithm that can be used to solve the problem.

The idea behind the algorithm is to run Dijkstra's algorithm with a twist. Normally, when running Dijkstra's algorithm, you keep track of the length of the shortest path to each node. In the modified Dijkstra's algorithm, you instead store, for each node, the maximum possible value of a minimum-weight edge on any path that reaches the node. In other words, normally in Dijkstra's algorithm you determine which edge to expand by finding the edge that maximizes the quantity

d(s, u) + l(u, v)

Where s is the start node, u is some node you've explored so far, and (u, v) is an edge. In the modified Dijkstra's, you instead find the edge minimizing

min(bottleneck(s, u), l(u, v))

That is, you consider the bottleneck edge on the path from the source node to any node you've seen so far and consider what bottleneck path would be formed if you left that node and went some place else. This is the best bottleneck path to the target node, and you can repeat this process.

This variant of Dijkstra's algorithm also runs in O(m + n log n) time using a good priority queue. For more information, consider looking into these lecture slides that have a brief discussion of the algorithm.

Interestingly, this is a well-known problem that's used as a subroutine in many algorithms. For example, one of the early polynomial-time algorithms for solving the maximum flow problem uses this algorithm as a subroutine. For details about how, check out these lecture notes.

Hope this helps! And if I've misinterpreted your question, please let me know so I can delete/update this answer.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • +1, this is what I was thinking about when I said there's an `O(X + Y)` algorithm - although that was actually wrong. You can make it `O(X + Y + V)` where `V` is the weight bound by using something similar to counting sort's idea: keep `MaxEdgeWeight` lists - `L[i] = list of nodes x for which d[x] = i`, where `d[x] = maximum minimum edge from source to x`. When `d[x]` decreases, we insert it into a "smaller" list, but don't delete it from the original. Instead, when expanding a node `x` from a list `i`, we first check if `d[x] = i`, and if not we ignore it. – IVlad Sep 09 '11 at 11:22
1

Here is an easy and efficient way:

Let MAX be the maximum edge weight in the graph. Binary search 0 <= k <= MAX such that you can get from A to F using only edges with weights >= k. You can use a breadth first search to see if this is possible (don't take an edge if its weight is too small).

This gives an O((X + Y) log V) algorithm, where V is the range of your weights.

IVlad
  • 43,099
  • 13
  • 111
  • 179
  • 1
    This is wrong. You can not exclude any of the graph nodes before you processed the graph completely, because it can be a part of the solution, so the excluded node can be in a path with a maximum weight. – Alan Turing Sep 09 '11 at 02:22
  • @Artur Mustafin - it's only wrong for what you edited the problem to be, it's correct for what the OP posted. – IVlad Sep 09 '11 at 10:43
  • If a truck is too heavy to cross on a certain road, there is no point in including it within the search, so IVlad is right, I believe. – Kris Schouw Sep 09 '11 at 20:11
  • Yep. it is possible in one of the cases, sure. – Alan Turing Sep 10 '11 at 12:20
  • I was trying to solve the problem `UVa 10099 - The Tourist Guide`. I first used the Floyd-Warshall algorithm which was able to solve the problem in `O(X^3)`. Then I solved the problem using this with a much better time complexity of `O((X + Y) log V)` where `V` is the range of weights in the graph. – risingStark Apr 17 '21 at 18:58
1

No Dijkstra, no flow problem. It's a lot easier: Just use your favorite graph search (BFS or DFS).

Instead of computing & tracking the cost associated with reaching a certain node in the graph, just compute the 'size' of the biggest truck that is allowed to use this path (the minimum of weights of all edges in the path). When multiple search paths meet in a node throw away the path that has the lower 'truck weight limit'.

Michael Nett
  • 375
  • 1
  • 10
  • I agree, use the conjunction matrix (M) for the graph, reorder the nodes so that for the start node (A) index for the column is 0, and for the last node (F) index = L(M)-1. – Alan Turing Sep 09 '11 at 05:16
0

What a Dijkstra-like algorithm requires is optimal substructure and a way quickly to compute the objective value for a one-edge extension of a path with a known objective value. Here, optimal substructure means that if you have an optimal path from a vertex x to a different vertex y, then the subpath from x to the second-to-last vertex is optimal.

(IVlad, I only can get O(X + Y) with randomization.)

doug
  • 36
  • 2
  • (Note: this framework covers algorithms attributed to people who are not Dijkstra.) – doug Sep 09 '11 at 02:40
  • Can't put this comment where it belongs: even though flows and paths are closely related combinatorial objects, I don't think any of my colleagues or I would describe this as a "flow problem". – doug Sep 09 '11 at 03:53