Questions tagged [bellman-ford]

The Bellman–Ford algorithm computes single-source shortest paths in a weighted digraph. For graphs with only non-negative edge weights, the faster Dijkstra's algorithm also solves the problem. Thus, Bellman–Ford is used primarily for graphs with negative edge weights. The algorithm is named after its developers, Richard Bellman and Lester Ford, Jr.

Overview

Bellman–Ford is in its basic structure very similar to Dijkstra's algorithm, but instead of greedily selecting the minimum-weight node not yet processed to relax, it simply relaxes all the edges, and does this |V | − 1 times, where |V | is the number of vertices in the graph. The repetitions allow minimum distances to accurately propagate throughout the graph, since, in the absence of negative cycles, the shortest path can only visit each node at most once. Unlike the greedy approach, which depends on certain structural assumptions derived from positive weights, this straightforward approach extends to the general case.

Bellman–Ford runs in O(|V|·|E|) time, where |V| and |E| are the number of vertices and edges respectively.

Pseudocode

        BELLMAN-FORD (G, w, s)

            INITIALIZE-SINGLE-SOURCE (G, s)
            for each vertex i = 1 to V[G] - 1 do
                for each edge (u, v) in E[G] do
                    RELAX (u, v, w)
            For each edge (u, v) in E[G] do
                if d[u] + w(u, v) < d[v] then
                    return FALSE
            return TRUE

Implementation Example

This is a simple implementation of the Bellman-Ford algorithm for finding the shortest path from a single source in a graph.

#include <stdio.h>

typedef struct {
    int u, v, w;
} Edge;

int n;                        /* the number of nodes */
int e;                        /* the number of edges */
Edge edges[1024];             /* large enough for n <= 2^5=32 */
int d[32];                    /* d[i] is the minimum distance from node s to node i */

#define INFINITY 10000

void printDist() {
    int i;

    printf("Distances:\n");

    for (i = 0; i < n; ++i)
        printf("to %d\t", i + 1);
    printf("\n");

    for (i = 0; i < n; ++i)
        printf("%d\t", d[i]);

    printf("\n\n");
}

void bellman_ford(int s) {
    int i, j;

    for (i = 0; i < n; ++i)
        d[i] = INFINITY;

    d[s] = 0;

    for (i = 0; i < n - 1; ++i)
        for (j = 0; j < e; ++j)
            if (d[edges[j].u] + edges[j].w < d[edges[j].v])
                d[edges[j].v] = d[edges[j].u] + edges[j].w;
}

int main(int argc, char *argv[]) {
    int i, j;
    int w;

    FILE *fin = fopen("dist.txt", "r");
    fscanf(fin, "%d", &n);
    e = 0;

    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j) {
            fscanf(fin, "%d", &w);
            if (w != 0) {
                edges[e].u = i;
                edges[e].v = j;
                edges[e].w = w;
                ++e;
            }
        }
    fclose(fin);

    /* printDist(); */

    bellman_ford(0);

    printDist();

    return 0;
}
211 questions
73
votes
8 answers

Bellman-Ford vs Dijkstra: Under what circumstances is Bellman-Ford better?

After a lot of Googling, I've found that most sources say that the Dijkstra algorithm is "more efficient" than the Bellman-Ford algorithm. But under what circumstances is the Bellman-Ford algorithm better than the Dijkstra algorithm? I know…
crazyCoder
  • 1,552
  • 3
  • 20
  • 25
23
votes
3 answers

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

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…
anuj pradhan
  • 2,777
  • 4
  • 26
  • 31
22
votes
2 answers

Am I right about the differences between Floyd-Warshall, Dijkstra's and Bellman-Ford algorithms?

I've been studying the three and I'm stating my inferences from them below. Could someone tell me if I have understood them accurately enough or not? Thank you. Dijkstra's algorithm is used only when you have a single source and you want to know…
GrowinMan
  • 4,891
  • 12
  • 41
  • 58
21
votes
2 answers

Why do we call it "Relaxing" an edge?

In Dijkstra's shortest path algorithm and others, to examine an edge to see if it offers a better path to a node is referred to as relaxing the edge. Why is it called relaxing?
Eric G
  • 4,018
  • 4
  • 20
  • 23
17
votes
9 answers

Finding the shortest path in a graph without any negative prefixes

Find the shortest path from source to destination in a directed graph with positive and negative edges, such that at no point in the path the sum of edges coming before it is negative. If no such path exists report that too. I tried to use…
anirudh
  • 562
  • 6
  • 23
15
votes
4 answers

What exactly can cause counting-to-infinity in the bellman-ford algorithm

From what I can understand, counting-to-infinity occurs when one router feeds another old information, which continues to propagate through the network toward infinity. From what I read, this can definitely occur when a link is removed. So in this…
Cory Gross
  • 36,833
  • 17
  • 68
  • 80
9
votes
4 answers

Negative Weight Cycle Algorithm

I was thinking about the algorithm of finding a negative weight cycle in a directed graph. The Problem is: we have a graph G(V,E), we need to find an efficient algorithm to find a cycle with negative weight. I understand the algorithm in this PDF…
Saher Ahwal
  • 9,015
  • 32
  • 84
  • 152
9
votes
2 answers

Can Dijkstra's Single Source Shortest Path Algorithm detect an infinite cycle in a graph?

So I came to this beautiful problem that asks you to write a program that finds whether a negative infinity shortest path exists in a directed graph. (Also can be thought of as finding whether a "negative cycle" exists in the graph). Here's a link…
Traveling Salesman
  • 2,209
  • 11
  • 46
  • 83
9
votes
1 answer

Yen's improvement to Bellman-Ford

I stumbled with famous Yen's optimization of Bellman-Ford algorithm that I got initially from Wikipedia, then I found the same improvement in several textbooks in Exercise section (for example, this is a problem 24-1 in Cormen and Web exercise N5 in…
Roman Dobrovenskii
  • 935
  • 10
  • 23
8
votes
1 answer

Algorithm to find top K paths in graph, with no common vertices, negative weights?

I'm using Bellman-Ford to find the shortest path through a graph with some negative weights. The graph has no possibility of loops and no bi-directional connections. I'd like to find the K shortest paths through the graph, where the paths share no…
Mastiff
  • 2,101
  • 2
  • 23
  • 38
7
votes
2 answers

Fastest algorithm to detect if there is negative cycle in a graph

I use a matrix d to present a graph. d.(i).(j) means the distance between i and j; v denotes the number of nodes in the graph. It is possible that there is negative cycle in this graph. I would like to check if a negative cycle exists. I have…
SoftTimur
  • 5,630
  • 38
  • 140
  • 292
6
votes
4 answers

How can I create random single source random acyclic directed graphs with negative edge weights in python

I want to do a execution time analysis of the bellman ford algorithm on a large number of graphs and in order to do that I need to generate a large number of random DAGS with the possibility of having negative edge weights. I am using networkx in…
Sandeep Soni
  • 127
  • 3
  • 7
6
votes
1 answer

Bellman-Ford: all shortest paths

I've successfully implemented Bellman-Ford to find the distance of the shortest path when edges have negative weights/distances. I've not been able to get it to return all shortest paths (when there are ties for shortest). I managed to get all…
user1507844
  • 5,973
  • 10
  • 38
  • 55
5
votes
3 answers

cross thread communication java

So I am new to Java, I have done a bit of c programming. I am trying to make a virtual network of nodes, each node would need to be a thread. The nodes are only allowed to talk to their neighbor nodes. there will be a master node that can talk to…
fieldju
  • 1,443
  • 1
  • 14
  • 20
5
votes
2 answers

Why need (node number - 1) iterations in Bellman Ford algorithm to find shortest paths?

Image: 4 iterations, with (a) is the original graph. (b), (c), (d), (e) correspond to result after each iteration. Example from "Introduction to Algorithm 3rd" Hi, I do not understand few aspects about the algorithm. I hope someone could help.…
hieppm
  • 113
  • 2
  • 9
1
2 3
14 15