GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Jan-2009, 17:09
biancareanys biancareanys is offline
New Member
 
Join Date: Jan 2009
Posts: 2
biancareanys is on a distinguished road
Question

Algorithm: shortest paths to every vertex of G


Can you tell me some ideas for an efficient algorithm on this problem?

We have a directed graph and a function of weight w we know that there is only one edge with a negative weight and that there is no cycle in the graph with negative weight we have s a vertex
We have to find an algorithm that find the length of all the shortest paths from s to every vertex of G
  #2  
Old 04-Jan-2009, 08:51
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Algorithm: shortest paths to every vertex of G


Dijkstra's algorithm doesn't allow negative-weight edges, but you can still apply it to this problem. Let (i, j) be the negative-weight edge. Then:
  1. remove edge (i, j) from the graph.
  2. Run Dikstra's algorithm using s as the source.
  3. Run Dikstra's algorithm using j as the source.
  4. For each vertex v, there are two possibilities:
    1. The shortest path from s to v does not use edge (i,j):
      In this case, the results from step 2 give you the shortest path.
    2. The shortest path from s to v goes through edge (i, j)
      In this case, the shortest path is the shortest path from s to i (obtained in step 2) plus edge (i, j) plus the shortest path from j to v (obtained in step 3).

Thus you need to check both possibilities for every vertex and see which is better.

This is the fastest way I can think of off the top of my head. Bellman-Ford allows for negative-weight edges, but it's not as efficient as Dikstra.
__________________
www.blake-foster.com
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Shortest Path for Airport mxb1145 C++ Forum 1 16-Dec-2008 07:28
Need Help - Shortest Path Problem mas287 C Programming Language 5 18-Jun-2008 03:59
Shortest Path Program. bomber456 C++ Forum 1 05-Mar-2007 13:32
all shortest paths with dijkstra cpit C++ Forum 0 05-Jun-2006 11:07
shorthest path-another way Pandiani C++ Forum 6 09-May-2004 20:51

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:00.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.