Skip to content

Floyd-Warshall's algorithm #16

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 1 commit into from
May 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion algorithm/category.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"dfs": "DFS",
"bfs": "BFS",
"dijkstra": "Dijkstra",
"bellman_ford": "Bellman-Ford"
"bellman_ford": "Bellman-Ford",
"floyd_warshall": "Floyd-Warshall"
}
},
"search": {
Expand Down
16 changes: 16 additions & 0 deletions algorithm/graph_search/floyd_warshall/desc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Floyd-Warshall": "Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights (but with no negative cycles)",
"Applications": [
""
],
"Complexity": {
"time": "worst O(|V|<sup>3</sup>)",
"space": "worst O(|V|<sup>2</sup>)"
},
"References": [
"<a href='https://en.wikipedia.org/wiki/Floyd–Warshall_algorithm'>Wikipedia</a>"
],
"files": {
"shortest_paths": "Finding the shortest path between all nodes"
}
}
34 changes: 34 additions & 0 deletions algorithm/graph_search/floyd_warshall/shortest_paths/code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function FloydWarshall(){
// Finds the shortest path between all nodes
var S = new Array(G.length);
for (var i = 0; i < G.length; i++) S[i] = new Array(G.length)
for (i = 0; i < G.length; i++){
tracer._visit(i)
for (var j = 0; j < G.length; j++){
// Distance to self is always 0
if (i==j) S[i][i] = 0;
// Distance between connected nodes is their weight
else if (G[i][j] > 0){
S[i][j] = G[i][j];
tracer._visit(j,i,S[i][j]);
tracer._leave(j,i,S[i][j]);
}// Else we don't know the distnace and we set it to a big value (infinity)
else S[i][j] = 19990719;
}
tracer._leave(i)
}
// If there is a shorter path using k, use it instead
for (var k = 0; k < G.length; k++)
for (i = 0; i < G.length; i++)
for (j = 0; j < G.length; j++)
if (S[i][j] > S[i][k] + S[k][j])
S[i][j] = S[i][k] + S[k][j];
for (i = 0; i < G.length; i++)
for (j = 0; j < G.length; j++)
if(S[i][j] == 19990719) tracer._print('there is no path from ' + i + ' to ' + j);
else tracer._print('the shortest path from ' + i +
' to ' + j + ' is ' + S[i][j]);
}
tracer._pace(200);
tracer._print('finding the shortest paths from and to all nodes');
FloydWarshall();
10 changes: 10 additions & 0 deletions algorithm/graph_search/floyd_warshall/shortest_paths/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var tracer = new WeightedDirectedGraphTracer();
/*var G = [ // G[i][j] indicates the weight of the path from the i-th node to the j-th node
[0, 3, 0, 1, 0],
[5, 0, 1, 2, 4],
[1, 0, 0, 2, 0],
[0, 2, 0, 0, 1],
[0, 1, 3, 0, 0]
];*/
var G = WeightedDirectedGraph.random(10, .4, 1, 9);
tracer._setData(G);