From ce3ce27588c4bd8c6fcc8a7867e843f1199ad768 Mon Sep 17 00:00:00 2001 From: fadli0029 Date: Fri, 12 Jul 2024 22:30:36 -0700 Subject: [PATCH] [ADD] bellman ford --- algorithms/CMakeLists.txt | 6 ++ algorithms/graph/bellman_ford.cpp | 129 +++++++++++++++++++++++++++++- algorithms/graph/dijkstra.cpp | 1 + 3 files changed, 132 insertions(+), 4 deletions(-) diff --git a/algorithms/CMakeLists.txt b/algorithms/CMakeLists.txt index 8930761..fbc401c 100644 --- a/algorithms/CMakeLists.txt +++ b/algorithms/CMakeLists.txt @@ -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) diff --git a/algorithms/graph/bellman_ford.cpp b/algorithms/graph/bellman_ford.cpp index a6fe24a..f3cf382 100644 --- a/algorithms/graph/bellman_ford.cpp +++ b/algorithms/graph/bellman_ford.cpp @@ -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: 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 + +#include "bits/stdc++.h" +using namespace std; + +vector bellman_ford(int n, vector>& e, int s) { + vector dists(n, INT_MAX); + dists[s] = 0; + + for (int i=0; i> 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 expected = {0, 2, 5, 6, 7}; + vector result = bellman_ford(n, e, start); + + REQUIRE(result == expected); + } + + SECTION("Graph with negative weights but no negative cycle") { + int n = 5; + vector> 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 expected = {0, -1, 2, -2, 1}; + vector result = bellman_ford(n, e, start); + + REQUIRE(result == expected); + } + + SECTION("Graph with a negative cycle") { + int n = 4; + vector> e = { + {0, 1, 1}, + {1, 2, -1}, + {2, 3, -1}, + {3, 0, -1} + }; + int start = 0; + vector expected = {-1}; + vector result = bellman_ford(n, e, start); + + REQUIRE(result == expected); + } + + SECTION("Disconnected graph") { + int n = 4; + vector> e = { + {0, 1, 4}, + {1, 2, 5} + }; + int start = 0; + vector expected = {0, 4, 9, INT_MAX}; + vector result = bellman_ford(n, e, start); + + REQUIRE(result == expected); + } + + SECTION("Graph with a single node") { + int n = 1; + vector> e = {}; + int start = 0; + vector expected = {0}; + vector result = bellman_ford(n, e, start); + + REQUIRE(result == expected); + } +} + diff --git a/algorithms/graph/dijkstra.cpp b/algorithms/graph/dijkstra.cpp index b070a92..d8f72f9 100644 --- a/algorithms/graph/dijkstra.cpp +++ b/algorithms/graph/dijkstra.cpp @@ -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: the shortest distance from the starting node to all other nodes @author: Muhammad Fadli Alim Arsani =====================================================================================*/