From d72a548c7c458bc17de310812797adcfdfe5c89b Mon Sep 17 00:00:00 2001 From: Abiral jain <134861989+Abiral-2724@users.noreply.github.com> Date: Mon, 30 Sep 2024 23:40:35 +0530 Subject: [PATCH] Create Bellman-Ford Algorithm --- Graphs/Bellman-Ford Algorithm | 59 +++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Graphs/Bellman-Ford Algorithm diff --git a/Graphs/Bellman-Ford Algorithm b/Graphs/Bellman-Ford Algorithm new file mode 100644 index 0000000000..871d0ba9a7 --- /dev/null +++ b/Graphs/Bellman-Ford Algorithm @@ -0,0 +1,59 @@ +class Solution { + // Function to implement Bellman Ford + // edges: array of arrays which represents the graph + // S: source vertex to start traversing graph with + // V: number of vertices + bellmanFord(V, edges, S) { + // Initialize distance array with a large value (1e8) + let dis = new Array(V).fill(1e8); + dis[S] = 0; + + // Bellman Ford algorithm + for (let i = 0; i < V - 1; i++) { + for (let edge of edges) { + let u = edge[0]; + let v = edge[1]; + let wt = edge[2]; + + // If you have not reached 'u' till now, move forward + if (dis[u] !== 1e8 && dis[u] + wt < dis[v]) { + // Update distance array + dis[v] = dis[u] + wt; + } + } + } + + // Checking for negative cycle + // Check for nth relaxation + for (let edge of edges) { + let u = edge[0]; + let v = edge[1]; + let wt = edge[2]; + + if (dis[u] !== 1e8 && dis[u] + wt < dis[v]) { + // If the distance array gets reduced for the nth iteration + // It means a negative cycle exists + return [-1]; + } + } + + return dis; + } +} + +// Example usage: +let solution = new Solution(); +let V = 5; // Number of vertices +let edges = [ + [0, 1, -1], + [0, 2, 4], + [1, 2, 3], + [1, 3, 2], + [1, 4, 2], + [3, 2, 5], + [3, 1, 1], + [4, 3, -3] +]; +let S = 0; // Source vertex + +console.log(solution.bellmanFord(V, edges, S));