From a780afa201e77ec4c0261800891653b42f927ae6 Mon Sep 17 00:00:00 2001 From: TornjV Date: Sun, 22 May 2016 23:29:41 +0200 Subject: [PATCH] Floyd-Warshall algorithm --- algorithm/category.json | 3 +- .../graph_search/floyd_warshall/desc.json | 16 +++++++++ .../floyd_warshall/shortest_paths/code.js | 34 +++++++++++++++++++ .../floyd_warshall/shortest_paths/data.js | 10 ++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 algorithm/graph_search/floyd_warshall/desc.json create mode 100644 algorithm/graph_search/floyd_warshall/shortest_paths/code.js create mode 100644 algorithm/graph_search/floyd_warshall/shortest_paths/data.js diff --git a/algorithm/category.json b/algorithm/category.json index 9bf56e1b..21d7d53a 100644 --- a/algorithm/category.json +++ b/algorithm/category.json @@ -5,7 +5,8 @@ "dfs": "DFS", "bfs": "BFS", "dijkstra": "Dijkstra", - "bellman_ford": "Bellman-Ford" + "bellman_ford": "Bellman-Ford", + "floyd_warshall": "Floyd-Warshall" } }, "search": { diff --git a/algorithm/graph_search/floyd_warshall/desc.json b/algorithm/graph_search/floyd_warshall/desc.json new file mode 100644 index 00000000..8b369e17 --- /dev/null +++ b/algorithm/graph_search/floyd_warshall/desc.json @@ -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|3)", + "space": "worst O(|V|2)" + }, + "References": [ + "Wikipedia" + ], + "files": { + "shortest_paths": "Finding the shortest path between all nodes" + } +} \ No newline at end of file diff --git a/algorithm/graph_search/floyd_warshall/shortest_paths/code.js b/algorithm/graph_search/floyd_warshall/shortest_paths/code.js new file mode 100644 index 00000000..f4879fbb --- /dev/null +++ b/algorithm/graph_search/floyd_warshall/shortest_paths/code.js @@ -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(); \ No newline at end of file diff --git a/algorithm/graph_search/floyd_warshall/shortest_paths/data.js b/algorithm/graph_search/floyd_warshall/shortest_paths/data.js new file mode 100644 index 00000000..1578cb5f --- /dev/null +++ b/algorithm/graph_search/floyd_warshall/shortest_paths/data.js @@ -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); \ No newline at end of file