TSP-Variant, possible algorithm? - 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.

Related

most lightweight circle in directed graph that goes through specific vertex

I have directed Graph G(V,E) with weight function w. so that weight of each (u,v) is a positive value. I need to find the most lightweight circle in the graph that vertex k' is part of it.
I've also given an algorithm i can use which can find the most lightweight path for a graph with positives weights ( i can use it only once).
I thought about creating a sub graph G' where all vertices and edges that are strongly connected components. find the graph which k' is part of it. then find for the most lightweight adjacent edge from k' to some v of vertices. from that v i can run the algorithm given and find the lightweight path then add the weight of the vertex missing ( (k',v) ).
is that seems correct ? I'm in the beginning of this course and I feel i'm not there yet.
It is a single-source shortest-path problem, where you exclude k->k self-loop as a solution, and find a longer path from k to k. The trick is always expand the shortest path thread.
Given this definition, you can start Googling...
I can't imagine why you called your source vertex k'. Anyway...
Add a new vetrex w that has the same outgoing edges as k'.
Then use Dijkstra's algorithm to find the shortest path from w to k'.
Substitute k' for w in the path, and you have the smallest cycle including k'.
Very interesting problem. I am assuming that there are no negative values in the graph, or otherwise the following solutions requires first normalizing the vertices such that the negative values become at least 0. First method (trivial) is to detect all cycles starting from the target vertex (k). Then compute the weight of all those cycles and take the minimum. The second method is to run Dijkstra algorithm (again watch out negative weights) from the target node (k). Then iterate over all incoming edges of (k), and select the source node that has the minimum Dijkstra value. Now the lightest cycle includes the single path (formed by Dijkstra traversal) from (k) to the chosen node + the bridge to come back to (k). I hope that helps :)

Modified Dijkstra's Algorithm

We are given a directed graph with edge weights W lying between 0 and 1. Cost of a path from source to target node is the product of the weights of edges lying on the path from source to target node. I wanted to know of an algorithm which can find the minimum cost path in polynomial time or using any other heuristic.
I thought along the lines of taking the log values of the edges weights (taking mod values) and then applying dijkstra for this graph but think there will be precision problems which can't be calculated.
Is there any other better way or can I improve upon the log approach.
In Dijkstra's algorithm, when you visit a node you know that there is no shorter road to this node. This is not true if you multiply the edges with weights between 0..1 as if you visit more vertices you will get a smaller number.
Basically this is equivalent of finding the longest path in a graph. This can be seen also by using your idea of taking logarithms, as the logarithm of a number between 0 and 1 is negative. If you take absolute values of the logarithms of the weights, the longest path corresponds to the shortest path in the multiplicative graph.
If your graph is acyclic there is a straightforward algorithm (modified from Longest path problem).
Find a Topological ordering of the DAG.
For each vertex you need to store the cost of path. Initialize this to one at the beginning.
Travel through the DAG in topological order starting from your start vertex. In each vertex check all the children and if the cost is smaller than previously, update it. Store also the vertex where you arrive at this vertex with the lowest cost.
After you reach your final vertex, you can find the "shortest" path by travelling back from the end vertex using the stored vertices.
Of course, if you graph is not acyclic you can always reach a zero end cost by repeating a loop infinitely.

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

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.

Hamilton path finding with the use of hamilton cycle function and the opposite task

The problem is testing whether a graph G contains a Hamiltonian path or not with the one use of hamiltonian cycle Hcycle(V,E) function which gives output true of false whether the G contains Hamiltonian cycle.
I must write a program with polynomial time complexity, which has to decide whether the unoriented graph G contains at least one Hamiltonian path with the use of one Hamiltonian Cycle function which has to give output to this problem.
Also I need to write a program with the opposite problem. (use of Hpath function to find out whether the graph contains Hemiltonian Cycle).
I can't find a solution to this problem.
I can use both Hcycle and Hpath only once.
We assume that the function Hcycle and Hpath run in linear time complexity.
Path by Hcycle(V,E): Call Hcycle() on a graph created by adding one vertex that is connected to all other vertices. If new graph has a cycle than removing new node from that cycle we get path on original graph.
Cycle by Hpath(V,E): Call Hpath() on a graph created by adding one vertex and connecting it to same neighbours as one existing vertex. That means these 2 vertices will have same naighbours. If new graph has a path than at least one path end is on these two vertices. If other vertex is not end, than it is on path third position and by reordering path we can set both vertices to be path end points. Merging these two vertices (since they have same neighbours) we get a cycle in original graph.
Path by Hcycle(V,E): If graph has cycle than it has path also. If graph doesn't have cycle than for each unconnected pair of vertices (v1, v2) add edge between them and check is there a cycle with Hcycle(V,E+(v1,v2)). If there is a cycle than there is a path between v1 and v2 in original graph. Hcycle is called max |V|^2 times.
Cycle by Hpath(V,E): Idea is to force a path to has end vertices we know about. That can be done by constructing graph where two vertices have degree one. Let N(v) be neighbours of v. For an edge (v1,v2) and n1 from N(v1)-v2 and n2 from N(v2)-v1 construct a graph by removing all edges from v1 except to n1, and removing all edges from v2 except to n2. If that graph has a path, than it's ends are on v1 and v2, and our original graph has a circle. Hpath is called max |E|*|V|^2 times.
Every hamiltonian cycle is a hamiltonian path, just break the cycle somewhere.
The other way around doesn't work quite easily. The brute-force solution is:
for all hamiltonian paths p, check whether there is edge between start and end of p, if there is, make a cycle. However if HPath returns just some path, there is no way to make a cycle out of it (I guess).
Check Wikipedia

Difference between hamiltonian path and euler path

Can some one tell me the difference between hamiltonian path and euler path. They seem similar!
An Euler path is a path that passes through every edge exactly once. If it ends at the initial vertex then it is an Euler cycle.
A Hamiltonian path is a path that passes through every vertex exactly once (NOT every edge). If it ends at the initial vertex then it is a Hamiltonian cycle.
In an Euler path you might pass through a vertex more than once.
In a Hamiltonian path you may not pass through all edges.
Graph Theory Definitions
(In descending order of generality)
Walk: a sequence of edges where the end of one edge marks the beginning of the next edge
Trail: a walk which does not repeat any edges. All trails are walks.
Path: a walk where each vertex is traversed at most once.
(paths used to refer to open walks, the definition has changed now) The property of traversing vertices at most once means that edges are also crossed at most once, hence all paths are trails.
Hamiltonian paths & Eulerian trails
Hamiltonian path: visits every vertex in the graph (exactly once, because it is a path)
Eulerian trail: visits every edge in the graph exactly once (because it is a trail, vertices may well be crossed more than once.)
Eulerian path must visit each edge exactly once, while Hamiltonian path must visit each vertex exactly once.
They are related but are neither dependent nor mutually exclusive. If a graph has an Eurler cycle, it may or may not also have a Hamiltonian cyle and vice versa.
Euler cycles visit every edge in the graph exactly once. If there are vertices in the graph with more than two edges, then by definition, the cycle will pass through those vertices more than once. As a result, vertices can be repeated but edges cannot.
Hamiltonian cycles visit every vertex in the graph exactly once (similar to the travelling salesman problem). As a result, neither edges nor vertices can be repeated.
I'll use a common example in biology; reconstructing a genome by making DNA samples.
De-novo assembly
To construct a genome from short reads, it's necessary to construct a graph of those reads. We do it by breaking the reads into k-mers and assemble them into a graph.
We can reconstruct the genome by visiting each node once as in the diagram. This is known as Hamiltonian path.
Unfortunately, constructing such path is NP-hard. It's not possible to derive an efficient algorithm for solving it. Instead, in bioinformatics we construct a Eulerian cycle where an edge represents an overlap.
A Hamiltonian path visits every node (or vertex) exactly once, and a Eulerian path traverses every edge exactly once.
Euler Path - An Euler path is a path in which each edge is traversed exactly once.
Hamiltonian Path - An Hamiltonian path is path in which each vertex is traversed exactly once.
If you have ever confusion remember E - Euler E - Edge.
An Euler path is a path that uses every edge of a
graph exactly once.and it must have exactly two odd vertices.the path starts and ends at different vertex. A Hamiltonian cycle is a cycle that contains every vertex of the graph hence you may not use all the edges of the graph.
Euler path is a graph using every edge(NOTE) of the graph exactly once. Euler circuit is a euler path that returns to it starting point after covering all edges.
While hamilton path is a graph that covers all vertex(NOTE) exactly once. When this path returns to its starting point than this path is called hamilton circuit.

Resources