0
$\begingroup$

I am reading about algorithms to find the shortest path on a graph with one source, and I have a doubt about Dijkstra's algorithm about the negative weights on edges. In this case is Bellman-Ford algorithm a better solution for this part?

asked Jan 30, 2023 at 19:50
$\endgroup$
1

1 Answer 1

1
$\begingroup$

Dijkstra's algorithm might solve some cases where there are negative edges in the graph. However, it doesn't solve for every graph that has a negative edge. For instance, if you have a negative cycle, Dijkstra won't work.

For graphs with negative weighted edges, you should use Bellman-Ford's algorithm.

If you want to find the shortest path from one source, then use Bellman-Ford on graphs with negative edges, and Dijkstra on graphs with non-negative edges.

If you want to find the shortest path for every pair of vertices $(u, v)$, then you can use Floyd-Warshall algorithm

I will leave you to determine, whether on graphs with non-negative edges, whether it is more efficient to run Dijkstra $n$ times where $n = |V|$, or to run Floyd-Warshall.

answered Feb 5, 2023 at 21:47
$\endgroup$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.