mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 23:21:18 +08:00
Refactor Floyd-Warshall tests.
This commit is contained in:
parent
2874637d40
commit
fafa52c26e
@ -55,7 +55,7 @@ describe('floydWarshall', () => {
|
|||||||
.addEdge(edgeFG)
|
.addEdge(edgeFG)
|
||||||
.addEdge(edgeEG);
|
.addEdge(edgeEG);
|
||||||
|
|
||||||
const { distances, previousVertices } = floydWarshall(graph);
|
const { distances, nextVertices } = floydWarshall(graph);
|
||||||
|
|
||||||
const vertices = graph.getAllVertices();
|
const vertices = graph.getAllVertices();
|
||||||
|
|
||||||
@ -77,13 +77,13 @@ describe('floydWarshall', () => {
|
|||||||
expect(distances[vertexAIndex][vertexGIndex]).toBe(12);
|
expect(distances[vertexAIndex][vertexGIndex]).toBe(12);
|
||||||
expect(distances[vertexAIndex][vertexFIndex]).toBe(11);
|
expect(distances[vertexAIndex][vertexFIndex]).toBe(11);
|
||||||
|
|
||||||
expect(previousVertices[vertexAIndex][vertexFIndex]).toBe(vertexD);
|
expect(nextVertices[vertexAIndex][vertexFIndex]).toBe(vertexD);
|
||||||
expect(previousVertices[vertexAIndex][vertexDIndex]).toBe(vertexB);
|
expect(nextVertices[vertexAIndex][vertexDIndex]).toBe(vertexB);
|
||||||
expect(previousVertices[vertexAIndex][vertexBIndex]).toBe(vertexA);
|
expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexA);
|
||||||
expect(previousVertices[vertexAIndex][vertexGIndex]).toBe(vertexE);
|
expect(nextVertices[vertexAIndex][vertexGIndex]).toBe(vertexE);
|
||||||
expect(previousVertices[vertexAIndex][vertexCIndex]).toBe(vertexA);
|
expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexA);
|
||||||
expect(previousVertices[vertexAIndex][vertexAIndex]).toBe(null);
|
expect(nextVertices[vertexAIndex][vertexAIndex]).toBe(null);
|
||||||
expect(previousVertices[vertexAIndex][vertexHIndex]).toBe(null);
|
expect(nextVertices[vertexAIndex][vertexHIndex]).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should find minimum paths to all vertices for directed graph', () => {
|
it('should find minimum paths to all vertices for directed graph', () => {
|
||||||
@ -119,7 +119,7 @@ describe('floydWarshall', () => {
|
|||||||
.addEdge(edgeCA)
|
.addEdge(edgeCA)
|
||||||
.addEdge(edgeCD);
|
.addEdge(edgeCD);
|
||||||
|
|
||||||
const { distances, previousVertices } = floydWarshall(graph);
|
const { distances, nextVertices } = floydWarshall(graph);
|
||||||
|
|
||||||
const vertices = graph.getAllVertices();
|
const vertices = graph.getAllVertices();
|
||||||
|
|
||||||
@ -140,11 +140,11 @@ describe('floydWarshall', () => {
|
|||||||
[2, 5, 7, 0],
|
[2, 5, 7, 0],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(previousVertices[vertexAIndex][vertexDIndex]).toBe(vertexC);
|
expect(nextVertices[vertexAIndex][vertexDIndex]).toBe(vertexC);
|
||||||
expect(previousVertices[vertexAIndex][vertexCIndex]).toBe(vertexB);
|
expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexB);
|
||||||
expect(previousVertices[vertexBIndex][vertexDIndex]).toBe(vertexC);
|
expect(nextVertices[vertexBIndex][vertexDIndex]).toBe(vertexC);
|
||||||
expect(previousVertices[vertexAIndex][vertexAIndex]).toBe(null);
|
expect(nextVertices[vertexAIndex][vertexAIndex]).toBe(null);
|
||||||
expect(previousVertices[vertexAIndex][vertexBIndex]).toBe(vertexA);
|
expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexA);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should find minimum paths to all vertices for directed graph with negative edge weights', () => {
|
it('should find minimum paths to all vertices for directed graph with negative edge weights', () => {
|
||||||
@ -188,7 +188,7 @@ describe('floydWarshall', () => {
|
|||||||
.addEdge(edgeCB)
|
.addEdge(edgeCB)
|
||||||
.addEdge(edgeBA);
|
.addEdge(edgeBA);
|
||||||
|
|
||||||
const { distances, previousVertices } = floydWarshall(graph);
|
const { distances, nextVertices } = floydWarshall(graph);
|
||||||
|
|
||||||
const vertices = graph.getAllVertices();
|
const vertices = graph.getAllVertices();
|
||||||
|
|
||||||
@ -208,11 +208,13 @@ describe('floydWarshall', () => {
|
|||||||
expect(distances[vertexFIndex][vertexDIndex]).toBe(9);
|
expect(distances[vertexFIndex][vertexDIndex]).toBe(9);
|
||||||
expect(distances[vertexFIndex][vertexEIndex]).toBe(8);
|
expect(distances[vertexFIndex][vertexEIndex]).toBe(8);
|
||||||
|
|
||||||
expect(previousVertices[vertexFIndex][vertexGIndex]).toBe(null);
|
expect(nextVertices[vertexFIndex][vertexGIndex]).toBe(null);
|
||||||
expect(previousVertices[vertexFIndex][vertexFIndex]).toBe(null);
|
expect(nextVertices[vertexFIndex][vertexFIndex]).toBe(null);
|
||||||
// expect(previousVertices[vertexFIndex][vertexBIndex]).toBe(vertexC);
|
expect(nextVertices[vertexAIndex][vertexBIndex]).toBe(vertexC);
|
||||||
// expect(previousVertices[vertexFIndex][vertexCIndex].getKey()).toBe(vertexA.getKey());
|
expect(nextVertices[vertexAIndex][vertexCIndex]).toBe(vertexA);
|
||||||
// expect(previousVertices[vertexFIndex][vertexAIndex]).toBe(vertexD);
|
expect(nextVertices[vertexFIndex][vertexBIndex]).toBe(vertexE);
|
||||||
// expect(previousVertices[vertexFIndex][vertexDIndex]).toBe(vertexE);
|
expect(nextVertices[vertexEIndex][vertexBIndex]).toBe(vertexD);
|
||||||
|
expect(nextVertices[vertexDIndex][vertexBIndex]).toBe(vertexC);
|
||||||
|
expect(nextVertices[vertexCIndex][vertexBIndex]).toBe(vertexC);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @param {Graph} graph
|
* @param {Graph} graph
|
||||||
* @return {{distances: number[][], previousVertices: GraphVertex[][]}}
|
* @return {{distances: number[][], nextVertices: GraphVertex[][]}}
|
||||||
*/
|
*/
|
||||||
export default function floydWarshall(graph) {
|
export default function floydWarshall(graph) {
|
||||||
// Get all graph vertices.
|
// Get all graph vertices.
|
||||||
@ -8,7 +8,7 @@ export default function floydWarshall(graph) {
|
|||||||
|
|
||||||
// Init previous vertices matrix with nulls meaning that there are no
|
// Init previous vertices matrix with nulls meaning that there are no
|
||||||
// previous vertices exist that will give us shortest path.
|
// 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);
|
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.
|
// There is an edge from vertex with startIndex to vertex with endIndex.
|
||||||
// Save distance and previous vertex.
|
// Save distance and previous vertex.
|
||||||
distances[startIndex][endIndex] = edge.weight;
|
distances[startIndex][endIndex] = edge.weight;
|
||||||
previousVertices[startIndex][endIndex] = startVertex;
|
nextVertices[startIndex][endIndex] = startVertex;
|
||||||
} else {
|
} else {
|
||||||
distances[startIndex][endIndex] = Infinity;
|
distances[startIndex][endIndex] = Infinity;
|
||||||
}
|
}
|
||||||
@ -60,13 +60,13 @@ export default function floydWarshall(graph) {
|
|||||||
if (distances[startIndex][endIndex] > distViaMiddle) {
|
if (distances[startIndex][endIndex] > distViaMiddle) {
|
||||||
// We've found a shortest pass via middle vertex.
|
// We've found a shortest pass via middle vertex.
|
||||||
distances[startIndex][endIndex] = distViaMiddle;
|
distances[startIndex][endIndex] = distViaMiddle;
|
||||||
previousVertices[startIndex][endIndex] = middleVertex;
|
nextVertices[startIndex][endIndex] = middleVertex;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Shortest distance from x to y: distance[x][y].
|
// Shortest distance from x to y: distance[x][y].
|
||||||
// Previous vertex of shortest path from x to y: previousVertices[x][y].
|
// Next vertex after x one in path from x to y: nextVertices[x][y].
|
||||||
return { distances, previousVertices };
|
return { distances, nextVertices };
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user