Bellman- Ford Algorithm Algorithm

The Bellman-Ford algorithm is an essential graph algorithm that is used to find the shortest path from a single source vertex to all other vertices in a weighted graph. It is capable of handling graphs with negative weight edges, which distinguishes it from other shortest path algorithms like Dijkstra's. The algorithm is named after its developers, Richard Bellman and Lester Ford, who independently proposed their versions of the algorithm in the late 1950s. The Bellman-Ford algorithm works by iteratively updating the distance estimates for each vertex in the graph. Initially, the distance from the source vertex to itself is set to zero, while the distances to all other vertices are set to infinity. The algorithm then iteratively relaxes all edges in the graph (i.e., updates the distance estimates if a shorter path is found) for a total of |V| - 1 times, where |V| is the number of vertices in the graph. The reason for the |V| - 1 iterations is that, in the worst case, a shortest path can have at most |V| - 1 edges. After these iterations, the distance estimates for all vertices should be the shortest possible distances from the source vertex. If a negative weight cycle is detected, the algorithm will indicate that no solution exists, as the presence of such a cycle would result in infinite loops and undefined shortest paths.
/*
 Petar 'PetarV' Velickovic
 Algorithm: Bellman-Ford Algorithm
*/

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <complex>
#define MAX_N 5001
#define MAX_E 25000001
#define INF 987654321
using namespace std;
typedef long long lld;

int v, e;

int dist[MAX_N];
struct Edge
{
    int x, y, weight;
};
Edge E[MAX_N];

//Bellman-Ford algoritam za trazenje najkracih puteva iz odredjenog cvora u grafu (graf moze imati i negativne ivice)
//Slozenost: O(V*E)

inline int BellmanFord(int source)
{
    for (int i=0;i<v;i++)
    {
        if (i == source) dist[i]=0;
        else dist[i] = INF;
    }
    bool done = false;
    for (int i=0;!done&&i<v;i++)
    {
        done = true;
        for (int j=0;j<e;j++)
        {
            int so = E[j].x;
            int de = E[j].y;
            if (dist[so] + E[j].weight < dist[de])
            {
                dist[de] = dist[so] + E[j].weight;
                done=false;
            }
        }
    }
    if (!done) return -1; //negative edge cycle detected
    return 0;
}

int main()
{
    v = 4, e = 8;
    
    E[0].x = 0, E[0].y = 1, E[0].weight = 5;
    E[1].x = 1, E[1].y = 0, E[1].weight = 5;
    
    E[2].x = 1, E[2].y = 2, E[2].weight = 5;
    E[3].x = 2, E[3].y = 1, E[3].weight = 5;
    
    E[4].x = 2, E[4].y = 3, E[4].weight = 5;
    E[5].x = 3, E[5].y = 2, E[5].weight = 5;
    
    E[6].x = 3, E[6].y = 1, E[6].weight = 6;
    E[7].x = 1, E[7].y = 3, E[7].weight = 6;
    
    BellmanFord(0);
    printf("%d\n",dist[3]);
    return 0;
}

LANGUAGE:

DARK MODE: