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 <iostream>
#include <limits>
#include <vector>
Include dependency graph for travelling_salesman_using_bit_manipulation.cpp:

Namespaces

namespace  bit_manipulation
 for IO operations
 
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
138 {
139 test(); // run self-test implementations
140 return 0;
141}
static void test()
Self-test implementations.
Definition travelling_salesman_using_bit_manipulation.cpp:99
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
99 {
100 // 1st test-case
102 {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}};
103 uint32_t V = dist.size();
105 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
107 97);
108 std::cout << "1st test-case: passed!"
109 << "\n";
110
111 // 2nd test-case
112 dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
113 V = dist.size();
115 std::vector<uint32_t>(V, -1));
116 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
117 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) ==
118 75);
119 std::cout << "2nd test-case: passed!"
120 << "\n";
121 // 3rd test-case
122 dist = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}};
123 V = dist.size();
125 std::vector<uint32_t>(V, -1));
126 assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
127 travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) ==
128 80);
129
130 std::cout << "3rd test-case: passed!"
131 << "\n";
132}
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:52
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
64{
65 // base case;
66 if (setOfCities == (1 << n) - 1) { // we have covered all the cities
67 return dist[city][0]; // return the cost from the current city to the
68 // original city.
69 }
70
71 if (dp[setOfCities][city] != -1) {
72 return dp[setOfCities][city];
73 }
74 // otherwise try all possible options
75 uint64_t ans = 2147483647;
76 for (int choice = 0; choice < n; choice++) {
77 // check if the city is visited or not.
78 if ((setOfCities & (1 << choice)) ==
79 0) { // this means that this perticular city is not visited.
80 std::uint64_t subProb =
81 dist[city][choice] +
83 dist, setOfCities | (1 << choice), choice, n, dp);
84 // Here we are doing a recursive call to tsp with the updated set of
85 // city/node and choice which tells that where we are currently.
86 ans = std::min(ans, subProb);
87 }
88 }
89 dp[setOfCities][city] = ans;
90 return ans;
91}
T min(T... args)
Here is the call graph for this function: