diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js index cf9d168460..9a717a5e3d 100644 --- a/Graphs/BellmanFord.js +++ b/Graphs/BellmanFord.js @@ -1,56 +1,75 @@ -/* -The Bellman–Ford algorithm is an algorithm that computes shortest paths -from a single source vertex to all of the other vertices in a weighted digraph. -It also detects negative weight cycle. - -Complexity: - Worst-case performance O(VE) - Best-case performance O(E) - Worst-case space complexity O(V) - -Reference: - https://en.wikipedia.org/wiki/Bellman–Ford_algorithm - https://cp-algorithms.com/graph/bellman_ford.html - -*/ - -/** - * - * @param graph Graph in the format (u, v, w) where - * the edge is from vertex u to v. And weight - * of the edge is w. - * @param V Number of vertices in graph - * @param E Number of edges in graph - * @param src Starting node - * @param dest Destination node - * @returns Shortest distance from source to destination - */ -function BellmanFord (graph, V, E, src, dest) { - // Initialize distance of all vertices as infinite. - const dis = Array(V).fill(Infinity) - // initialize distance of source as 0 - dis[src] = 0 - - // Relax all edges |V| - 1 times. A simple - // shortest path from src to any other - // vertex can have at-most |V| - 1 edges - for (let i = 0; i < V - 1; i++) { - for (let j = 0; j < E; j++) { - if ((dis[graph[j][0]] + graph[j][2]) < dis[graph[j][1]]) { dis[graph[j][1]] = dis[graph[j][0]] + graph[j][2] } +class Edge { + constructor(src, dest, weight) { + this.src = src; + this.dest = dest; + this.weight = weight; } - } - // check for negative-weight cycles. - for (let i = 0; i < E; i++) { - const x = graph[i][0] - const y = graph[i][1] - const weight = graph[i][2] - if ((dis[x] !== Infinity) && (dis[x] + weight < dis[y])) { - return null +} + +class Graph { + constructor(vertices, edges) { + this.vertices = vertices; + this.edges = edges; + this.distance = new Array(vertices); + + // Initialize distances to Infinity + for (let i = 0; i < vertices; i++) { + this.distance[i] = Infinity; + } + } + + bellmanFord(source) { + // Initialize the distance to the source vertex as 0 + this.distance[source] = 0; + + // Relax all edges V-1 times (V is the number of vertices) + for (let i = 0; i < this.vertices - 1; i++) { + for (let j = 0; j < this.edges.length; j++) { + const edge = this.edges[j]; + const u = edge.src; + const v = edge.dest; + const w = edge.weight; + + if (this.distance[u] !== Infinity && this.distance[u] + w < this.distance[v]) { + this.distance[v] = this.distance[u] + w; + } + } + } + + // Check for negative-weight cycles + for (let i = 0; i < this.edges.length; i++) { + const edge = this.edges[i]; + const u = edge.src; + const v = edge.dest; + const w = edge.weight; + + if (this.distance[u] !== Infinity && this.distance[u] + w < this.distance[v]) { + console.log("Graph contains negative-weight cycle. Bellman-Ford cannot solve it."); + return; + } + } + + // Print the distances from the source vertex + console.log("Shortest distances from source vertex:"); + for (let i = 0; i < this.vertices; i++) { + console.log(`Vertex ${i}: ${this.distance[i]}`); + } } - } - for (let i = 0; i < V; i++) { - if (i === dest) return dis[i] - } } -export { BellmanFord } +// Example usage +const vertices = 5; +const edges = [ + new Edge(0, 1, -1), + new Edge(0, 2, 4), + new Edge(1, 2, 3), + new Edge(1, 3, 2), + new Edge(1, 4, 2), + new Edge(3, 2, 5), + new Edge(3, 1, 1), + new Edge(4, 3, -3) +]; + +const graph = new Graph(vertices, edges); +const sourceVertex = 0; +graph.bellmanFord(sourceVertex);