Can we apply the Bellman-Ford algorithm to an Undirected Graph?

anuj pradhan picture anuj pradhan · Feb 9, 2013 · Viewed 15.5k times · Source

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?

Answer

mikyra picture mikyra · Feb 11, 2013

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.