C++: How to implement sparse matrices with very large indices?

强颜欢笑 提交于 2019-12-11 03:34:19

问题


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 nx2^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!

来源:https://stackoverflow.com/questions/47723270/c-how-to-implement-sparse-matrices-with-very-large-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!