From 1260a128a4f7056e9680b39ceca5112d2141cb4b Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Sat, 4 Sep 2021 23:24:35 +0530 Subject: [PATCH 1/6] Added BellmanFord --- Graphs/BellmanFord.js | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Graphs/BellmanFord.js diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js new file mode 100644 index 0000000000..a8dcfee3dd --- /dev/null +++ b/Graphs/BellmanFord.js @@ -0,0 +1,51 @@ +//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. +//Worst-case performance O(VE) +//Best-case performance O(E) +//Worst-case space complexity O(V) + +function BellmanFord(graph, V, E, src) +{ + // Initialize distance of all vertices as infinite. + var 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 (var i = 0; i < V - 1; i++){ + for (var 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]; + } + } + // check for negative-weight cycles. + for (var i = 0; i < E; i++){ + var x = graph[i][0]; + var y = graph[i][1]; + var weight = graph[i][2]; + if ((dis[x] != Infinity) && (dis[x] + weight < dis[y])){ + console.log("Graph contains negative weight cycle") + } + } + console.log("Vertex Distance from Source") + for (var i = 0; i < V; i++){ + console.log(i + " " + dis[i]) + } +} + +// Driver code +var V = 5; // Number of vertices in graph +var E = 8; // Number of edges in graph + +// Every edge has three values (u, v, w) where +// the edge is from vertex u to v. And weight +// of the edge is w. +var graph = [[ 0, 1, -1 ], [ 0, 2, 4 ], + [ 1, 2, 3 ], [ 1, 3, 2 ], + [ 1, 4, 2 ], [ 3, 2, 5 ], + [ 3, 1, 1 ], [ 4, 3, -3 ]] + +BellmanFord(graph, V, E, 0) \ No newline at end of file From abc5a2fcef57655be74db3009c93cb70bfddefc9 Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Sun, 5 Sep 2021 15:38:08 +0530 Subject: [PATCH 2/6] Add References for BellmanFord --- Graphs/BellmanFord.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js index a8dcfee3dd..1578811215 100644 --- a/Graphs/BellmanFord.js +++ b/Graphs/BellmanFord.js @@ -5,6 +5,10 @@ //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 + function BellmanFord(graph, V, E, src) { // Initialize distance of all vertices as infinite. From fcfbf822582d4adb5f10f8ff6f1096129f18eb83 Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Sun, 5 Sep 2021 21:56:14 +0530 Subject: [PATCH 3/6] Style code using standard.js --- Graphs/BellmanFord.js | 111 ++++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js index 1578811215..922bb569f1 100644 --- a/Graphs/BellmanFord.js +++ b/Graphs/BellmanFord.js @@ -1,55 +1,62 @@ -//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. -//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 - -function BellmanFord(graph, V, E, src) -{ - // Initialize distance of all vertices as infinite. - var 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 (var i = 0; i < V - 1; i++){ - for (var 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]; - } - } - // check for negative-weight cycles. - for (var i = 0; i < E; i++){ - var x = graph[i][0]; - var y = graph[i][1]; - var weight = graph[i][2]; - if ((dis[x] != Infinity) && (dis[x] + weight < dis[y])){ - console.log("Graph contains negative weight cycle") - } +/* +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 + +*/ + +function BellmanFord (graph, V, E, src) { + // 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] } } - console.log("Vertex Distance from Source") - for (var i = 0; i < V; i++){ - console.log(i + " " + dis[i]) + } + // 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])) { + console.log('Graph contains negative weight cycle') } + } + console.log('Vertex Distance from Source') + for (let i = 0; i < V; i++) { + console.log(i + ' ' + dis[i]) + } } - -// Driver code -var V = 5; // Number of vertices in graph -var E = 8; // Number of edges in graph - -// Every edge has three values (u, v, w) where -// the edge is from vertex u to v. And weight -// of the edge is w. -var graph = [[ 0, 1, -1 ], [ 0, 2, 4 ], - [ 1, 2, 3 ], [ 1, 3, 2 ], - [ 1, 4, 2 ], [ 3, 2, 5 ], - [ 3, 1, 1 ], [ 4, 3, -3 ]] - -BellmanFord(graph, V, E, 0) \ No newline at end of file + +function main () { + // Driver code + const V = 5 // Number of vertices in graph + const E = 8 // Number of edges in graph + + // Every edge has three values (u, v, w) where + // the edge is from vertex u to v. And weight + // of the edge is w. + const graph = [[0, 1, -1], [0, 2, 4], + [1, 2, 3], [1, 3, 2], + [1, 4, 2], [3, 2, 5], + [3, 1, 1], [4, 3, -3]] + + BellmanFord(graph, V, E, 0) +} + +main() From cca86358d35755c87473d3f1e12bcd92a4041dc9 Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Mon, 6 Sep 2021 17:06:21 +0530 Subject: [PATCH 4/6] Add tests and modify code --- Graphs/BellmanFord.js | 10 +++++----- Graphs/test/BellmanFord.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 Graphs/test/BellmanFord.test.js diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js index 922bb569f1..7fc9e7d3fe 100644 --- a/Graphs/BellmanFord.js +++ b/Graphs/BellmanFord.js @@ -14,7 +14,7 @@ Reference: */ -function BellmanFord (graph, V, E, src) { +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 @@ -34,12 +34,11 @@ function BellmanFord (graph, V, E, src) { const y = graph[i][1] const weight = graph[i][2] if ((dis[x] !== Infinity) && (dis[x] + weight < dis[y])) { - console.log('Graph contains negative weight cycle') + return null } } - console.log('Vertex Distance from Source') for (let i = 0; i < V; i++) { - console.log(i + ' ' + dis[i]) + if (i === dest) return dis[i] } } @@ -47,6 +46,7 @@ function main () { // Driver code const V = 5 // Number of vertices in graph const E = 8 // Number of edges in graph + const destination = 3 // Destination where we want to reach // Every edge has three values (u, v, w) where // the edge is from vertex u to v. And weight @@ -56,7 +56,7 @@ function main () { [1, 4, 2], [3, 2, 5], [3, 1, 1], [4, 3, -3]] - BellmanFord(graph, V, E, 0) + console.log(BellmanFord(graph, V, E, 0, destination)) } main() diff --git a/Graphs/test/BellmanFord.test.js b/Graphs/test/BellmanFord.test.js new file mode 100644 index 0000000000..a3c14f868a --- /dev/null +++ b/Graphs/test/BellmanFord.test.js @@ -0,0 +1,31 @@ +import { BellmanFord } from '../BellmanFord' + +test('Test Case 1', () => { + const V = 5 + const E = 8 + const destination = 3 + const graph = [[0, 1, -1], [0, 2, 4], + [1, 2, 3], [1, 3, 2], + [1, 4, 2], [3, 2, 5], + [3, 1, 1], [4, 3, -3]] + expect(BellmanFord(graph,V,E,0,destination)).toBe(-2) +}) +test('Test Case 2', () => { + const V = 6 + const E = 9 + const destination = 4 + const graph = [[0, 1, 3], [0, 3, 6], + [0, 5, -1], [1, 2, -3], + [1, 4, -2], [5, 2, 5], + [2, 3, 1], [4, 3, 5], [5,4,2]] + expect(BellmanFord(graph,V,E,0,destination)).toBe(1) +}) +test('Test Case 3', () => { + const V = 4 + const E = 5 + const destination = 1 + const graph = [[0, 3, -1], [0, 2, 4], + [3, 2, 2], [3, 1, 5], + [2, 1, -1]] + expect(BellmanFord(graph,V,E,0,destination)).toBe(0) +}) \ No newline at end of file From 8e8a0664a4a7e91484410d5e367c46f5c45a3985 Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Tue, 7 Sep 2021 20:09:32 +0530 Subject: [PATCH 5/6] Fixed BellmanFord test file --- Graphs/test/BellmanFord.test.js | 48 ++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/Graphs/test/BellmanFord.test.js b/Graphs/test/BellmanFord.test.js index a3c14f868a..0458654c79 100644 --- a/Graphs/test/BellmanFord.test.js +++ b/Graphs/test/BellmanFord.test.js @@ -1,31 +1,31 @@ import { BellmanFord } from '../BellmanFord' test('Test Case 1', () => { - const V = 5 - const E = 8 - const destination = 3 - const graph = [[0, 1, -1], [0, 2, 4], - [1, 2, 3], [1, 3, 2], - [1, 4, 2], [3, 2, 5], - [3, 1, 1], [4, 3, -3]] - expect(BellmanFord(graph,V,E,0,destination)).toBe(-2) + const V = 5 + const E = 8 + const destination = 3 + const graph = [[0, 1, -1], [0, 2, 4], + [1, 2, 3], [1, 3, 2], + [1, 4, 2], [3, 2, 5], + [3, 1, 1], [4, 3, -3]] + expect(BellmanFord(graph, V, E, 0, destination)).toBe(-2) }) test('Test Case 2', () => { - const V = 6 - const E = 9 - const destination = 4 - const graph = [[0, 1, 3], [0, 3, 6], - [0, 5, -1], [1, 2, -3], - [1, 4, -2], [5, 2, 5], - [2, 3, 1], [4, 3, 5], [5,4,2]] - expect(BellmanFord(graph,V,E,0,destination)).toBe(1) + const V = 6 + const E = 9 + const destination = 4 + const graph = [[0, 1, 3], [0, 3, 6], + [0, 5, -1], [1, 2, -3], + [1, 4, -2], [5, 2, 5], + [2, 3, 1], [4, 3, 5], [5, 4, 2]] + expect(BellmanFord(graph, V, E, 0, destination)).toBe(1) }) test('Test Case 3', () => { - const V = 4 - const E = 5 - const destination = 1 - const graph = [[0, 3, -1], [0, 2, 4], - [3, 2, 2], [3, 1, 5], - [2, 1, -1]] - expect(BellmanFord(graph,V,E,0,destination)).toBe(0) -}) \ No newline at end of file + const V = 4 + const E = 5 + const destination = 1 + const graph = [[0, 3, -1], [0, 2, 4], + [3, 2, 2], [3, 1, 5], + [2, 1, -1]] + expect(BellmanFord(graph, V, E, 0, destination)).toBe(0) +}) From 4ab3036ebf70c57f28988314054593c8b764cdc9 Mon Sep 17 00:00:00 2001 From: Mayank17M Date: Thu, 9 Sep 2021 16:34:28 +0530 Subject: [PATCH 6/6] Add BellmanFord and tests --- Graphs/BellmanFord.js | 30 ++++++++++++------------------ Graphs/test/BellmanFord.test.js | 11 +++++++---- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/Graphs/BellmanFord.js b/Graphs/BellmanFord.js index 7fc9e7d3fe..cf9d168460 100644 --- a/Graphs/BellmanFord.js +++ b/Graphs/BellmanFord.js @@ -14,6 +14,17 @@ Reference: */ +/** + * + * @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) @@ -42,21 +53,4 @@ function BellmanFord (graph, V, E, src, dest) { } } -function main () { - // Driver code - const V = 5 // Number of vertices in graph - const E = 8 // Number of edges in graph - const destination = 3 // Destination where we want to reach - - // Every edge has three values (u, v, w) where - // the edge is from vertex u to v. And weight - // of the edge is w. - const graph = [[0, 1, -1], [0, 2, 4], - [1, 2, 3], [1, 3, 2], - [1, 4, 2], [3, 2, 5], - [3, 1, 1], [4, 3, -3]] - - console.log(BellmanFord(graph, V, E, 0, destination)) -} - -main() +export { BellmanFord } diff --git a/Graphs/test/BellmanFord.test.js b/Graphs/test/BellmanFord.test.js index 0458654c79..a5d8e2a856 100644 --- a/Graphs/test/BellmanFord.test.js +++ b/Graphs/test/BellmanFord.test.js @@ -1,4 +1,4 @@ -import { BellmanFord } from '../BellmanFord' +import { BellmanFord } from '../BellmanFord.js' test('Test Case 1', () => { const V = 5 @@ -8,7 +8,8 @@ test('Test Case 1', () => { [1, 2, 3], [1, 3, 2], [1, 4, 2], [3, 2, 5], [3, 1, 1], [4, 3, -3]] - expect(BellmanFord(graph, V, E, 0, destination)).toBe(-2) + const dist = BellmanFord(graph, V, E, 0, destination) + expect(dist).toBe(-2) }) test('Test Case 2', () => { const V = 6 @@ -18,7 +19,8 @@ test('Test Case 2', () => { [0, 5, -1], [1, 2, -3], [1, 4, -2], [5, 2, 5], [2, 3, 1], [4, 3, 5], [5, 4, 2]] - expect(BellmanFord(graph, V, E, 0, destination)).toBe(1) + const dist = BellmanFord(graph, V, E, 0, destination) + expect(dist).toBe(1) }) test('Test Case 3', () => { const V = 4 @@ -27,5 +29,6 @@ test('Test Case 3', () => { const graph = [[0, 3, -1], [0, 2, 4], [3, 2, 2], [3, 1, 5], [2, 1, -1]] - expect(BellmanFord(graph, V, E, 0, destination)).toBe(0) + const dist = BellmanFord(graph, V, E, 0, destination) + expect(dist).toBe(0) })