TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
travelling_salesman_using_bit_manipulation.cpp
Go to the documentation of this file.
1
23#include <algorithm>
24#include <cassert>
25#include <cstdint>
26#include <iostream>
27#include <limits>
28#include <vector>
29
34namespace bit_manipulation {
54 std::vector<std::vector<uint32_t>>
55 dist, // dist is the adjacency matrix containing the distance.
56 // setOfCities as a bit represent the cities/nodes. Ex: if
57 // setOfCities = 2 => 0010(in binary) means representing the
58 // city/node B if city/nodes are represented as D->C->B->A.
59 std::uint64_t setOfCities,
60 std::uint64_t city, // city is taken to track our current city/node
61 // movement,where we are currently.
62 std::uint64_t n, // n is the no of cities we have.
63 std::vector<std::vector<uint32_t>>
64 &dp) // dp is taken to memorize the state to avoid recomputition
65{
66 // base case;
67 if (setOfCities == (1 << n) - 1) { // we have covered all the cities
68 return dist[city][0]; // return the cost from the current city to the
69 // original city.
70 }
71
72 if (dp[setOfCities][city] != -1) {
73 return dp[setOfCities][city];
74 }
75 // otherwise try all possible options
76 uint64_t ans = 2147483647;
77 for (int choice = 0; choice < n; choice++) {
78 // check if the city is visited or not.
79 if ((setOfCities & (1 << choice)) ==
80 0) { // this means that this perticular city is not visited.
81 std::uint64_t subProb =
82 dist[city][choice] +
84 dist, setOfCities | (1 << choice), choice, n, dp);
85 // Here we are doing a recursive call to tsp with the updated set of
86 // city/node and choice which tells that where we are currently.
87 ans = std::min(ans, subProb);
88 }
89 }
90 dp[setOfCities][city] = ans;
91 return ans;
92}
93} // namespace travelling_salesman_using_bit_manipulation
94} // namespace bit_manipulation
95
100static void test() {
101 // 1st test-case
102 std::vector<std::vector<uint32_t>> dist = {
103 {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}};
104 uint32_t V = dist.size();
105 std::vector<std::vector<uint32_t>> dp(1 << V, std::vector<uint32_t>(V, -1));
106 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
107 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) ==
108 97);
109 std::cout << "1st test-case: passed!"
110 << "\n";
111
112 // 2nd test-case
113 dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
114 V = dist.size();
115 std::vector<std::vector<uint32_t>> dp1(1 << V,
116 std::vector<uint32_t>(V, -1));
117 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
118 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) ==
119 75);
120 std::cout << "2nd test-case: passed!"
121 << "\n";
122 // 3rd test-case
123 dist = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}};
124 V = dist.size();
125 std::vector<std::vector<uint32_t>> dp2(1 << V,
126 std::vector<uint32_t>(V, -1));
127 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
128 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) ==
129 80);
130
131 std::cout << "3rd test-case: passed!"
132 << "\n";
133}
134
139int main() {
140 test(); // run self-test implementations
141 return 0;
142}
for std::vector
static void test()
Self-test implementations.
std::uint64_t travelling_salesman_using_bit_manipulation(std::vector< std::vector< uint32_t > > dist, std::uint64_t setOfCities, std::uint64_t city, std::uint64_t n, std::vector< std::vector< uint32_t > > &dp)
The function implements travellingSalesman using bitmanipulation.