I want to have two types of graph (with different bundled properties, simplified below). One of the types is used to represent subgraphs of the other type, so i want to be able to map the vertices and edges of the subgraphs to their corresponding vertices and edges in the original graph.
`// main graph structure
struct VertexProperties
{
...
};
struct EdgeProperties{
double cost;
...
};
using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VertexProperties, EdgeProperties>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using Edge = boost::graph_traits<Graph>::edge_descriptor;
// subgraph structure
struct SubVertexProperties
{
Vertex original_vertex;
};
struct SubEdgeProperties{
Edge original_edge;
...
};
using SubGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, SubVertexProperties, SubEdgeProperties>;
using SubVertex = boost::graph_traits<Graph>::vertex_descriptor;
using SubEdge = boost::graph_traits<Graph>::edge_descriptor;
I create one “original” graph of type Graph and many subgraphs of type SubGraph and at that point i can access edges of the original graph as i expect:
Graph g = ...;
SubGraph sg = ...;
vector<int> sg_to_g_vertex; // maps each vertex in the subgraph to the corresponding vertex in original graph
for (auto e : make_iterator_range(edges(g))) {
Vertex u = source(e, g);
Vertex v = target(e, g);
if (someCondition(u,v)) {
boost::add_edge(vertex_map[u], vertex_map[v], {e, ...}, com_graph);
}
}
for (auto e : make_iterator_range(edges(sg))
{
std::cout << g[sg[e].original_edge].cost << std::endl;
}
This will print out the cost of the edge in the original graph that corresponds to each edge in the subgraph. Next i store both the original graph and the subgraphs in a struct which is passed around in my code. Once i pass this struct to another function, executing a loop like the last one above results in gibberish values. But if i print out the edges of my original and subgraphs and the associated bundled properties, they look just fine. So i assume the Edge identifiers original_edge
are not valid anymore. Is there a way to store such identifiers that they remain useable?
If it helps, after initial construction i do not add or delete vertices from either of these graphs.
I tried reading the boost documentation and all stackoverflow posts that sounded related, but i do not really understand what type these edge identifiers have and how i could obtain more stable ones.