diff --git a/annotated.html b/annotated.html index 257a39f03..4ae138a8d 100644 --- a/annotated.html +++ b/annotated.html @@ -3,7 +3,7 @@ - + Algorithms_in_C++: Class List @@ -41,7 +41,7 @@ MathJax.Hub.Config({ - + + + + + + + + + + + + + + +
+
+ + + + + + +
+
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 <vector>
+#include <limits>
+
+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
+
116 {
+
117 test(); // run self-test implementations
+
118 return 0;
+
119}
+
static void test()
Self-test implementations.
Definition: travelling_salesman_using_bit_manipulation.cpp:84
+
+Here is the call graph for this function:
+
+
+
+
+ +
+
+ +

◆ test()

+ +
+
+ + + + + +
+ + + + + + + +
static void test ()
+
+static
+
+ +

Self-test implementations.

+
Returns
void
+
84 {
+
85 // 1st test-case
+ +
87 {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}
+
88 };
+
89 uint32_t V = dist.size();
+ +
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();
+ +
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();
+ +
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}
+ +
for std::vector
Definition: partition_problem.cpp:39
+
T size(T... args)
+ +
+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
+
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}
+ +
T min(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:48
+
+Here is the call graph for this function:
+
+
+
+
+ +
+
+
+
+ + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.js b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.js new file mode 100644 index 000000000..a760343e5 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.js @@ -0,0 +1,6 @@ +var travelling__salesman__using__bit__manipulation_8cpp = +[ + [ "main", "d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4", null ], + [ "test", "d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.html#aa8dca7b867074164d5f45b0f3851269d", null ], + [ "travelling_salesman_using_bit_manipulation", "d4/d8f/travelling__salesman__using__bit__manipulation_8cpp.html#ad08f082be02c3437c2fe89cb035fcee1", null ] +]; \ No newline at end of file diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map new file mode 100644 index 000000000..dc9f10ea0 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.map @@ -0,0 +1,4 @@ + + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 new file mode 100644 index 000000000..92d0199fa --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.md5 @@ -0,0 +1 @@ +c9ad688e352e8ccd9832494ce18d9bbf \ No newline at end of file diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg new file mode 100644 index 000000000..fd07d0fd6 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg @@ -0,0 +1,36 @@ + + + + + + +test + + +Node1 + + +test + + + + + +Node2 + + +std::vector::size + + + + + +Node1->Node2 + + + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.map b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.map new file mode 100644 index 000000000..68647c917 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.map @@ -0,0 +1,4 @@ + + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.md5 b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.md5 new file mode 100644 index 000000000..459afa83e --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.md5 @@ -0,0 +1 @@ +51b5cb6e681c1c05c736d3e602345d08 \ No newline at end of file diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.svg b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.svg new file mode 100644 index 000000000..af3ee1896 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ad08f082be02c3437c2fe89cb035fcee1_cgraph.svg @@ -0,0 +1,45 @@ + + + + + + +bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation + + +Node1 + + +bit_manipulation::travelling +_salesman_using_bit_manipulation +::travelling_salesman_using_bit +_manipulation + + + + + +Node1->Node1 + + + + + +Node2 + + +std::min + + + + + +Node1->Node2 + + + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map new file mode 100644 index 000000000..c0b1ebdc2 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.map @@ -0,0 +1,5 @@ + + + + + diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 new file mode 100644 index 000000000..bbc04f586 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.md5 @@ -0,0 +1 @@ +eb1f4b315113057baa73b52f56cdfc81 \ No newline at end of file diff --git a/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg new file mode 100644 index 000000000..954771974 --- /dev/null +++ b/d4/d8f/travelling__salesman__using__bit__manipulation_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg @@ -0,0 +1,51 @@ + + + + + + +main + + +Node1 + + +main + + + + + +Node2 + + +test + + + + + +Node1->Node2 + + + + + +Node3 + + +std::vector::size + + + + + +Node2->Node3 + + + + + diff --git a/d4/d90/classdata__structures_1_1_skip_list.html b/d4/d90/classdata__structures_1_1_skip_list.html index 356e670c1..d2d5be523 100644 --- a/d4/d90/classdata__structures_1_1_skip_list.html +++ b/d4/d90/classdata__structures_1_1_skip_list.html @@ -3,7 +3,7 @@ - + Algorithms_in_C++: data_structures::SkipList Class Reference @@ -41,7 +41,7 @@ MathJax.Hub.Config({ - + + + + + + + + + + + + + + +
+
+ + + + + + +
+
Algorithms_in_C++ 1.0.0 +
+
Set of algorithms implemented in C++.
+
+
+ + + + + + + +
+
+ +
+
+
+ +
+ +
+
+ + +
+
+
+
+
+
Loading...
+
Searching...
+
No Matches
+
+
+
+
+ +
+
travellingSalesman_bitmanipulation Namespace Reference
+
+
+ +

Functions for the Travelling Salesman Bitmask implementation. +More...

+

Detailed Description

+

Functions for the Travelling Salesman Bitmask implementation.

+
+
+ + + + diff --git a/d7/d40/class_solution__coll__graph.svg b/d7/d40/class_solution__coll__graph.svg index 4823f3318..a36049418 100644 --- a/d7/d40/class_solution__coll__graph.svg +++ b/d7/d40/class_solution__coll__graph.svg @@ -1,7 +1,7 @@ - - - + Algorithms_in_C++: XOR Namespace Reference @@ -41,7 +41,7 @@ MathJax.Hub.Config({ - +