We know that finding a maximum weight path between two vertices is np-hard. But if we restrict edge weights, for eg. all edge weights are less than some particular value x. I am defining problem below clearly.
I have a directed graph G(V,E) in which weight of every edge is between 1 and |V|. I want to find maximum weight path between the two vertices u and v.
Is this problem polynomial-time solvable?
I am afraid your restricted problem is still NP-hard because finding the longest simple path is NP-complete even if the weights are all equal to 1.
A proof is on wikipedia:
The NP-hardness of the unweighted longest path problem can be shown using a reduction from the Hamiltonian path problem: a graph G has a Hamiltonian path if and only if its longest path has length n − 1, where n is the number of vertices in G. Because the Hamiltonian path problem is NP-complete, this reduction shows that the decision version of the longest path problem is also NP-complete. In this decision problem, the input is a graph G and a number k; the desired output is "yes" if G contains a path of k or more edges, and no otherwise.[1]
If the longest path problem could be solved in polynomial time, it could be used to solve this decision problem, by finding a longest path and then comparing its length to the number k. Therefore, the longest path problem is NP-hard. The question "does there exist a simple path in a given graph with at least k edges" is NP-complete.
Related
I'm taking the Algorithms: Design and Analysis II class, one of the questions asks:
Assume that P ≠ NP. Consider undirected graphs with nonnegative edge
lengths. Which of the following problems can be solved in polynomial
time?
Hint: The Hamiltonian path problem is: given an undirected graph with
n vertices, decide whether or not there is a (cycle-free) path with n
- 1 edges that visits every vertex exactly once. You can use the fact that the Hamiltonian path problem is NP-complete. There are relatively
simple reductions from the Hamiltonian path problem to 3 of the 4
problems below.
For a given source s and destination t, compute the length of a shortest s-t path that has exactly n - 1 edges (or +∞, if no such path
exists). The path is allowed to contain cycles.
Amongst all spanning trees of the graph, compute one with the smallest-possible number of leaves.
Amongst all spanning trees of the graph, compute one with the minimum-possible maximum degree. (Recall the degree of a vertex is the
number of incident edges.)
For a given source s and destination t, compute the length of a shortest s-t path that has exactly n - 1 edges (or +∞, if no such path
exists). The path is not allowed to contain cycles.
Notice that a Hamiltonian path is a spanning tree of a graph and only has two leaf nodes, and that any spanning tree of a graph with exactly two leaf nodes must be a Hamiltonian path. That means that the NP-Complete problem of determining whether a Hamiltonian path exists in a graph can be solved by finding the minimum-leaf spanning tree of the graph: the path exists if and only if the minimum-leaf spanning tree has exactly two leaves. Thus, problem 2 is NP-Complete.
Problem 3 is NP-Hard; here is a paper that proves that.
That means, between 1 and 4, one is NP-Complete, another is in P. It seems like problem 4 reduces trivially to the the Hamiltonian path problem, but I'm not able to understand how having a cycle makes it solvable? Or is it the other way?
For the first one you can use Dijkstra to get shortest even and odd distances possible. To this end for every vertex you need to store not a single minimum number, but two of them. One is minimum weight of an odd path, another one is for minimum weight of an even path. After you have these two lengths you can easily increase path length by even number of edges if cycles are allowed. So, the first problem is from P. Step-be-step algorithm would be:
Find shortest even and odd length paths.
Increase length of one of these paths which has the same parity as n-1 to n-1 by adding cycle of length 2 required number of times.
Note: This is almost the same questions as this: Shortest path to visit all nodes
But I have a complete graph.
Problem: Consider a complete undirected graph with nonnegative edge lengths. Question: Compute the shortest path that visits every node at least once.
NB: This is NOT the TSP problem. The path does not have an ending node and the path can pass through nodes more than once.
Other notes:
The number of nodes is small (less than 20).
Problem is still NP-Complete (for decision variant), with reduction from Hamiltonian Path Problem.
Given Hamiltonian Path Problem instance G=(V,E), reduce it to your problem with: G'=(V, E', w) and path length |V| - 1.
Where:
E' = VxV
w(u,v) = 1 if (u,v) is in E
w(u,v) = 2 otherwise
If there is a hamiltonian path in G, then there is a path in G' that costs |V| - 1.
If there is a path in G' that costs |V| - 1, then by following these edges in G, we get a Hamiltonian Paht.
Thus, the above is a polynomial reduction from Hamiltonian Path Problem to this TSP variant, and since Hamiltonian Path Problem is NP-Hard, so is this problem.
Claim
Allowing nodes to be revisited does not make the problem substantially easier.
Explanation
Suppose we wish to find a Hamiltonian path in a graph G. We can turn this into an instance of your problem by setting the edge weights to 1 for edges in G, and edge weights to 10 for edges not in G.
We now have a complete graph H with non-negative edges.
Graph G has a Hamiltonian path if and only if we find the shortest path in H is of length n-1.
Recommendation
Therefore your modified problem is NP-hard, so it seems unlikely that you can do better than simply adapting standard TSP techniques (such as the Held-Karp algorithm ) to solve it.
I have a weighted and undirected graph G with n vertices. Two of these vertices are X and Y. I need to find the shortest path that starts at X, ends at Y and passes through all the vertices of G (in any order).
How I can do this?
This is not the Travelling Salesman Problemm: I don't need to visit each vertex just once and I don't want to return to the first vertex.
This problem is basically NP-Hard, I am going to give a sketch of a proof (and not a proper reduction), that explains that unless P = NP, there is no polynomial solution to this problem.
Assume torwards contradiction that this problem can be solved in polynomial time O(P(n)) by some algorithm A(G,x,y)
Define the following algorithm:
HamiltonianPath(G):
for each pair (x,y):
if A(G(x,y) == |V| - 1):
return true
return false
This algorithm solves Hamiltonian Path Problem.
-> If there is a path between some pair x,y that goes through all nodes and its length is exactly |V|, it means it did not use any
vertex twice, and the path found is Hamiltonian.
<- If there is a Hamiltonian Path v1->v2->...->vn, then when invoking
A(G,v1,vn), you will find the shortest possible path, which its
length is at most |V|-1 (and it cannot be less because it needs to
go through all vertices), and the algorithm will yield true.
Complexity:
Complexity of the algorithm is O(n^2 * P(n)), which is polynomial time.
So, assuming such an algorithm exists, Hamiltonian Path can be solved in polynomial time, and since it (Hamiltonian Path Problem) is NP-Complete, P=NP.
Try to look at Dijkstra's algorithm
The basic idea is to filter the routes that traverse all the nodes and get the route with the shortest path.
Bu actually this may be not an optimal way.
I've read that the problem of finding whether a Hamiltonian path exists in a graph is NP-Complete, and since Dijkstra's Shortest Path Algorithm runs in Polynomial Time, it cannot be modified to find the shortest Hamiltonian path. (Is this logic valid?)
But what if you are given two nodes (say A and Z) on an undirected graph (with all edges having non-negative costs), and it is given that there is at least one Hamiltonian path with the given nodes (A and Z) as end points. Given these specifications, would it now be possible to modify Dijkstra's algorithm to find the shortest Hamiltonian path with A and Z as endpoints? (in Polynomial Time)
Note: I'm only concerned with finding the shortest Hamiltonian path from two nodes specifically. For example, if there is a graph containing 26 nodes (labelled A to Z), what is the shortest path that passes through all points but starts at A and ends at Z. (I'm not concerned with finding other Hamiltonian paths with different endpoints, just A and Z)
Additional question: If the answer is "No" but there is another algorithm that can be used to solve this, what algorithm is it, and what is its time complexity?
(Note: This question has "hamiltonian-cycle" as a tag, even though I'm looking for a Hamiltonian PATH, because I do not have enough rep to make the tag "hamiltonian-path". However, let's say A and Z is connected by exactly one edge, then the shortest Hamiltonian path can be found by finding the shortest Hamiltonian cycle and then removing the edge connecting A and Z)
No, this is not possible. Your simplified problem is still NP-hard. A reduction from travelling salesman:
Given a graph (V, E), find the shortest path that visits each v in V exactly once. Take an arbitrary vertex v in V. Split v into two vertices v_source and v_sink. Use your algorithm to find the shortest hamiltonian path P from v_source to v_sink. P is the shortest cycle starting and ending at v which visits each v in V. Since P is a cycle, the 'starting' vertex is irrelevant. Therefore, P is also the solution to the travelling salesman problem.
The reduction is obviously polynomial time (constant, actually), so your problem is NP-hard.
But what if you are given two nodes (say A and Z) on an undirected
graph (with all edges having non-negative costs), and it is given that
there is at least one Hamiltonian path with the given nodes (A and Z)
as end points. Given these specifications, would it now be possible to
modify Dijkstra's algorithm to find the shortest Hamiltonian path with
A and Z as endpoints? (in Polynomial Time)
How do you propose to modify it? This only works if there is a single path between A and Z and it visits all the other points on the graph. Otherwise, Dijkstra would terminate some shorter path that only visits some subset of the nodes. If there is a Hamiltonian path between A and Z, you could solve the longest path problem, but this is also NP-hard.
I'm not asking for an algorithm to check the existence of a negative cycle in a graph(Bellman Ford or Floyd Warshall can do that), rather whether or not there exists a polynomial time algorithm to find the shortest path between two points when the graph contains at least one negative cycle that is reachable from the source vertex and the target vertex can be reached from the negative cycle.
If you are looking for a path (no repeating vertices) then this problem is NP hard.
You can reduce longest path problem to this one by simply multiplying weights by -1.
The main difference between classical (only positive weights on edges) shortest path (which is in P) and longest path problem (which is NP-complete) is following:
All polynomial shortest path algorithms are basically calculating shortest walk, but since you have positive weight on edges, the shortest walk is also shortest path (repeating edges does not decrease the length of walk).
In longest path problem you have to somehow check for uniqueness of vertices on the path.
When you have negative cycle, then the Bellman-Ford algorithm calculates the length of the shortest walk which has at most N edges.