Published 2020-09-05
A friend recently mentioned a graph/flow network problem consisting of finding the shortest path between two vertices such that some secondary cost of the path doesn’t exceed some set threshold.
As an example, we can think of a transportation network, where each edge has both a cost and a transportation time associated. Then the problem consist of finding a route that can move goods as cheaply as possible within a given maximum transportation time, if one exists. Or, vice versa, finding a fastest route that doesn’t exceed some transportation budget, if one exists.
We can formalize this as the following optimization problem:
Optimization problem [Constrained Shortest Path].
Input. A directed multigraph , two vertices , two edge weight functions , such that and are not both zero for each , and a maximum weight .
Output. A simple path from to minimizing subject to , or, if no such path exists, .
(In the above, denotes the total -weight of the path , that is, , and similarly for .)
My friend was in particular interested in whether the problem was solvable in polynomial time, so, having recently taken a course in NP-completeness, I decided to try to answer this question.
While the optimization problem is the problem we actually want to reason about, to use the concept of NP-completeness, we must first find a corresponding decision problem. A decision problem is a problem that given some input either has the answer YES or NO.
To be more precise, a corresponding decision problem is one where, given an algorithm that answers the decision problem, we can construct an algorithm for answering the optimization problem, and vice versa, such that the constructed algorithm invokes only a polynomial number of times (as a function of the input size) and only performs additional computations with polynomial time complexity.
We focus here on polynomial time complexity because it means that a polynomial-time leads to a polynomial-time algorithm for the optimization problem—and, conversely, that if no polynomial-time exists, there can exist no polynomial-time algorithm for the optimization problem.
Surprisingly, it is possible to formulate the decision problem in a way that makes the weights and symmetrical. Using this, we can group them as pairs, so that for each . This lets us simplify our notation a bit, by saying if and only if and .
A somewhat natural corresponding decision problem is then given by:
Decision problem [Constrained Shortest Path].
Input. A directed multigraph , two vertices , a pair-valued edge weight function where for each , and a maximum weight pair .
Output. YES if there exists a path from to such that , otherwise NO.
(Here, we let .)
Importantly, this problem is actually in NP. A bit informally, this means that there exists a polynomial-time non-deterministic algorithm that always answers NO for NO-instances, and is capable of answering YES for every YES-instance.
To see this, we can observe, that we can non-deterministically “guess” a simple path from to (in polynomial time), and then determine whether it satisfies the constraint (in polynomial time), by simply calculating .
We will now show that the optimization corresponds to the decision problem.
First we need to prove that we can use a solution to the optimization to solve the decision problem. To do this, consider an instance of the decision problem. We can then construct an instance of the optimization problem using the same multigraph , same vertices , same edge weight function , and same -constraint .
A solution to the optimization problem will then provide us with either a path minimizing subject to , or , if no such path exists. In the first case, we know that the decision problem must have answer YES if , as then is an example of a satisfying path, or NO if , as minimizes . In the second case, there exists no path satisfying at all, so clearly the answer to the decision problem is NO. This shows that we can turn an answer to the optimization problem into an answer to the decision problem.1
To see that the reverse is also the case, that is, that a solution to the decision problem helps us solve the optimization problem, let be an algorithm solving the decision problem. Then we can solve the optimization problem with the following algorithm:
Algorithm [Optimization problem from decision problem].
Input. A directed multigraph , two vertices , two edge weight functions , such that and are not both zero for each , and a maximum weight .
Output. A simple path from to minimizing subject to , or, if no such path exists, .
Steps.
- Let . If answers NO when given , then answer and stop.
- Perform a binary search on the interval to find the smallest such that answers YES when given .
- For each edge , do the following:
- Remove from .
- If this causes to answer NO, then put back in .
- Return the path consisting of exactly the remaining edges in .
We now need to show two things: That the algorithm correctly solves the optimization problem, and that it runs in polynomial time provided that runs in polynomial time.
Correctness. Since every simple path contains each edge at most once, we must have for all simple paths . It is therefore correct to return if answers NO in step 1 when given .
Assume now that the algorithm reaches step 2. Then there must exist at least one simple path such that . Let then denote the smallest -weight of such a path, such that no simple path satisfies both and . Clearly, , when given , must then answer YES if and NO if . Because of this, we can conclude that the binary search in step 2 must successfully find , and that every solution to the optimization problem we are trying to solve must have .
If we reach step 2, we thus know answers YES for . Since there is a minimal -weight, we know that must answer NO for all values of less than this value and YES for all values greater than or equal to it. Hence, the so the binary search must successfully find a smallest satisfactory . In other words, in order to minimize , every solution must have .
For the loop in step 3, at the start and end of each iteration, must answer YES. This means that we gradually remove each edge that is not part of every remaining solution to the problem. Hence, the edges remaining after step 3 are exactly those edges that are part of every remaining solution. But then they must in fact form the last and only remaining solution, so step 4 is correct.
Analysis. The only parts of the algorithm that could be problematic in terms of time complexity are the binary search in step 2 and the loop in step 3. However, since the binary search takes at most iterations, it invokes a number of times polynomial in the input size.2 The loop in step 3 invokes exactly once for each edge in , so it requires calls to , which is also polynomial in the input size. We can thus conclude that the algorithm invokes a polynomial number of times and otherwise takes polynomial time.
In summary, we can conclude, that there exists a (deterministic) polynomial-time algorithm for solving the optimization problem if and only if the given decision problem is in P.
Having found a corresponding decision problem, the next step is to either show it is in P or that it is NP-complete. If the problem is in P, then it can be solved by a deterministic polynomial-time algorithm. If on the other hand it is NP-complete, then it is at least as difficult to solve as all other problems in NP, and probably can’t be solved by a polynomial-time algorithm.
Failing to easily come up with a proof either way, I decided to try to look up whether the problem was well-known, hoping to find an existing proof. Doing this, I found a reference to a variant of the same problem in the book Computers and Intractability: A Guide to the Theory of NP-Completeness by Michael R. Garey and David S. Johnson:
[ND30] SHORTEST WEIGHT-CONSTRAINED PATH
INSTANCE: Graph , length , and weight for each , specified vertices , positive integers .
QUESTION: Is there a simple path in from to with total weight or less and total length or less?
Reference: [Megiddo, 1977]. Transformation from PARTITION.
Comment: Also NP-complete for directed graphs. Both problems are solvable in polynomial time if all weights are equal or all lengths are equal.
It states that the problem can be proven NP-complete by transformation from the Partition problem. Unfortunately, the reference to the proof is given as “private communication”, so no actual proof is included.
It does, however, provide a hint for the kind of problem we can use. Instead of the Partition problem, I will here provide a proof using the similar Subset Sum problem.
It should be noted, that the formulation of the Shortest Weight-Constrained Path problem differs from that of the Constrained Shortest Path problem given earlier. However, the proof presented here can, as we will see at the end, be adapted to work for the Shortest Weight-Constrained Path problem as well.
In any case, here we go.
Subset Sum is a known NP-complete problem, so if we can show that Constrained Shortest Path is at least as difficult to solve as Subset Sum, it too must be NP-complete. To show this in practice, we must show that if we can solve Constrained Shortest Path instances in polynomial time, then we can also solve Subset Sum instances in polynomial time. Somewhat counterintuitively, this means we must be able to transform the problem instances in the opposite direction, i.e., that we must be able to turn each instance of Subset Sum into an instance of Constrained Shortest Path. Additionally, this transformation must preserve the answer of the instance and must itself have polynomial time complexity.
The formulation of Subset Sum we will use here is given by:
Decision problem [Subset Sum].
Input. Positive integers and integer .
Output. YES if there exists such that , otherwise NO.
For this pair of problems, there exists a relatively clean transformation:
Transformation.
Input. An instance of Subset Sum, that is, positive integers and integer .
Output. An instance of Constrained Shortest Path, that is, a directed multigraph , two vertices , a pair-valued edge weight function where for each , and a maximum weight pair .
Steps.
- Create vertices, through , and let . For each , create two parallel edges and between and , and let .
- Let and .
- Let and for each .
- Let , where .
The transformation has polynomial time complexity. Labeling each edge with , we can visualize the constructed multigraph as:
This transformation has several important properties. First note, that for each we have . Hence, since a path from to must include either or but not both for each , we must have .
Now, assume the answer to the Subset Sum instance is YES, that is, that there exists such that . Then consider the path constructed by following if and following if for each . Then clearly and , so satisfies . The answer to the constructed Constrained Shortest Path instance is thus also YES.
Assume now instead, that the answer to the constructed Constrained Shortest Path instance is YES, that is, that there exists a path from to such that and . Then
so . Let now be defined by if and only if includes . Then , so the answer to the Subset Sum instance is YES.
We have in other words shown that the Subset Sum is a YES-instance if and only if the constructed Constrained Shortest Path instance is a YES-instance. Hence if there exists a polynomial-time algorithm for solving instances of Constrained Shortest Path, then we can combine that algorithm with the above transformation to get a polynomial-time algorithm for solving Subset Sum.
In conclusion, since Subset Sum is NP-complete, we see that Constrained Shortest Path also is NP-complete.
Note that since the construction happens to be acyclic, the problem is still NP-complete if we restrict it to acyclic directed multigraphs.
The formulation of Shortest Weight-Constrained Path from Computers and Intractability differs from our Constrained Shortest Path formulation in two aspects:
Adapting to the first difference, we can make the edges undirected and add a dummy vertex per edge to ensure there is at most one edge between any two vertices. To satisfy the second difference, we can add to the weight of every edge, and, in response, increase by .
We can visualize the new transformation as:
We then set .