1

is it possible to create a bit adjacency array for a graph with c++11 in 64 bit system

I need to deploy a graph with large size, say N=10000000. But the compiler do not allowed me to define a two dimension, large size array, like Adj[N][N]. I just deploy graph with which weight is 0. If two nodes have a link, the correspondent element of Adj[N][N] is 1, otherwise 0. So I just need one bit to do this. I was wondering whether I can use a long bit memory to store a bit array to describe a two dimension array.

For example:

4X4 adjacency matrix with 16 bit array: 1010010100110110

So that element 1,1 is 1, element 1,2 is 0, ......, element 4,4 is 0

I need bit operation to find the element that I need to deploying.

The problem is how to do this? How to allocate a long continuous bit storage in memory? malloc? how to define the pointer? void type?

EDIT

Let's say 10000000. Maybe 90T memory for unsigned char. I knew that there is someone deploying 10^7 nodes. Maybe they are using vector or list which is dynamic allocation memory.

Nick Dong
  • 3,638
  • 8
  • 47
  • 84
  • 2
    If `N=10000000000`, you'd need approximately `1e10` *gigabytes* to store N^2 bits. – HolyBlackCat Jun 16 '18 at 10:35
  • 1
    Wrap a `std::bitset bits;` in a class, and make a `at(N1, N2)` function that returns `bits[N1 + N2*N]`. If `N` is a runtime value you can use `std::vector`. – super Jun 16 '18 at 11:02
  • Let's say 10000000. Maybe 90T memory for unsigned char. I knew that there is someone deploying 10^7 nodes. Maybe they are using vector or list which is dynamic allocation memory. @HolyBlackCat – Nick Dong Jun 16 '18 at 11:16
  • With one bit per edge you need approx. 1,5 TB. You'll need a sparse matrix. – joe_chip Jun 16 '18 at 11:22
  • There are length limits either. In my current OSX 10.9.4.,Segmentation fault: 11 for longer than 10^7 bits. @super – Nick Dong Jun 16 '18 at 11:51
  • @NickDong After your edit it's clear that your problem is the big size and not the basic method. So my comment is not a good option for you. – super Jun 16 '18 at 11:58
  • 1
    Interesting read: https://stackoverflow.com/questions/50323522/any-optimization-for-random-access-on-a-very-big-array-when-the-value-in-95-of – Joseph D. Jun 16 '18 at 13:37

2 Answers2

2

You need a "sparse array", where just a few elements are actually filled. I would first think of using std::map, like this:

#include <map>
class MyNode {
// your stuff here
};
typedef <your node id type> MyNodeId; // must naturally or by overload define an operator <

typedef std::map<MyNodeId, MyNode> ArcsFromNode;

typedef std:map<MyNodeId, ArcsFromNode> MyGraph;
rpaulin56
  • 436
  • 8
  • 14
  • Of course, if youre nodes are allocated somewhere else, you can use MyNode * instead of "MyNode" in the ArcsFromNode map – rpaulin56 Jun 16 '18 at 11:15
1

most entries in your adjecentcy matrix are propably 0, so you need a so-called "sparse matrix" to save memory.

the basic idea is to store element-maps, so instead of storing the big matrix it just stores the non-zero entries.

Boost has some implementations of such thing which could save you some implmentation time and testing, see https://www.boost.org/doc/libs/1_45_0/libs/numeric/ublas/doc/matrix_sparse.htm for an overview.

skeller
  • 1,151
  • 6
  • 6