views:

827

answers:

2

So, I understand the problem of finding the longest simple path in a graph is NP-hard, since you could then easily solve the Hamiltonian circuit problem by setting edge weights to 1 and seeing if the length of the longest simple path equals the number of edges.

My question is: What kind of path would you get if you took a graph, found the maximum edge weight, m, replaced each edge weight w with m - w, and ran a standard shortest path algorithm on that? It's clearly not the longest simple path, since if it were, then NP = P, and I think the proof for something like that would be a bit more complicated =P.

+1  A: 

If you could solve shortest path problems with negative weights you would find a longest path, the two are equivalent, this could be done by putting a weight of -w instead of w

The standard algorithm for negative weights is the Bellman-Ford algorithm. However the algorithm will not work if there is a cycle in the graph such that the sum of edges is negative. In the graph that you create, all such cycles have negative sum weights and so the algorithm won't work. Unless of course you have no cycles, in which case you have a tree (or a forest) and the problem is solvable via dynamic programming.

If we replace a weight of w by m-w, which guarantees that all weights will be positive, then the shortest path can be found via standard algorithms. If the shortest path P in this graph has k edges then the length is k*m-w(P) where w(P) is the length of the path with the original weights. This path is not necessarily the longest one, however, of all paths with k edges, P is the longest one.

Pall Melsted
Ah, I set the new weight to `m-w`, not `-w`. The smallest weight will be 0.
Claudiu
"Of all paths of length k, P is the longest one." - Wouldn't all paths of length k have equal length?
Claudiu
I was using "length" in two senses, the number of edges and the sum of the weights. I've clarified this in the edit.
Pall Melsted
+1  A: 
kunjaan
@kunjaan I'm sorry, but it seems the pic you attached does not work now. Maybe you removed it from your dropbox?
ibread