Find the shortest path in a graph visiting all nodes - algorithm

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.

Related

Algorithm for finding shortest "k stride" path in graph

The graph is unweighted and undirected.
Given two vertices s and t, and stride k, I want to find the shortest path between them that is:
Of length divisible by k
Every k "steps" in the path, counting from the first, are a simple path.
Meaning, the shortest non-simple path that can only "jump" k vertices at every step (exactly k), and every "jump" must be a simple sub-path.
I think this problem is equal to constructing a second graph, G', where (u, v) are an edge in G' if there exists a path of length k in G, because a BFS scan would give the required path -- but I haven't been able to construct such a graph in reasonable time (apparently it's an NP problem). Is there an alternative algorithm?
The problem you're describing is NP-hard overall because you can reduce the Hamiltonian path problem to it. Specifically, given an n-node graph and a pair of nodes s and t, you can determine whether there's a Hamiltonian path from s to t by checking if the shortest (n-1)-stride path from s to t has length exactly n-1, since in that case there's a simple path from s to t passing through every node once and exactly once. As a result, if k is allowed to be large relative to n, you shouldn't expect there to be a particularly efficient algorithm for solving this problem.
In case it helps, there are some fast algorithms for finding long simple paths in graphs that might work really well for reasonable values of k. Look up the color-coding technique, in particular, as an example of this.

Is it possible to use Dijkstra's Shortest Path Algorithm to find the shortest Hamiltonian path? (in Polynomial Time)

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.

Reduction of Leaf constrained MST p‌r‌o‌b‌l‌e‌m to Hamiltonian path p‌r‌o‌b‌l‌e‌m

It is well known that computing a spanning tree that has the minimum possible number of leaves is NP complete. But I cannot figure out a polynomial time reduction of this problem to the hamiltonian path problem.
My exponential reduction:
if(hamiltonian path exists for whole graph)
min leaves = 1;
return;
else
for each vertex of the graph
if(hamiltonian path exists for this graph after removing the vertex and its incident edges)
min leaves = 2;
return;
continue similarly for the graph deleting 2 vertices, 3 vertices, 4vertices,... until you get a minimum spanning tree with some minimum number of leaves.
So, in the worst case, this algorithm will make a total of
(N choose 1) + (N choose 2) + (N choose 3) + ....(N choose N) = 2^N
calls to the hamiltonian path problem . Hence reduction is exponential.
Please suggest a polynomial time reduction for this problem.
The idea of reducing the algorithm is that if you can show that the Hamiltonian Path problem can be solved using the constrained MST problem, (with a polynomial time reduction), then any polynomial time solution to the MST problem would allow you to solve the Hamiltonian Path problem in polynomial time. As this is impossible, it would prove the constrained MST problem cannot be solved in polynomial time.
What you are trying to do is the opposite - proving that the Hamiltonian Path problem is at least as hard as the constrained MST problem.
Note that you stated in the comments that your assignment was to reduce from the Hamiltonian Path problem, and in the question you said you were trying to reduce to the Hamiltonian Path problem.
You can easily solve the Hamiltonian Path problem using the constrained MST problem, as a Hamiltonian Path will always be a spanning tree with 2 (or 0 for a Hamiltonian Cycle) leaves.

Reduction algorithm from the Hamiltonian cycle

I believe that the Hamiltonian cycle problem can be summed up as the following:
Given an undirected graph G = (V, E), a
Hamiltonian circuit is a tour in G passing through
every vertex of G once and only once.
Now, what I would like to do is reduce my problem to this. My problem is:
Given a weighted undirected graph G, integer k, and vertices u, v
both in G, is there a simple path in G from u to v
with total weight of AT LEAST k?
So knowing that the Hamiltonian cycle problem is NP-complete, by reducing this problem to the Hamiltonian, this problem is also proved NP-complete. My issue is the function reducing it to Hamiltonian.
The big issue is that the Hamiltonian problem does not deal with edge weights, so I must convert my graph to one that doesn't have any weights.
On top of that, this problem has a designated start and finish (u and v), whereas the Hamiltonian finds a cycle, so any start is the same as the finish.
For (1), I am thinking along the lines of passing a graph with all simple paths of total weight LESS THAN k taken out.
For (2), I am thinking that this is not really an issue, because if there is a Hamiltonian cycle, then the simple path from u to v can be sliced out of it.
So, my real questions are:
Is my solution going to give me the right answer?
If yes, then how can I take out the edges that will produce simple paths of total weight less than k WITHOUT affecting the possibility that one of those edges may be required for the actual solution? Because if an edge e is taken out because it produces a simple path of weight < k for a subset of E, it can still be used in a simple path with a different combination of edges to produce a path of weight >= k.
Thanks!
Your reduction is in the wrong direction. To show that your problem is NP-complete, you need to reduce Hamilton Circuit to it, and that is very easy. I.e. show that every Hamilton Circuit problem can be expressed in terms of your problem variant.
More of a hint than an answer:
A unweighted graph can be interpreted as a weighted graph where each edge has weight 1. What would the cost of a Hamiltonian cycle be in a graph like that?
The part about the mismatch between cycles and paths is correct and is a problem you need to solve. Basically you need to prove that the Hamiltonian Path problem is also NP complete (a relatively straightfoward reduction to the Cycle problem)
As for the other part, you are doing things in the wrong direction. You need to show that your more complicated problem can solve the Hamiltonian Path problem (and is thus NP-hard). To do this you just basically have to use the special case of unit-cost edges.

Optimal Graph path containing n nodes

Is there an established algorithm for finding a path from point A to point B in a directed-weighted graph which visits exactly N nodes, but not necessarily any nodes in particular?
This problem is known to be NP-hard via a reduction from Hamiltonian path. In particular, you can solve Hamiltonian path with a polynomial-time reduction to this problem as follows: for each possible pair of nodes (s, t) in a graph with n nodes, ask if there is a path from s to t that passes through exactly n nodes. This makes only polynomial calls to your solver, so any polynomial-time solution to your problem would result in a polynomial-time solution to the Hamiltonian path problem.
So in short, you shouldn't expect a polynomial-time algorithm for this problem unless P = NP.
I'm going to assume you're trying to find the shortest/longest weight with N nodes. This probably isn't optimal, but from your original graph, you could generate a state graph with 'N*(#Nodes)' nodes representing consisting of the original node and number of steps taken and run it thorough some shortest path algorithm like http://en.wikipedia.org/wiki/Dijkstra's_algorithm.
i.e.,
A->B->C
\___/
becomes
(A,0)->(B,1)->(C,2)
\>(C,1)
Your target node would then be node (B,N) - B with N steps. This approach would allow for loops in the original graph if it's not a DAG ( (X,0)->(Y,1)->(X,2) )
I am not sure if I undersand it correctly, can you do a depth first search (up to N-1 layers away from the source)?
If you can visit your destination in that layer. you can get a path down there.

Resources