Skip to content

Commit

Permalink
[ADD] bellman ford
Browse files Browse the repository at this point in the history
  • Loading branch information
fadli0029 committed Jul 13, 2024
1 parent da4402a commit ce3ce27
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
6 changes: 6 additions & 0 deletions algorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ add_executable(dijkstra_exe graph/dijkstra.cpp)
target_include_directories(dijkstra_exe PUBLIC ${Catch2_INCLUDE_DIRS})
target_link_libraries(dijkstra_exe Catch2::Catch2WithMain)
add_test(NAME DijkstraTest COMMAND dijkstra_exe)

# Add bellman-ford
add_executable(bellman_ford_exe graph/bellman_ford.cpp)
target_include_directories(bellman_ford_exe PUBLIC ${Catch2_INCLUDE_DIRS})
target_link_libraries(bellman_ford_exe Catch2::Catch2WithMain)
add_test(NAME BellmanFordTest COMMAND bellman_ford_exe)
129 changes: 125 additions & 4 deletions algorithms/graph/bellman_ford.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,125 @@
// There's two variant:
// 1. We want shortest path, so do N-1 iterations.
// 2. A constraint says at most k edges are allowed to get the shortest distance between two nodes, so do k iterations.
// (see letcode graph explore card bellman ford chapter).
/* =====================================================================================
This file contains the implementation of the Bellman-Ford's algorithm. It returns
the shortest distance from the starting node to all other nodes.
@param n: the number of vertices in the graph
@param e: the edge list representation of the graph
@param s: the starting node
@return vector<int>: the shortest distance from the starting node to all other nodes,
{-1} if there is a negative cycle in the graph
@author: Muhammad Fadli Alim Arsani
======================================================================================*/

#define CATCH_CONFIG_MAIN
#include <catch2/catch_test_macros.hpp>

#include "bits/stdc++.h"
using namespace std;

vector<int> bellman_ford(int n, vector<vector<int>>& e, int s) {
vector<int> dists(n, INT_MAX);
dists[s] = 0;

for (int i=0; i<n-1; i++) {
for (const auto& edge : e) {
int u = edge[0], v = edge[1], w = edge[2];

// Edge relaxation step
if (dists[u] != INT_MAX && dists[u] + w < dists[v]) {
dists[v] = dists[u] + w;
}
}
}

// Check for negative cycle existence by doing
// one more iteration (i.e: n iteration instead of n-1)
for (const auto& edge : e) {
int u = edge[0], v = edge[1], w = edge[2];

// Edge relaxation step
if (dists[u] != INT_MAX && dists[u] + w < dists[v]) {
return {-1};
}
}

return dists;
}

TEST_CASE("Bellman-Ford algorithm test cases") {
SECTION("Simple graph without negative weights") {
int n = 5;
vector<vector<int>> e = {
{0, 1, 2},
{0, 3, 6},
{1, 2, 3},
{1, 3, 8},
{1, 4, 5},
{2, 4, 7},
{3, 4, 9}
};
int start = 0;
vector<int> expected = {0, 2, 5, 6, 7};
vector<int> result = bellman_ford(n, e, start);

REQUIRE(result == expected);
}

SECTION("Graph with negative weights but no negative cycle") {
int n = 5;
vector<vector<int>> e = {
{0, 1, -1},
{0, 2, 4},
{1, 2, 3},
{1, 3, 2},
{1, 4, 2},
{3, 2, 5},
{3, 1, 1},
{4, 3, -3}
};
int start = 0;
vector<int> expected = {0, -1, 2, -2, 1};
vector<int> result = bellman_ford(n, e, start);

REQUIRE(result == expected);
}

SECTION("Graph with a negative cycle") {
int n = 4;
vector<vector<int>> e = {
{0, 1, 1},
{1, 2, -1},
{2, 3, -1},
{3, 0, -1}
};
int start = 0;
vector<int> expected = {-1};
vector<int> result = bellman_ford(n, e, start);

REQUIRE(result == expected);
}

SECTION("Disconnected graph") {
int n = 4;
vector<vector<int>> e = {
{0, 1, 4},
{1, 2, 5}
};
int start = 0;
vector<int> expected = {0, 4, 9, INT_MAX};
vector<int> result = bellman_ford(n, e, start);

REQUIRE(result == expected);
}

SECTION("Graph with a single node") {
int n = 1;
vector<vector<int>> e = {};
int start = 0;
vector<int> expected = {0};
vector<int> result = bellman_ford(n, e, start);

REQUIRE(result == expected);
}
}

1 change: 1 addition & 0 deletions algorithms/graph/dijkstra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@param n: the number of vertices in the graph
@param g: the adjacency list representation of the graph
@param s: the starting node
@return vector<int>: the shortest distance from the starting node to all other nodes
@author: Muhammad Fadli Alim Arsani
=====================================================================================*/
Expand Down

0 comments on commit ce3ce27

Please sign in to comment.