diff --git a/graph/floyd_warshall.ts b/graph/floyd_warshall.ts deleted file mode 100644 index c8addc46..00000000 --- a/graph/floyd_warshall.ts +++ /dev/null @@ -1,37 +0,0 @@ -/** - * @function floydWarshall - * @description Compute the shortest path for all pairs of nodes for a graph without negative weight edges. The input graph is a adjacency matrix, where graph[i][j] holds the weight of edges a->b. If the edge does not exist, the value in the matrix is Infinity. - * @Complexity_Analysis - * Time complexity: O(V^3) - * Space Complexity: O(V^2). This space is required to hold the result - * @param {number[][]} graph - The graph in adjacency matrix form - * @return {number[][]} - A matrix holding the shortest path for each pair of nodes. matrix[i][j] holds the distance of the shortest path (i -> j). - * @see https://en.wikipedia.org/wiki/Floyd%E2%80%93Warshall_algorithm - */ -export const floydWarshall = (graph: number[][]): number[][] => { - let distances = structuredClone(graph); - let N = graph.length; - - // We begin by setting the weighted adjacency matrix as the shortest paths. - // For the k'th iteration, we try to relax the shortest paths by including node k in the path. - for (let k = 0; k < N; ++k) { - let newDistances = []; - for (let i = 0; i < N; ++i) { - newDistances.push(Array(N).fill(Infinity)); - } - - for (let i = 0; i < N; ++i) { - for (let j = 0; j < N; ++j) { - // The shortest path from node i to j is the minimum of: - // 1. the shortest path (i -> j) without node k - // 2. the sum of the shortest path (i -> k) and (k -> j) - newDistances[i][j] = Math.min(distances[i][j], distances[i][k] + distances[k][j]); - } - } - distances = newDistances; - } - - return distances; -} - - diff --git a/graph/test/floyd_warshall.test.ts b/graph/test/floyd_warshall.test.ts deleted file mode 100644 index 33cc67dc..00000000 --- a/graph/test/floyd_warshall.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { floydWarshall } from "../floyd_warshall"; - -describe("floydWarshall", () => { - it("should return the correct value for zero element graph", () => { - expect(floydWarshall([])).toEqual([]); - }); - - it("should return the correct value for one element graph", () => { - expect(floydWarshall([[1]])).toStrictEqual([[1]]); - }); - - it("should return the correct value for two element graph", () => { - expect(floydWarshall([[10, 4], [3, 6]])).toStrictEqual([[7, 4], [3, 6]]); - }); - - it("should return the correct value", () => { - let graph = []; - for (let i = 1; i <= 5; ++i) { - let arr = []; - for (let j = 1; j <= 5; ++j) { - arr.push(i * j); - } - graph.push(arr); - } - - let expected = [ - [ 1, 2, 3, 4, 5 ], - [ 2, 4, 5, 6, 7 ], - [ 3, 5, 6, 7, 8 ], - [ 4, 6, 7, 8, 9 ], - [ 5, 7, 8, 9, 10 ] - ]; - expect(floydWarshall(graph)).toStrictEqual(expected); - }); - - it("should return the correct value", () => { - let graph = [ - [0, 4, Infinity, Infinity, Infinity, Infinity, Infinity, 8, Infinity], - [4, 0, 8, Infinity, Infinity, Infinity, Infinity, 11, Infinity], - [Infinity, 8, 0, 7, Infinity, 4, Infinity, Infinity, 2], - [Infinity, Infinity, 7, 0, 9, 14, Infinity, Infinity, Infinity], - [Infinity, Infinity, Infinity, 9, 0, 10, Infinity, Infinity, Infinity], - [Infinity, Infinity, 4, 14, 10, 0, 2, Infinity, Infinity], - [Infinity, Infinity, Infinity, Infinity, Infinity, 2, 0, 1, 6], - [8, 11, Infinity, Infinity, Infinity, Infinity, 1, 0, 7], - [Infinity, Infinity, 2, Infinity, Infinity, Infinity, 6, 7, 0] - ]; - - let expected = [ - [0, 4, 12, 19, 21, 11, 9, 8, 14], - [4, 0, 8, 15, 22, 12, 12, 11, 10], - [12, 8, 0, 7, 14, 4, 6, 7, 2], - [19, 15, 7, 0, 9, 11, 13, 14, 9], - [21, 22, 14, 9, 0, 10, 12, 13, 16], - [11, 12, 4, 11, 10, 0, 2, 3, 6], - [9, 12, 6, 13, 12, 2, 0, 1, 6], - [8, 11, 7, 14, 13, 3, 1, 0, 7], - [14, 10, 2, 9, 16, 6, 6, 7, 0] - ] - - expect(floydWarshall(graph)).toStrictEqual(expected); - }); -});