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

20,365

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.

Share:
20,365
anuj pradhan
Author by

anuj pradhan

a hardworking person...like to do new things...

Updated on March 27, 2021

Comments

  • anuj pradhan
    anuj pradhan over 3 years

    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?

  • templatetypedef
    templatetypedef over 11 years
    To 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.
  • whitehat
    whitehat almost 9 years
    I has the same doubt. Thank you for clarification.
  • Ironcache
    Ironcache about 6 years
    Expecting 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.
  • Guru
    Guru over 2 years
    nice answer, thanks for clarity