What is the difference between Travelling Salesman and finding Shortest Path? - algorithm

The only difference I could think of for the question is that in the Travelling Salesman Problem (TSP) I need to find a
minimum permutation of all the vertices in the graph and in Shortest Paths problem there is no need to consider all the vertices we can search the states space for minimum path length routes can anyone suggest more differences.

You've already called out the essential difference: the TSP is to find a path that contains a permutation of every node in the graph, while in the shortest path problem, any given shortest path may, and often does, contain a proper subset of the nodes in the graph.
Other differences include:
The TSP solution requires its answer to be a cycle.
The TSP solution will necessarily repeat a node in its path, while a shortest path will not (unless one is looking for shortest path from a node to itself).
TSP is an NP-complete problem and shortest path is known polynomial-time.
If you are looking for a precise statement of the difference I would say you just need to replace your idea of the "permuation" with the more technical and precise term "simple cycle visiting every node in the graph", or better, "Hamilton cycle":
The TSP requires one to find the simple cycle covering every node in the graph with the smallest weight (alternatively, the Hamilton cycle with the least weight). The Shortest Path problem requires one to find the path between two given nodes with the smallest weight. Shortest paths need not be Hamiltonian, nor do they need to be cycles.

With the shortest path problem you consider paths between two nodes. With the TSP you consider paths between all node. This makes the latter much more difficult.
Consider two paths between nodes A and B. One over D the other one of C. Let the one over C be the longer path. In the Shortest Path problem this path can get immediately discarded. In the TSP it is perfectly possible that this path is part of the over all solution, because you'll have to visit C and visiting it later might be even more expensive.
Therefor you can't break down the TSP in similar but smaller sub-problems.

Shortest path is just have source and target.we need to find shortest path between them obviously output must be tree in polynomial-time.
Finding Shortest Path:-
Undirected graphs:
Dijkstra's algorithm with list O(V^2)
Directed graphs with arbitrary weights without negative cycles:
Bellman–Ford algorithm Time complexity O(VE)
Floyd–Warshall's Algorithm is used to find the shortest paths between between all pairs
TSP (Travelling-Salesman Problem) is not like that we have cover every node from source and finally we've reach source at minimum cost.Eventually there must be cycle. TSP is an NP-complete problem
Ref:
https://en.wikipedia.org/wiki/Shortest_path_problem
https://en.wikipedia.org/wiki/Travelling_salesman_problem
http://www.geeksforgeeks.org/travelling-salesman-problem-set-1/
http://www.geeksforgeeks.org/greedy-algorithms-set-6-dijkstras-shortest-path-algorithm/
https://www.hackerearth.com/practice/algorithms/graphs/shortest-path-algorithms/tutorial/

In TSP, you need to both visit all nodes and also return to your starting point. This complicates the problem immensely.

Related

Question about single source longest path in a DAG

If I am right, the problem of single source longest path for any general graph is NP-hard.
Can we negate all the edge weights of a DAG and run Dijstra's or Bellman Ford algorithm to get single source longest path?
If yes, Dijstra's can't run on a graph with negative edge weights, then how come it gives us a result will all negative edge weights?
Yes, you are right. Your proposed solution can be used to find a longest path in a DAG.
However, notice that the longest path problem for a DAG is not NP-hard. It is NP-hard for the general case where the graph might not be a DAG. There is some interesting, relevant discussion on Wikipedia's article on the Longest path problem.
As for your second question, Dijkstra's algorithm is defined for non-negative edge weights. The implementation might give you a result anyway, but it is no longer guaranteed to be correct.

Shortest path between two nodes vs shortest path from one node to all other nodes

I'm currently studying shortest path problems in graphs with non-negative edge weight.
I know that Dijkstra algorithm can give me the solution to the single-source shortest path problem ie one can find the shortest path from one node to all other nodes but I haven't found algorithm that can give me the solution to a a priori simpler problem : find the shortest path between two nodes.
Intuitively, I think that one can find examples that show that the "simpler" problem isn't simpler than the single-source shortest path problem but I'm looking for references that show this contradiction (a priori) on simple (ie with a few number of nodes) graphs.
Some (but not all) single-source shortest paths algorithms can be easily modified to return the shortest path between two nodes by stopping the algorithm early. A simple example of this is breadth-first search in unweighted graphs. For example, to find the shortest path from a node u to a node v, start a BFS from u. As soon as v is found, the path from u to v discovered that way is the shortest path. Dijkstra’s algorithm can also do this: if you run a Dijkstra’s algorithm starting at node u, you can stop the algorithm as soon as you dequeue v from the priority queue to get the shortest path there.
These approaches are usually faster than running the whole algorithm to completion. But if you’re interested specifically in finding a path from one node to another, you might want to look at the A* search algorithm. This is a modification of Dijkstra’s algorithm that’s specifically optimized for the problem of getting from one node to another. It uses a heuristic to guide the search toward the target, deprioritizing searching for other nodes, and thus is much faster than Dijkstra’s algorithm in general.
As a note, not all shortest paths algorithms can be cut off early this way. For example, when there are negative edge weights but no negative cycles, Bellman-Ford can still compute shortest paths. However, it may continually revise node distances as it runs, up to the last round of the algorithm. Cutting the search off early can give back the wrong answer.

need to detect a ending path in the graph with minimilistic weight

Would like to find the shortest weighted path from the node a to any node. The destination node is not given. One can visit any vertex many time.
if the path weight's should be less than Integer.MAX
what is the algorithm to proceed ..?? cant detect the algorithm itself.
i did try for travelling salesman problem but it doesn't match ; neither it match for Dijkstra ...
how to keep in memory all the paths is the major challenge here ..
Edit: Graph is not directed one and there are no negative wieghts.
references: http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare
http://en.wikipedia.org/wiki/Travelling_salesman_problem
For an undirected graph with no negative weights, it's possible to use Floyd-Warshall algorithm to find all-pairs shortest path with time complexity of O(V^3), where V is a number of vertices. The algorithm also allows an easy way to reconstruct the all computed shortest paths. It uses O(V^2) memory to store distances and additional information for reconstructing the path.
There are also some other algorithms for solving this problem with better time complexity, but Floyd-Warshall is really easy to code and start with.
Would like to find the shortest weighted path from the node a to any
node.
I immediately think Dijkstra's algorithm, because that's exactly what it does.
how to keep in memory all the paths is the major challenge here ..
For each node in the graph, keep track of it's previous node that would result in the shortest path. Take a look at the pseudocode, line 20.
To build the path, simply go backwards from the destination node until you reach the source node.

NP complete Theory: Long Path

The question is basically to show that for any unweighted graph G(V,E), if we could find a simple path as big as floor(|V|/2), we could compute hamiltonian paths.
Basically that Hamiltonian Paths are polynomial time reducible to the long path problem.
I have tried to find a graph in which a path of size |v|/2 would map to another graph's Hamiltonian Path. Yet I have not gotten anywhere with that approach.
Maybe there is a way to prove there is a finite number of paths grater than length |V|/2 for any graph, which would mean we could just repeat our long path algorithm several times to find our Hamiltonian Paths. But I am not sure of this.
As a hint, suppose that you want to find a Hamiltonian path in a graph with n nodes. What happens if you create a new graph that's two independent copies of the original graph and ask whether that new graph has a Hamiltonian path going through n nodes?
Hope this helps!

Why cannot we turn longest path into shortest graph?

Today I read on Introduction to Algorithms of the longest path problem, which asked in a weighted, directed graph what is the longest simple path passing two vertices. The author used a wonderful example showing that dynamic programming fails for the longest path problem because there does not exist a nice optimal structure that always comes with an optimal substructure. It was commented that this problem is actually NP-complete. So it must be really hard.
Now here is my question: Instead of assign every edge a positive weight k>0, what if we simply assign negative weights to each edge with weight-k? Then each "longest path" would automatically be the shortest path, and if there is no loops in the shortest path by definition, there should not be any loops in the corresponding longest path. Hence using a quite common trick we "can" turn the longest path problem into the shortest path problem.
Can someone point out my mistake in the reasoning? I figure something must be wrong but do not really know what it is. It is extremely unlikely my argument is correct, for obvious reasons. Reading the pseudo code provided in the book, it seems the algorithm for shortest path does not prohibit using negative weights, and after all everything can be "elevated" by adding a large enough constant to make them positive. I am a beginner in algorithms so this question might be trivial to experts.
The mistake in your reasoning is this line:
if there is no loops in the shortest path by definition, there should not be any loops in the corresponding longest path.
If you have a graph where all edges have negative weight (which can happen in the transformation you're describing) and there's a negative cycle, then there is no shortest path between any two nodes on that cycle, since any path between them can have its cost reduced by following the cycle more and more times. Since there is no shortest path in that case, your reasoning breaks down.
Now, you can argue that you should instead look for the shortest simple path between the nodes (that is, a path with no duplicated edges). Unfortunately, though, that problem is also NP-hard, so this reduction doesn't actually buy you anything.
Hope this helps!
if there is no loops in the shortest path by definition, there should
not be any loops in the corresponding longest path
...and so that particular original problem is solvable in polynomial time. And if the particular original problem is a DAG then this method solves it in linear time.
The complexity statement doesn't say that all graphs are that hard to solve, just that some are.
From your own link:
In contrast to the shortest path problem, which can be solved in
polynomial time in graphs without negative-weight cycles, the longest
path problem is NP-hard, meaning that it cannot be solved in
polynomial time for arbitrary graphs unless P = NP.
[...]
For most graphs, this transformation is not useful because it creates
cycles of negative length in −G. But if G is a directed acyclic graph,
then no negative cycles can be created, and longest paths in G can be
found in linear time by applying a linear time algorithm for shortest
paths in −G, which is also a directed acyclic graph.

Resources