Find the shortest path between a given source and a set of destinations - algorithm

You are given a weighted connected graph (20 nodes) with all edges having positive weight. We have a robot that starts at point A and it must pass at points B, D and E for example. The idea is to find the shortest path that connects all these 4 points. The robot also has a limited battery, but it can be recharged in some points.
After researching on the internet I have two algorithms in mind: Dijkstra's and TSP. Dijkstra's will find the shortest path between a node and every other node and TSP will find the shortest path that connects all points. Is there any variant of the TSP that only finds the shortest path between a set of nodes? After all, in the TSP all nodes are labeled "must-pass". I'm still not taking in account the battery constraint.
Thanks in advance!

You can reduce your graph to a TSP and then invoke a TSP algorithm on it:
Use Floyd-Warshall algorithm to find the distance u,v for ALL pairs of vertices u and v.
Create a new graph, containing only the "desired" vertices, and set the weight between two such vertices u and v as the distance found by Floyd-Warshall.
Run TSP Solver on the modified graph to get the path in the modified graph, and switch each edge in the modified graph with a shortest path from the original graph.
The above is optimal, because assume there is a shorter path.
D0=u->...D1->...->D2->...->Dk->...->t=D{k+1}
Di->...->D{i+1} has at least the weight of FloydWarshall(Di,D{i+1}) (correctness of Floyd-Warshall), and thus the edges (D0,D1),(D1,D2),...,(Dk,D{k+1) exist in the modified graph with a weight smaller/equal the weight in the given path.
Thus, from correctness of your TSP-Solver, by using D0->D1->...->Dk->D{k+1}, you get a path that is at least as good as the candidate optimal path.

You might also want to look into the generalized traveling salesman problem (GTSP): The nodes are partitioned into subsets, and the problem is to find the minimum-length route that visits exactly one node in each subset. The model is allowed to choose whichever node it wants from each subset. If there are nodes that must be visited, you can put them in a subset all by themselves.

Related

Given undirected weighted connected graph, s,t. Find path from s to t that its most weighted edge is low as possible

Given: undirected weighted connected graph. s,t are vertices.
Question: Find an algorithm as efficient as possible that returns a path from s to t. In that path, the edge that has the highest weight, will has the least weight as possible. So if we have 5 paths from s,t and for every path we have the heaviest edge, so the minimum edge of these 5.
What I've tried:
Use some algorithm to find the shortest path between s and t.
Delete all the edges that are not part of the shortest paths we've found
Use BFS with some modification, We run BFS depending on the number of paths from s to t. Every time we find a maximum edge and store it in an array, then we find the minimum of the array.
I'm struggling to find an algorithm that can be ran in (1), Bellman ford won't work - because it has to be directed graph. Dijkstra won't work because we don't know if it has negative circles or negative edges. And Prim is for finding MST which I'm not aware of how it can help us in finding the shortest path. Any ideas?
And other from that, If you have an algorithm in mind that can solve this question, would be much appreciated.
You can solve this with Kruskal's algorithm. Add edges as usual and stop as soon as s and t are in the same cluster.
The idea is that at each stage of the algorithm we have effectively added all edges below a certain weight threshold. Therefore, if s and t are in the same cluster then there is a route between them consisting entirely of edges with weight less than the threshold.
You can solve it by converting into a MST problem, basically the path from s to t in the MST would be the path which has the least possible maximum weight
find the most negative edge in the graph
add that (weight+1) to every edge.
Now all edge are positive so you can apply Dijkstra's algorithm
you can get the shortest_path between source and destination
Now count the number of edges between source and destination (say x)
Real shortest path will be: shortest_path - x * (weight+1)

Shortest-path algorithm: multiple source, closest destination

Algorithms like the Bellman-Ford algorithm and Dijkstra's algorithm exist to find the shortest path from a single starting vertex on a graph to every other vertex. Their multiple source version can be achieved by reversing all the edges and treating destination as start node.
I'd like to extend that to find the "barycentre" of the sources on the graph, ie the vertex that is "closest" to a set of sources, finding "fair" paths to a "consensual" vertex.
Are there algorithms already providing this? What are they?
Floyd–Warshall algorithm
I think you want to calculate the "Graph Eccentricity" of the sources (S1,S2,...Sn-1,Sn).
Use Floyd-Warshall algorithm to calculate All Pair of Shortest Path in graph.
Find the result node V in graph, which is the min sum of (d[v,S1]+d[v,S2]+d[v,S3]....d[v,Sn-1]+d[v,Sn])
More Information:
Graph Eccentricity
UPDATE
Maybe finding an existed node v In Graph G(V,E) which the distance to S are all equal is not realistic. You can calculate the Stand Deviation of (d[v,S1],d[v,S2],d[v,S3]....d[v,Sn-1],d[v,Sn]) between a range that grater or equal than 0 and less than a certain value you choose.

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.

TSP-Variant, possible algorithm?

One of the classical Travelling Salesman Problem (TSP) definitions is:
Given a weighted complete undirected graph where triangle inequality holds return an Hamiltonian path of minimal total weight.
In my case I do not want an Hamiltonian path, I need a path between two well known vertexes. So the formulation would be:
Given a weighted complete undirected graph where triangle inequality holds and two special vertexes called source and destination return a minimal weighted path that visits all nodes exactly once and starts from the source and ends to the destination.
I recall that an Hamiltonian path is a path in an undirected graph that visits each vertex exactly once.
For the original problem a good approximation (at worse 3/2 of the best solution) is the Christodes' algorithm, it is possible to modify for my case? Or you know another way?
Add an edge (= road) from your destination node to your source node with cost 0 and you've got a TSP (for which the triangle inequality doesn't hold though).
The book "In pursuit of the Traveling Salesman" briefly mentions this technique.
Why don't you use dijkstra's algorithm with extra book keeping on each node for the path information. i.e., the list of vertices passed in the shortest path to that particular vertex from the source.
And stop when you reach your end vertex. Then your path will be
Path to the starting vertex of the current edge + current edge.
where current edge is the last edge which lead you to your destination.
You can alter a TSP algorithm to make sure you use the edge between start and finish. Then simply remove the edge from the TSP result and you get your path.
In other words, if you add the edge from start to finish to your path, you get a TSP solution, but not necessary the optimal one.
In greedy algorithm, you have a sorted list of all edges L and an empty list I. You keep adding the shortest edge that does not form a cycle until you pass all vertexes. Simply remove the edge from start to finish from L and add it to I before you add the rest of the edges.
In nearest neighbor, you start from one vertex, then add the shortest edge that starts at that vertex and leads to any vertex that has not been visited. Mark your finish as visited, then start from your start vertex and add the edge between the last vertex in the resulted path and the finish and you got your path from start to finish.
There are other TSP algorithms that can be altered to provide this path.

Traveling Saleperson Variation, Plotting a travel itinery

Im looking for the name of a problem and an algortihm for its solution.
I have a graph of connected nodes (A..Z) where every node is connected to every other node. I would like to plot the shortest path through these nodes that visits a given subset of nodes (A,D,K,W). The path may include nodes not in the subset ie A->C->W->D->K is acceptable. The cost of traveling between nodes is non negative, but not necessarily linear. Thus a path segment from A->B->C may be 'shorter' than A->C
I assume it is a variation of Traveling salesperson.
I don't know if there is a special name for this problem, but it is easily reduced to the original traveling salesman problem for the selected nodes.
Let the set of all nodes be V and the selected ones W. I would start by collapsing the nodes not in the W to get a multigraph (like a graph, but can have multiple edges between the same pair of nodes). Each edge here may be a simple one or a sequence of edges and nodes from V\W. To reduce it to a regular graph, we have only to pick the shortest of the edges available for each pair of nodes, since any other would clearly not be a part of the answer. Now we have to solve the resulting traveling salesman problem for the reduced graph and then reconstruct the corresponding path in the original graph -- we have written down the actual path in the original graph each edge in the reduced graph corresponds to.

Resources