I know that Bellman-Ford Algorithm works for directed graphs. Will it will work for an undirected graph? It seems that with an undirected graph, it will not be able to detect cycles because parallel edges will be considered cycles. Is this true or not? Can the algorithm be applied?
3 Answers
As a matter of fact any undirected graph is also a directed graph.
You just have to specify any edges {u, v} twice (u, v) and (v, u).
But don't forget, that this also means any edge with a negative weight will count as a loop. As the Bellman-Ford algorithm ONLY works on graphs that don't contain any cycles with negative weights this actually means your un-directed graph mustn't contain any edges with negative weight.
If it doesn't its pretty fine to use Bellmann-Ford.

- 10,077
- 1
- 40
- 41
-
20To elaborate on this - since the graph has to have only nonnegative edges, this means that you might want to just use Dijkstra's algorithm instead, since it's asymptotically faster. – templatetypedef Feb 11 '13 at 01:03
-
I has the same doubt. Thank you for clarification. – whitehat Jul 15 '15 at 19:24
-
nice answer, thanks for clarity – Guru Dec 23 '21 at 05:44
Bellman Ford Algorithm Does not work on undirected graph with negative weight, because an undirected edge {u, v} with negative weight can be defined as 2 directed edge, (u, v) and (v, u) with negative weigh, which would be picked up as a negative cycle by Bellman Ford Algorithm.
Therefore, Bellman Ford would only work on positively weighted undirected graph. However in that case, it is preferred to use Dijkstra's algorithm instead as it is asymptotically faster.

- 43
- 6
Bellman-Ford is not applicable to find shortest paths on graphs which contain negative cycles, but it finds shortest paths on graphs and can detect if the graph contains a negative cycle, although it won't find a shortest path, as no such path exists.
-
1Expecting bad output because of bad input does not make the algorithm work for the problem. I feel like mikyra's answer has already correctly summarized the situation. – Ironcache Apr 25 '18 at 12:58