Optimal Graph path containing n nodes - algorithm

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.

Related

Find the shortest path in a graph visiting all nodes

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.

Given a weighted undirected graph, how do I find a path which has the total weights close to a given value?

Suppose I have a weighted, undirected graph. Each edge has a positive weight. I would like to find a simple path (no vertices appear in the path twice) from a given source node (s) to a target node (t) which has the total sum of weights close to a given value (P).
Even though it sounds like a well-studied problem, I couldn't find a satisfying solution. Many graph algorithms are aiming to find the shortest path (in a sense of steps or cost), but not to find the "matched" cost path.
A naive solution would be finding all paths from s to t, compute sum of weights for each path and select the one that is close to P. However, finding all paths between two nodes in a graph is known to be #P-hard.
A possible solution could be modified the A* algorithm, so that for each node in the frontier we get the cost from the root to that node (g), and estimate the cost from that node to the goal (h). Then instead of choosing a node with the smallest g+h, we choose a node with the smallest |P - (g+h)|. However, I am not sure if this is the best solution.
Another thought is inspired from the linear programming since the objective function of this problem is sum(weights of a path from s to t) - P = 0. I know the shortest path problem can be formed as a linear programming task but not sure how to formulate this problem as a one.
Please help, thanks in advance!
This problem is NP-hard via a reduction from the Hamiltonian path problem. In that problem, you are given a graph and a pair of nodes s and t and are asked whether there's a simple path from s to t that passes through all the nodes in the graph. You can solve the Hamiltonian path problem via your problem as follows:
Assign each edge in the graph weight 1.
Find the s-t simple path whose weight is as close to n-1 as possible, where n is the number of nodes in the graph.
Returns whether this path has cost exactly n-1.
If the graph has a Hamiltonian path, then that path will have cost n-1. Otherwise, it doesn't, and the best path found will have a cost that's lower than n-1.

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.

Shortest path visiting set of vertices in an un directed graph

I have an undirected graph of locations in an indoor map.
When given a set of vertices , i want to find the shortest path which will cover all those vertices.
Graph contains 52 vertices and 150 - 250 Edges.
What is the best algorithm i can use to find the shortest path.
Please don't get confused that this is a Travelling Salesman Problem. It doesn't have to cover all the nodes.Only the given set of nodes.
As i've commented, this is a hard problem, so don't expect a polynomial time algorithm.
But if you're looking for an algorithm that you may be able to compute in acceptable time for the problem instances you mentioned, this might work:
Let G(V,E) be the original graph, let N be the set of nodes that must be visited.
1. Compute the shortest-path matrix M for the entire graph (|V|x|V| matrix that contains
the length of the shortest path between each two nodes).
2. Generate a new graph G`, containing N alone, with the distances between each
two nodes taken from the shortest-path matrix M.
3. Solve the Minimum Weight Hamiltonian Path Problem on G`.
Note that the "hardest" part here is the third part, which takes exponential time. but if the group N is not too big, you'll be able to solve it:
Bruteforce algorithm will let you solve problems where N contains about 11 nodes within seconds ( O(|N|!) complexity )
Dynamic Programming will let you solve problems where N contains about 20 nodes within seconds ( O(2^|N|*|N|^2) complexity.
You can basically apply any algorithm that solves the Minimum Weight Hamiltonian Path Problem to the third part, these algorithms are usually equivalent to TSP algorithms (the only difference between these problems is that in the TSP you return to the source node after visiting all the other nodes).

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.

Resources