简体   繁体   中英

Boost Graph Library - Graph with exactly N Edges/Vertex

I am using the BGL adjacency list to portray a very regular graph, where I know exactly, that each vertex will have N edges.

This is my graph definition:

typedef boost::vecS OutEdgeList;
typedef boost::vecS VertexList;
typedef boost::no_property GraphProperties;
typedef boost::vecS EdgeList;

// Graph definition
typedef boost::adjacency_list<
            OutEdgeList,
            VertexList,
            boost::undirectedS,
            VertexProperties,
            EdgeProperties,
            GraphProperties,
            EdgeList
            > Graph;
Graph g;

I use a vector/vecS to store the connections and I suppose by naively calling boost::add_edge the list will get resized in need vector-like by something like twice its last size.

The graph uses a lot of RAM(~12GiB) and I don't want the vector when lets say N=9 to reserve for 16 connections.

Is there a memory efficient way to pre-initialize the adjacency list container like

for(auto edgesOfVert:edgesOfVerts)
    edgesOfVert.reserve(N);

or another way to make use of the regularity of the graph to save memory?

Thank you for any advice!

EDITED: As a possible solution I found out this but I agree with sehe and consider reserving the right amount of edged inelegant, because it tries to force a use on a data structure not made for the particular task I need it for

The real solution here would be not to use boost::adjacency_list . Instead, adapt your own data structures to model the Graph concept you need.

For example, there's the grid_graph<> model in the Boost Graph Library which (if I remember correctly) uses no storage for the vertices at all, and indeed knows exactly how many edges are going to exist between all nodes ahead of time.

You can still use all the graph algorithms that apply to the graph concepts modeled ( grid_graph<> models Incidence Graph , Adjacency Graph , Vertex List Graph , Edge List Graph , Bidirectional Graph , Adjacency Matrix ).

Also consider making the graph memory mapped so you do not have to "load it" fully, or even have to be able to fit it in RAM at any given time (beware of access times though, it will all depend on the access patterns and how you layout the data).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM