Can there be an input for the Bellman-Ford algorithm for a graph consisting of TWO vertices? If so, how to handle this case?

  • What is the connection with c# ? - Grundy

1 answer 1

Maybe even from 1.

Pseudocode of alogorithm ( http://e-maxx.ru/algo/ford_bellman )

 void solve() { vector<int> d (n, INF); d[v] = 0; for (int i=0; i<n-1; ++i) for (int j=0; j<m; ++j) if (d[e[j].a] < INF) d[e[j].b] = min (d[e[j].b], d[e[j].a] + e[j].cost); // вывод d, например, на экран } 

With N = 1, there will be no iterations at all. For N = 2, we check the only edge (if it is, of course, 1-->2 ) and if it is, we set it to d[2] = len(1->2) .

Especially nothing needs to be changed. If you have something wrong, check the implementation.

  • Comments are not intended for extended discussion; conversation moved to chat . - PashaPash