|
| 1 | +/** |
| 2 | + * Get all possible paths |
| 3 | + * @param {GraphVertex} startVertex |
| 4 | + * @param {GraphVertex[][]} [paths] |
| 5 | + * @param {GraphVertex[]} [path] |
| 6 | + */ |
| 7 | +function findAllPaths(startVertex, paths = [], path = []) { |
| 8 | + // Clone path. |
| 9 | + const currentPath = [...path]; |
| 10 | + |
| 11 | + // Add startVertex to the path. |
| 12 | + currentPath.push(startVertex); |
| 13 | + |
| 14 | + // Generate visited set from path. |
| 15 | + const visitedSet = currentPath.reduce((accumulator, vertex) => { |
| 16 | + const updatedAccumulator = { ...accumulator }; |
| 17 | + updatedAccumulator[vertex.getKey()] = vertex; |
| 18 | + |
| 19 | + return updatedAccumulator; |
| 20 | + }, {}); |
| 21 | + |
| 22 | + // Get all unvisited neighbors of startVertex. |
| 23 | + const unvisitedNeighbors = startVertex.getNeighbors().filter((neighbor) => { |
| 24 | + return !visitedSet[neighbor.getKey()]; |
| 25 | + }); |
| 26 | + |
| 27 | + // If there no unvisited neighbors then treat current path as complete and save it. |
| 28 | + if (!unvisitedNeighbors.length) { |
| 29 | + paths.push(currentPath); |
| 30 | + |
| 31 | + return paths; |
| 32 | + } |
| 33 | + |
| 34 | + // Go through all the neighbors. |
| 35 | + for (let neighborIndex = 0; neighborIndex < unvisitedNeighbors.length; neighborIndex += 1) { |
| 36 | + const currentUnvisitedNeighbor = unvisitedNeighbors[neighborIndex]; |
| 37 | + findAllPaths(currentUnvisitedNeighbor, paths, currentPath); |
| 38 | + } |
| 39 | + |
| 40 | + return paths; |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * @param {number[][]} adjacencyMatrix |
| 45 | + * @param {object} verticesIndices |
| 46 | + * @param {GraphVertex[]} cycle |
| 47 | + * @return {number} |
| 48 | + */ |
| 49 | +function getCycleWeight(adjacencyMatrix, verticesIndices, cycle) { |
| 50 | + let weight = 0; |
| 51 | + |
| 52 | + for (let cycleIndex = 1; cycleIndex < cycle.length; cycleIndex += 1) { |
| 53 | + const fromVertex = cycle[cycleIndex - 1]; |
| 54 | + const toVertex = cycle[cycleIndex]; |
| 55 | + const fromVertexIndex = verticesIndices[fromVertex.getKey()]; |
| 56 | + const toVertexIndex = verticesIndices[toVertex.getKey()]; |
| 57 | + weight += adjacencyMatrix[fromVertexIndex][toVertexIndex]; |
| 58 | + } |
| 59 | + |
| 60 | + return weight; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * BRUTE FORCE approach to solve Traveling Salesman Problem. |
| 65 | + * |
| 66 | + * @param {Graph} graph |
| 67 | + * @return {GraphVertex[]} |
| 68 | + */ |
| 69 | +export default function bfTravellingSalesman(graph) { |
| 70 | + // Pick starting point from where we will traverse the graph. |
| 71 | + const startVertex = graph.getAllVertices()[0]; |
| 72 | + |
| 73 | + // BRUTE FORCE. |
| 74 | + // Generate all possible paths from startVertex. |
| 75 | + const allPossiblePaths = findAllPaths(startVertex); |
| 76 | + |
| 77 | + // Filter out paths that are not cycles. |
| 78 | + const allPossibleCycles = allPossiblePaths.filter((path) => { |
| 79 | + /** @var {GraphVertex} */ |
| 80 | + const lastVertex = path[path.length - 1]; |
| 81 | + const lastVertexNeighbors = lastVertex.getNeighbors(); |
| 82 | + |
| 83 | + return lastVertexNeighbors.includes(startVertex); |
| 84 | + }); |
| 85 | + |
| 86 | + // Go through all possible cycles and pick the one with minimum overall tour weight. |
| 87 | + const adjacencyMatrix = graph.getAdjacencyMatrix(); |
| 88 | + const verticesIndices = graph.getVerticesIndices(); |
| 89 | + let salesmanPath = []; |
| 90 | + let salesmanPathWeight = null; |
| 91 | + for (let cycleIndex = 0; cycleIndex < allPossibleCycles.length; cycleIndex += 1) { |
| 92 | + const currentCycle = allPossibleCycles[cycleIndex]; |
| 93 | + const currentCycleWeight = getCycleWeight(adjacencyMatrix, verticesIndices, currentCycle); |
| 94 | + |
| 95 | + // If current cycle weight is smaller then previous ones treat current cycle as most optimal. |
| 96 | + if (salesmanPathWeight === null || currentCycleWeight < salesmanPathWeight) { |
| 97 | + salesmanPath = currentCycle; |
| 98 | + salesmanPathWeight = currentCycleWeight; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Return the solution. |
| 103 | + return salesmanPath; |
| 104 | +} |
0 commit comments