Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
travelling_salesman_using_bit_manipulation.cpp File Reference

Implementation to [Travelling Salesman problem using bit-masking] (https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/) More...

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <limits>
#include <vector>
Include dependency graph for travelling_salesman_using_bit_manipulation.cpp:

Namespaces

namespace  bit_manipulation
 for assert
 
namespace  travellingSalesman_bitmanipulation
 Functions for the Travelling Salesman Bitmask implementation.
 

Functions

std::uint64_t bit_manipulation::travelling_salesman_using_bit_manipulation::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.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Implementation to [Travelling Salesman problem using bit-masking] (https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/)

Given the distance/cost(as and adjacency matrix) between each city/node to the other city/node , the problem is to find the shortest possible route that visits every city exactly once and returns to the starting point or we can say the minimum cost of whole tour.

Explanation: INPUT -> You are given with a adjacency matrix A = {} which contains the distance between two cities/node.

OUTPUT -> Minimum cost of whole tour from starting point

Worst Case Time Complexity: O(n^2 * 2^n) Space complexity: O(n)

Author
Utkarsh Yadav

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
139 {
140 test(); // run self-test implementations
141 return 0;
142}
static void test()
Self-test implementations.
Definition travelling_salesman_using_bit_manipulation.cpp:100
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
100 {
101 // 1st test-case
103 {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}};
104 uint32_t V = dist.size();
106 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
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();
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();
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}
for std::vector
Definition partition_problem.cpp:39
T size(T... args)
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.
Definition travelling_salesman_using_bit_manipulation.cpp:53
Here is the call graph for this function:

◆ travelling_salesman_using_bit_manipulation()

std::uint64_t bit_manipulation::travelling_salesman_using_bit_manipulation::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.

Parameters
distis the cost to reach between two cities/nodes
setOfCititesrepresents the city in bit form.\
cityis taken to track the current city movement.
nis the no of citys .
dpvector is used to keep a record of state to avoid the recomputation.
Returns
minimum cost of traversing whole nodes/cities from starting point back to starting point
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}
T min(T... args)
Here is the call graph for this function: