|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Implementation to |
| 4 | + * [Travelling Salesman problem using bit-masking] |
| 5 | + * (https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/) |
| 6 | + * |
| 7 | + * @details |
| 8 | + * Given the distance/cost(as and adjacency matrix) between each city/node to the other city/node , |
| 9 | + * the problem is to find the shortest possible route that visits every city exactly once |
| 10 | + * and returns to the starting point or we can say the minimum cost of whole tour. |
| 11 | + * |
| 12 | + * Explanation: |
| 13 | + * INPUT -> You are given with a adjacency matrix A = {} which contains the distance between two cities/node. |
| 14 | + * |
| 15 | + * OUTPUT -> Minimum cost of whole tour from starting point |
| 16 | + * |
| 17 | + * Worst Case Time Complexity: O(n^2 * 2^n) |
| 18 | + * Space complexity: O(n) |
| 19 | + * @author [Utkarsh Yadav](https://github.com/Rytnix) |
| 20 | + */ |
| 21 | +#include <algorithm> /// for std::min |
| 22 | +#include <cassert> /// for assert |
| 23 | +#include <iostream> /// for IO operations |
| 24 | +#include <vector> /// for std::vector |
| 25 | +#include <limits> /// for limits of integral types |
| 26 | + |
| 27 | +/** |
| 28 | + * @namespace bit_manipulation |
| 29 | + * @brief Bit manipulation algorithms |
| 30 | + */ |
| 31 | +namespace bit_manipulation { |
| 32 | +/** |
| 33 | + * @namespace travellingSalesman_bitmanipulation |
| 34 | + * @brief Functions for the [Travelling Salesman |
| 35 | + * Bitmask](https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/) |
| 36 | + * implementation |
| 37 | + */ |
| 38 | +namespace travelling_salesman_using_bit_manipulation { |
| 39 | +/** |
| 40 | + * @brief The function implements travellingSalesman using bitmanipulation |
| 41 | + * @param dist is the cost to reach between two cities/nodes |
| 42 | + * @param setOfCitites represents the city in bit form.\ |
| 43 | + * @param city is taken to track the current city movement. |
| 44 | + * @param n is the no of citys . |
| 45 | + * @param dp vector is used to keep a record of state to avoid the recomputation. |
| 46 | + * @returns minimum cost of traversing whole nodes/cities from starting point back to starting point |
| 47 | + */ |
| 48 | +std::uint64_t travelling_salesman_using_bit_manipulation(std::vector<std::vector<uint32_t>> dist, // dist is the adjacency matrix containing the distance. |
| 49 | + // setOfCities as a bit represent the cities/nodes. Ex: if setOfCities = 2 => 0010(in binary) |
| 50 | + // means representing the city/node B if city/nodes are represented as D->C->B->A. |
| 51 | + std::uint64_t setOfCities, |
| 52 | + std::uint64_t city, // city is taken to track our current city/node movement,where we are currently. |
| 53 | + std::uint64_t n, // n is the no of cities we have. |
| 54 | + std::vector<std::vector<uint32_t>> &dp) //dp is taken to memorize the state to avoid recomputition |
| 55 | +{ |
| 56 | + //base case; |
| 57 | + if (setOfCities == (1 << n) - 1) // we have covered all the cities |
| 58 | + return dist[city][0]; //return the cost from the current city to the original city. |
| 59 | + |
| 60 | + if (dp[setOfCities][city] != -1) |
| 61 | + return dp[setOfCities][city]; |
| 62 | + //otherwise try all possible options |
| 63 | + uint64_t ans = 2147483647 ; |
| 64 | + for (int choice = 0; choice < n; choice++) { |
| 65 | + //check if the city is visited or not. |
| 66 | + if ((setOfCities & (1 << choice)) == 0 ) { // this means that this perticular city is not visited. |
| 67 | + std::uint64_t subProb = dist[city][choice] + travelling_salesman_using_bit_manipulation(dist, setOfCities | (1 << choice), choice, n, dp); |
| 68 | + // Here we are doing a recursive call to tsp with the updated set of city/node |
| 69 | + // and choice which tells that where we are currently. |
| 70 | + ans = std::min(ans, subProb); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + dp[setOfCities][city] = ans; |
| 75 | + return ans; |
| 76 | +} |
| 77 | +} // namespace travelling_salesman_using_bit_manipulation |
| 78 | +} // namespace bit_manipulation |
| 79 | + |
| 80 | +/** |
| 81 | + * @brief Self-test implementations |
| 82 | + * @returns void |
| 83 | + */ |
| 84 | +static void test() { |
| 85 | + // 1st test-case |
| 86 | + std::vector<std::vector<uint32_t>> dist = { |
| 87 | + {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0} |
| 88 | + }; |
| 89 | + uint32_t V = dist.size(); |
| 90 | + std::vector<std::vector<uint32_t>> dp(1 << V, std::vector<uint32_t>(V, -1)); |
| 91 | + assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) == 97); |
| 92 | + std::cout << "1st test-case: passed!" << "\n"; |
| 93 | + |
| 94 | + // 2nd test-case |
| 95 | + dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}}; |
| 96 | + V = dist.size(); |
| 97 | + std::vector<std::vector<uint32_t>> dp1(1 << V, std::vector<uint32_t>(V, -1)); |
| 98 | + assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) == 75); |
| 99 | + std::cout << "2nd test-case: passed!" << "\n"; |
| 100 | + // 3rd test-case |
| 101 | + dist = { |
| 102 | + {0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0} |
| 103 | + }; |
| 104 | + V = dist.size(); |
| 105 | + std::vector<std::vector<uint32_t>> dp2(1 << V, std::vector<uint32_t>(V, -1)); |
| 106 | + assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) == 80); |
| 107 | + |
| 108 | + std::cout << "3rd test-case: passed!" << "\n"; |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * @brief Main function |
| 114 | + * @returns 0 on exit |
| 115 | + */ |
| 116 | +int main() { |
| 117 | + test(); // run self-test implementations |
| 118 | + return 0; |
| 119 | +} |
0 commit comments