From fafa52c26ee5de19d5b6662c2565600e6abd0c2e Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Fri, 13 Jul 2018 17:36:39 +0300 Subject: [PATCH] Refactor Floyd-Warshall tests. --- .../__test__/floydWarshall.test.js | 44 ++++++++++--------- .../graph/floyd-warshall/floydWarshall.js | 12 ++--- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/algorithms/graph/floyd-warshall/__test__/floydWarshall.test.js b/src/algorithms/graph/floyd-warshall/__test__/floydWarshall.test.js index f04a0fca..dea170c6 100644 --- a/src/algorithms/graph/floyd-warshall/__test__/floydWarshall.test.js +++ b/src/algorithms/graph/floyd-warshall/__test__/floydWarshall.test.js @@ -55,7 +55,7 @@ describe('floydWarshall', () => { .addEdge(edgeFG) .addEdge(edgeEG); - const { distances, previousVertices } = floydWarshall(graph); + const { distances, nextVertices } = floydWarshall(graph); const vertices = graph.getAllVertices(); @@ -77,13 +77,13 @@ describe('floydWarshall', () => { expect(distances[vertexAIndex][vertexGIndex]).toBe(12); expect(distances[vertexAIndex][vertexFIndex]).toBe(11); - expect(previousVertices[vertexAIndex][vertexFIndex]).toBe(vertexD); - expect(previousVertices[vertexAIndex][vertexDIndex]).toBe(vertexB); - expect(previousVertices[vertexAIndex][vertexBIndex]).toBe(vertexA); - expect(previousVertices[vertexAIndex][vertexGIndex]).toBe(vertexE); - expect(previousVertices[vertexAIndex][vertexCIndex]).toBe(vertexA); - expect(previousVertices[vertexAIndex][vertexAIndex]).toBe(null); - expect(previousVertices[vertexAIndex][vertexHIndex]).toBe(null); + expect(nextVertices[vertexAIndex][vertexFIndex]).toBe(vertexD); + expect(nextVertices[vertexAIndex][vertexDIndex]).toBe(vertexB); + expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexA); + expect(nextVertices[vertexAIndex][vertexGIndex]).toBe(vertexE); + expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexA); + expect(nextVertices[vertexAIndex][vertexAIndex]).toBe(null); + expect(nextVertices[vertexAIndex][vertexHIndex]).toBe(null); }); it('should find minimum paths to all vertices for directed graph', () => { @@ -119,7 +119,7 @@ describe('floydWarshall', () => { .addEdge(edgeCA) .addEdge(edgeCD); - const { distances, previousVertices } = floydWarshall(graph); + const { distances, nextVertices } = floydWarshall(graph); const vertices = graph.getAllVertices(); @@ -140,11 +140,11 @@ describe('floydWarshall', () => { [2, 5, 7, 0], ]); - expect(previousVertices[vertexAIndex][vertexDIndex]).toBe(vertexC); - expect(previousVertices[vertexAIndex][vertexCIndex]).toBe(vertexB); - expect(previousVertices[vertexBIndex][vertexDIndex]).toBe(vertexC); - expect(previousVertices[vertexAIndex][vertexAIndex]).toBe(null); - expect(previousVertices[vertexAIndex][vertexBIndex]).toBe(vertexA); + expect(nextVertices[vertexAIndex][vertexDIndex]).toBe(vertexC); + expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexB); + expect(nextVertices[vertexBIndex][vertexDIndex]).toBe(vertexC); + expect(nextVertices[vertexAIndex][vertexAIndex]).toBe(null); + expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexA); }); it('should find minimum paths to all vertices for directed graph with negative edge weights', () => { @@ -188,7 +188,7 @@ describe('floydWarshall', () => { .addEdge(edgeCB) .addEdge(edgeBA); - const { distances, previousVertices } = floydWarshall(graph); + const { distances, nextVertices } = floydWarshall(graph); const vertices = graph.getAllVertices(); @@ -208,11 +208,13 @@ describe('floydWarshall', () => { expect(distances[vertexFIndex][vertexDIndex]).toBe(9); expect(distances[vertexFIndex][vertexEIndex]).toBe(8); - expect(previousVertices[vertexFIndex][vertexGIndex]).toBe(null); - expect(previousVertices[vertexFIndex][vertexFIndex]).toBe(null); - // expect(previousVertices[vertexFIndex][vertexBIndex]).toBe(vertexC); - // expect(previousVertices[vertexFIndex][vertexCIndex].getKey()).toBe(vertexA.getKey()); - // expect(previousVertices[vertexFIndex][vertexAIndex]).toBe(vertexD); - // expect(previousVertices[vertexFIndex][vertexDIndex]).toBe(vertexE); + expect(nextVertices[vertexFIndex][vertexGIndex]).toBe(null); + expect(nextVertices[vertexFIndex][vertexFIndex]).toBe(null); + expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexC); + expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexA); + expect(nextVertices[vertexFIndex][vertexBIndex]).toBe(vertexE); + expect(nextVertices[vertexEIndex][vertexBIndex]).toBe(vertexD); + expect(nextVertices[vertexDIndex][vertexBIndex]).toBe(vertexC); + expect(nextVertices[vertexCIndex][vertexBIndex]).toBe(vertexC); }); }); diff --git a/src/algorithms/graph/floyd-warshall/floydWarshall.js b/src/algorithms/graph/floyd-warshall/floydWarshall.js index 2e92758f..870d78a6 100644 --- a/src/algorithms/graph/floyd-warshall/floydWarshall.js +++ b/src/algorithms/graph/floyd-warshall/floydWarshall.js @@ -1,6 +1,6 @@ /** * @param {Graph} graph - * @return {{distances: number[][], previousVertices: GraphVertex[][]}} + * @return {{distances: number[][], nextVertices: GraphVertex[][]}} */ export default function floydWarshall(graph) { // Get all graph vertices. @@ -8,7 +8,7 @@ export default function floydWarshall(graph) { // Init previous vertices matrix with nulls meaning that there are no // previous vertices exist that will give us shortest path. - const previousVertices = Array(vertices.length).fill(null).map(() => { + const nextVertices = Array(vertices.length).fill(null).map(() => { return Array(vertices.length).fill(null); }); @@ -33,7 +33,7 @@ export default function floydWarshall(graph) { // There is an edge from vertex with startIndex to vertex with endIndex. // Save distance and previous vertex. distances[startIndex][endIndex] = edge.weight; - previousVertices[startIndex][endIndex] = startVertex; + nextVertices[startIndex][endIndex] = startVertex; } else { distances[startIndex][endIndex] = Infinity; } @@ -60,13 +60,13 @@ export default function floydWarshall(graph) { if (distances[startIndex][endIndex] > distViaMiddle) { // We've found a shortest pass via middle vertex. distances[startIndex][endIndex] = distViaMiddle; - previousVertices[startIndex][endIndex] = middleVertex; + nextVertices[startIndex][endIndex] = middleVertex; } }); }); }); // Shortest distance from x to y: distance[x][y]. - // Previous vertex of shortest path from x to y: previousVertices[x][y]. - return { distances, previousVertices }; + // Next vertex after x one in path from x to y: nextVertices[x][y]. + return { distances, nextVertices }; }