|
| 1 | +module.exports = function floydWarshall(graph) { |
| 2 | + // Get all graph vertices. |
| 3 | + const vertices = graph.getAllVertices(); |
| 4 | + |
| 5 | + // Init previous vertices matrix with nulls meaning that there are no |
| 6 | + // previous vertices exist that will give us shortest path. |
| 7 | + const nextVertices = Array(vertices.length).fill(null).map(() => { |
| 8 | + return Array(vertices.length).fill(null); |
| 9 | + }); |
| 10 | + |
| 11 | + // Init distances matrix with Infinities meaning there are no paths |
| 12 | + // between vertices exist so far. |
| 13 | + const distances = Array(vertices.length).fill(null).map(() => { |
| 14 | + return Array(vertices.length).fill(Infinity); |
| 15 | + }); |
| 16 | + |
| 17 | + // Init distance matrix with the distance we already now (from existing edges). |
| 18 | + // And also init previous vertices from the edges. |
| 19 | + vertices.forEach((startVertex, startIndex) => { |
| 20 | + vertices.forEach((endVertex, endIndex) => { |
| 21 | + if (startVertex === endVertex) { |
| 22 | + // Distance to the vertex itself is 0. |
| 23 | + distances[startIndex][endIndex] = 0; |
| 24 | + } else { |
| 25 | + // Find edge between the start and end vertices. |
| 26 | + const edge = graph.findEdge(startVertex, endVertex); |
| 27 | + |
| 28 | + if (edge) { |
| 29 | + // There is an edge from vertex with startIndex to vertex with endIndex. |
| 30 | + // Save distance and previous vertex. |
| 31 | + distances[startIndex][endIndex] = edge.weight; |
| 32 | + nextVertices[startIndex][endIndex] = startVertex; |
| 33 | + } else { |
| 34 | + distances[startIndex][endIndex] = Infinity; |
| 35 | + } |
| 36 | + } |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + // Now let's go to the core of the algorithm. |
| 41 | + // Let's all pair of vertices (from start to end ones) and try to check if there |
| 42 | + // is a shorter path exists between them via middle vertex. Middle vertex may also |
| 43 | + // be one of the graph vertices. As you may see now we're going to have three |
| 44 | + // loops over all graph vertices: for start, end and middle vertices. |
| 45 | + vertices.forEach((middleVertex, middleIndex) => { |
| 46 | + // Path starts from startVertex with startIndex. |
| 47 | + vertices.forEach((startVertex, startIndex) => { |
| 48 | + // Path ends to endVertex with endIndex. |
| 49 | + vertices.forEach((endVertex, endIndex) => { |
| 50 | + // Compare existing distance from startVertex to endVertex, with distance |
| 51 | + // from startVertex to endVertex but via middleVertex. |
| 52 | + // Save the shortest distance and previous vertex that allows |
| 53 | + // us to have this shortest distance. |
| 54 | + const distViaMiddle = distances[startIndex][middleIndex] + distances[middleIndex][endIndex]; |
| 55 | + |
| 56 | + if (distances[startIndex][endIndex] > distViaMiddle) { |
| 57 | + // We've found a shortest pass via middle vertex. |
| 58 | + distances[startIndex][endIndex] = distViaMiddle; |
| 59 | + nextVertices[startIndex][endIndex] = middleVertex; |
| 60 | + } |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + // Shortest distance from x to y: distance[x][y]. |
| 66 | + // Next vertex after x one in path from x to y: nextVertices[x][y]. |
| 67 | + return { distances, nextVertices }; |
| 68 | +}; |
0 commit comments