I am trying to implement the dynamic programming presented in this article to solve the Shortest Hamiltonian Path problem. This solution requires storing values in a 2d array called DP of size n
x2^n
where n
is the number of nodes of the graph.
My graph has more than 100 nodes, but it is very sparse, so most of the elements of the matrix DP is +infinity. Therefore I can store it using a sparse matrix library (and see zero elements as +infinity). For example, using Eigen:
Eigen::SparseMatrix<double> M(n, 1 << n);
M.reserve(5e8); // reserve 4GB of RAM since I'm not able to estimate the number of non-zeros
However, the maximum index of this matrix is 2^n - 1
(in C++ code 2^n
is 1 << n
), which is far larger than what is allowed for an index (if n = 100
for example). The operation 1 << n
is also no longer valid for very large n
as well (I tried cout<<(1 << n)<<endl;
and got 262144 as result for n = 114).
So, while the number of non-infinity elements of my matrix can fit into memory, I don't know how to deal with the indices.
I would appreciate a lot if somebody can help me to solve this problem. Thank you so much in advance!