I have a static graph (the topology does not change over time and is known at compile time) where each node in the graph can have one of three states. I then simulate a dynamic where a node has a probability of changing its state over time, and this probability depends on the state of its neighbors. As the graph grows larger the simulations start getting very slow, but after some profiling, I identified that most of the computation time was spent iterating over the list of neighbors.
I was able to improve the speed of the simulations by changing the data structure used to access neighbors in the graph but was wondering if there are better (faster) ways to do it. My current implementation goes like this:
For a graph with N
nodes labeled from 0
to N-1
and average number of neighbors of K
, I store each state as an integer in an std::vector<int> states
and the number of neighbors for each node in std::vector<int> number_of_neighbors
.
To store neighbors information I created two more vectors: std::vector<int> neighbor_lists
which stores, in order, the nodes that are neighbors to node 0
, node 1
, ... , node N
, and an index vector std::vector<int> index
which stores, for each node, the index of its first neighbor in neighbor_lists
.
So I have four vectors in total:
printf( states.size() ); // N
printf( number_of_neighbors.size() ); // N
printf( neighbor_lists.size() ); // N * k
printf( index.size() ); // N
When updating node i
I access its neighbors like so:
// access neighbors of node i:
for ( int s=0; s<number_of_neighbors[i]; s++ ) {
int neighbor_node = neighbor_lists[index[i] + s];
int state_of_neighbor = states[neighbor_node];
// use neighbor state for stuff...
}
To sum up my question then: is there a faster implementation for accessing neighboring nodes in a fixed graph structure?
Currently, I've gone up to N = 5000 for a decent number of simulation time, but I was aiming for N ~ 15.000 if at all possible.