bellman ford only detects negative cycle, its not what I need. I need to FIND a simple path
Main Topics
Browse All TopicsI'm looking for an algorithm for the single pair shortest path with negative weights problem. Does anyone knows any?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Use the Bellman-Ford algorithm to detect negative cycles. If there are any and your graph is connected, there is no shortest path as you could find a shorter path using the cycle one more time...
If there are none, your weight function c is called 'conservative' and you can use the Moore-Bellman-Ford algorithm. It works like this:
Let s be your starting knode and n the number of knodes.
1: Set l(s):=0, l(v)=infty for all v in V(G)\{s}.
2: For i=1 to n do
for every edge (v,w) in E(G) do
if l(w) > l(v) + c(v,w) then set
l(w):=l(v)+c(v,w) and p(w):=r
That should give you l(v) which is the length of the shortest path from s to v, and the path v,p(v),p(p(v)),p(p(p(v))),
doggz
I think this is what you are looking for, firstly +ve weights then -ve weights
The Single Source Shortest Path Problem +ve weights
The input to this problem is a directed positive edge weighted graph with a designated vertex s. The problem is to find the shortest simple path from s to each other vertex. For more information see section 4.2 of the text.
Here feasible solutions are simple paths that start from the source vertex s. The nodes in level k of the tree represent all paths from s of k or less hops. Note that by simplicity we we need only consider the tree to depth n, the number of vertices.
The pruning rule is that we need only remember the shortest path to a particular vertex. We thus get the following code. Here D[k,i] is the shortest path from s to i of k or less hops.
For k= 1 to n do
For i= 1 to n do
D[k, i] = min (D[k-1, i], D[k, i])
for each edge e = (i, j) do
D[k, j]=min( D[k, j], D[k -1, i] + the length of e )
This is Bellman-Ford shortest path algorithm and has running time O(VE).
and now for -ve weights
The Shortest Path Problem with Negative Edge Weights
The input to this problem is a directed edge weighted graph with a designated vertex s. The edge weights may be positive or negative. The problem is to find the shortest simple path from s to each other vertex. For more information see section 4.2 of the text.
Here feasible solutions are simple paths that start from the source vertex s. The nodes in level k of the tree represent all paths from s of k or less hops. Note that by simplicity we we need only consider the tree to depth n, the number of vertices.
The pruning rule is that if two paths end at the same vertex and contain the same vertices then we may prune the shorter one. Make sure you understand why the pruning rule that we used for positive weights does not work here.
We thus get the following code. Here D[k, S, i] is the shortest path from s to i of k or less hops that visits exactly the vertices in S.
For k= 1 to n do
For i= 1 to n do
For S= 1 to 2^n do
D[k, S, i] = min (D[k-1, S, i], D[k, S, i])
for each edge e = (i, j) do
if j is not in S then
D[k, S+ j, j]=min( D[k, S+j, j], D[k, S, i] + the length of e )
The running time of this code is O(VE2V), where V is the number of vertices and E is the number of edges.
doggz
I though about this some more. Surely just add k=-(min weight w(e), e in G) to all weights so they are now all positive, now find the shortest P path using a positive weight algorithm (eg Dykstra etc), it will be the same path as the original problem. To get the weight of the actual path subtract k*(number of edges in the path) ie w(P)-k|P|
GwynforWeb
doggz
To explain how the algorithm works requires diagrams to do it justice. If you understand Dijkstra algorithm then understanding this is not difficult as it invloves many of the same concepts. The web abounds with examples eg http://www.cs.uiowa.edu/~h
If you are looking for Hamiltonian cycles then the complexity goes up massively both in the nature of the algorithms and in the number of operations. As far as we know the finding of a Hamiltonian cycle is NP incomplete ie just as bad as finding an optimal cycle. There has been work done in finding optimal cycles for specific types of graphs eg bipartite, 3-regular etc., but this is still an area of research in graph theory. There are many people working in this area, because of this my experience of graph theory is that if you have a good idea it has probably been thought of before.
Business Accounts
Answer for Membership
by: ozoPosted on 2002-10-10 at 16:14:12ID: 7324958
There's the Bellman-Ford algorithm.