Skip to content

Commit 0545555

Browse files
authored
Merge branch 'master' into strassen-multiplication
2 parents 729e874 + a6a9d8e commit 0545555

File tree

9 files changed

+844
-208
lines changed

9 files changed

+844
-208
lines changed

DIRECTORY.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp)
2121
* [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp)
2222
* [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp)
23+
* [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp)
2324

2425
## Ciphers
2526
* [A1Z26 Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/ciphers/a1z26_cipher.cpp)
@@ -66,7 +67,7 @@
6667
* [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp)
6768
* [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp)
6869
* [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp)
69-
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.h)
70+
* [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp)
7071
* [Stack Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_array.cpp)
7172
* [Stack Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_linked_list.cpp)
7273
* [Stack Using Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_queue.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

data_structures/stack.h

-150
This file was deleted.

data_structures/stack.hpp

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @file
3+
* @author danghai
4+
* @author [Piotr Idzik](https://github.com/vil02)
5+
* @brief This class specifies the basic operation on a stack as a linked list
6+
**/
7+
#ifndef DATA_STRUCTURES_STACK_HPP_
8+
#define DATA_STRUCTURES_STACK_HPP_
9+
10+
#include <iostream> /// for IO operations
11+
#include <memory> /// for std::shared_ptr
12+
#include <stdexcept> /// for std::invalid_argument
13+
#include <vector> /// for std::vector
14+
15+
/** Definition of the node as a linked-list
16+
* \tparam ValueType type of data nodes of the linked list should contain
17+
*/
18+
template <class ValueType>
19+
struct node {
20+
ValueType data = {}; ///< data at current node
21+
std::shared_ptr<node<ValueType>> next =
22+
{}; ///< pointer to the next ::node instance
23+
};
24+
25+
template <typename Node, typename Action>
26+
void traverse(const Node* const inNode, const Action& action) {
27+
if (inNode) {
28+
action(*inNode);
29+
traverse(inNode->next.get(), action);
30+
}
31+
}
32+
33+
/** Definition of the stack class
34+
* \tparam value_type type of data nodes of the linked list in the stack should
35+
* contain
36+
*/
37+
template <class ValueType>
38+
class stack {
39+
public:
40+
using value_type = ValueType;
41+
42+
/** Show stack */
43+
void display() const {
44+
std::cout << "Top --> ";
45+
traverse(stackTop.get(), [](const node<value_type>& inNode) {
46+
std::cout << inNode.data << " ";
47+
});
48+
std::cout << std::endl;
49+
std::cout << "Size of stack: " << size << std::endl;
50+
}
51+
52+
std::vector<value_type> toVector() const {
53+
std::vector<value_type> res;
54+
res.reserve(this->size);
55+
traverse(stackTop.get(), [&res](const node<value_type>& inNode) {
56+
res.push_back(inNode.data);
57+
});
58+
return res;
59+
}
60+
61+
private:
62+
void ensureNotEmpty() const {
63+
if (isEmptyStack()) {
64+
throw std::invalid_argument("Stack is empty.");
65+
}
66+
}
67+
68+
public:
69+
/** Determine whether the stack is empty */
70+
bool isEmptyStack() const { return (stackTop == nullptr); }
71+
72+
/** Add new item to the stack */
73+
void push(const value_type& item) {
74+
auto newNode = std::make_shared<node<value_type>>();
75+
newNode->data = item;
76+
newNode->next = stackTop;
77+
stackTop = newNode;
78+
size++;
79+
}
80+
81+
/** Return the top element of the stack */
82+
value_type top() const {
83+
ensureNotEmpty();
84+
return stackTop->data;
85+
}
86+
87+
/** Remove the top element of the stack */
88+
void pop() {
89+
ensureNotEmpty();
90+
stackTop = stackTop->next;
91+
size--;
92+
}
93+
94+
/** Clear stack */
95+
void clear() {
96+
stackTop = nullptr;
97+
size = 0;
98+
}
99+
100+
private:
101+
std::shared_ptr<node<value_type>> stackTop =
102+
{}; /**< Pointer to the stack */
103+
std::size_t size = 0; ///< size of stack
104+
};
105+
106+
#endif // DATA_STRUCTURES_STACK_HPP_

0 commit comments

Comments
 (0)