|
| 1 | +# Travelling Salesman Problem - Bitmasking Approach |
| 2 | + |
| 3 | +## English |
| 4 | + |
| 5 | +The Travelling Salesman Problem (TSP) is a classic algorithmic problem in the field of computer science and operations research. It focuses on optimization. In this problem, a salesman is given a list of cities and must determine the shortest possible route that visits each city exactly once and returns to the origin city. |
| 6 | + |
| 7 | +This challenge requires implementing the TSP using bitmasking and dynamic programming to efficiently explore all possible routes without redundant calculations. The solution is implemented in JavaScript using Object-Oriented Programming (OOP) principles and follows DRY (Don't Repeat Yourself) best practices. |
| 8 | + |
| 9 | +### Relevant Code Snippet |
| 10 | + |
| 11 | +```javascript |
| 12 | +class TravellingSalesman { |
| 13 | + constructor(distanceMatrix) { |
| 14 | + this.distanceMatrix = distanceMatrix; |
| 15 | + this.n = distanceMatrix.length; |
| 16 | + this.memo = Array.from({ length: this.n }, () => []); |
| 17 | + } |
| 18 | + |
| 19 | + tsp(mask = 1, pos = 0) { |
| 20 | + if (mask === (1 << this.n) - 1) { |
| 21 | + return this.distanceMatrix[pos][0]; |
| 22 | + } |
| 23 | + if (this.memo[pos][mask] !== undefined) { |
| 24 | + return this.memo[pos][mask]; |
| 25 | + } |
| 26 | + |
| 27 | + let minCost = Infinity; |
| 28 | + for (let city = 0; city < this.n; city++) { |
| 29 | + if ((mask & (1 << city)) === 0) { |
| 30 | + const newCost = this.distanceMatrix[pos][city] + this.tsp(mask | (1 << city), city); |
| 31 | + if (newCost < minCost) { |
| 32 | + minCost = newCost; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + this.memo[pos][mask] = minCost; |
| 37 | + return minCost; |
| 38 | + } |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Español |
| 45 | + |
| 46 | +El Problema del Viajante de Comercio (TSP) es un problema clásico en el campo de la informática y la investigación operativa. Se centra en la optimización. En este problema, un viajante debe determinar la ruta más corta posible que visite cada ciudad exactamente una vez y regrese a la ciudad de origen. |
| 47 | + |
| 48 | +Este reto requiere implementar el TSP usando bitmasking y programación dinámica para explorar eficientemente todas las rutas posibles sin cálculos redundantes. La solución está implementada en JavaScript usando principios de Programación Orientada a Objetos (POO) y siguiendo las mejores prácticas de DRY (No te repitas). |
| 49 | + |
| 50 | +### Fragmento de Código Relevante |
| 51 | + |
| 52 | +```javascript |
| 53 | +class TravellingSalesman { |
| 54 | + constructor(distanceMatrix) { |
| 55 | + this.distanceMatrix = distanceMatrix; |
| 56 | + this.n = distanceMatrix.length; |
| 57 | + this.memo = Array.from({ length: this.n }, () => []); |
| 58 | + } |
| 59 | + |
| 60 | + tsp(mask = 1, pos = 0) { |
| 61 | + if (mask === (1 << this.n) - 1) { |
| 62 | + return this.distanceMatrix[pos][0]; |
| 63 | + } |
| 64 | + if (this.memo[pos][mask] !== undefined) { |
| 65 | + return this.memo[pos][mask]; |
| 66 | + } |
| 67 | + |
| 68 | + let minCost = Infinity; |
| 69 | + for (let city = 0; city < this.n; city++) { |
| 70 | + if ((mask & (1 << city)) === 0) { |
| 71 | + const newCost = this.distanceMatrix[pos][city] + this.tsp(mask | (1 << city), city); |
| 72 | + if (newCost < minCost) { |
| 73 | + minCost = newCost; |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + this.memo[pos][mask] = minCost; |
| 78 | + return minCost; |
| 79 | + } |
| 80 | +} |
0 commit comments