-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Added BellmanFord #679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Added BellmanFord #679
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1260a12
Added BellmanFord
Mayank17M abc5a2f
Add References for BellmanFord
Mayank17M fcfbf82
Style code using standard.js
Mayank17M cca8635
Add tests and modify code
Mayank17M 8e8a066
Fixed BellmanFord test file
Mayank17M 4ab3036
Add BellmanFord and tests
Mayank17M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.