
Hello, I Have 2 graphs: graph1 and graph2, and two sets of edge descriptors. One set is associated to edges of graph1 and another set associated to edges of graph2. I don't understand why is it possible to access information about edges of graph2 by using graph1 and the edge descriptors of graph2. I realized of that because of an own mistake when using graphs. What I would like to know is why is that possible to do and if it should be an exception that can handle this situation. I send the code I was using to test this: #include <iostream> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/graphviz.hpp> #include <fstream> #include "GraphInfo.h" using namespace std; using namespace boost; typedef adjacency_list<setS, vecS, bidirectionalS, VertexInfo, EdgeInfo> Graph; void printGraph(Graph graph, string file) { ofstream ofs(file.c_str()); dynamic_properties dp; dp.property("node_id", get(vertex_index, graph)); dp.property("label", get(&EdgeInfo::weight, graph)); write_graphviz_dp(ofs, graph, dp); } int main() { Graph graph1; Graph::vertex_descriptor a1 = add_vertex(graph1); Graph::vertex_descriptor b1 = add_vertex(graph1); Graph::vertex_descriptor c1 = add_vertex(graph1); Graph::edge_descriptor e1 = (add_edge(a1, b1, graph1)).first; Graph::edge_descriptor e2 = (add_edge(b1, a1, graph1)).first; graph1[a1].id = 10; graph1[b1].id = 20; graph1[c1].id = 30; graph1[e1].weight = 1; graph1[e2].weight = 2; Graph graph2; Graph::vertex_descriptor a2 = add_vertex(graph2); Graph::vertex_descriptor b2 = add_vertex(graph2); Graph::vertex_descriptor c2 = add_vertex(graph2); Graph::vertex_descriptor d2 = add_vertex(graph2); Graph::edge_descriptor e4 = (add_edge(c2, b2, graph2)).first; Graph::edge_descriptor e5 = (add_edge(b2, c2, graph2)).first; Graph::edge_descriptor e6 = (add_edge(d2, a2, graph2)).first; Graph::edge_descriptor e7 = (add_edge(a2, d2, graph2)).first; graph2[a2].id = 10; graph2[b2].id = 20; graph2[c2].id = 30; graph2[e4].weight = 13; graph2[e5].weight = 14; graph2[e6].weight = 15; graph2[e7].weight = 16; // WHY IS THIS POSSIBLE TO DO // IT IS ASKING FOR AN EDGE OF GRAPH2 BUT USING GRAPH1 ??? double weight = graph1[e7].weight; cout<<"weight: "<<weight<<endl; printGraph(graph1, "graph1.dot"); printGraph(graph2, "graph2.dot"); cout << "end" << endl; return 0; } Thank you very much

On Wednesday, June 11, 2014 09:36 PM, Pablo Madoery wrote:
Hello, I Have 2 graphs: graph1 and graph2, and two sets of edge descriptors. One set is associated to edges of graph1 and another set associated to edges of graph2. I don't understand why is it possible to access information about edges of graph2 by using graph1 and the edge descriptors of graph2.
If the type of the vertex_descriptor is void*, internally, it's probably just cast to some node in the graph. Just because the interface takes a graph and a vertex_descriptor pair, doesn't mean it uses the graph you passed*. I guess the same would happen with a std::list<iterator>, if you use an iterator from the wrong list, the next one would still point into the wrong list, right? It's undefined behaviour, and not all operations are likely to work, but some might, as you've seen. Ben * It looks like you're using a member of the graph in that interface, but if the implementation of that member doesn't use the this pointer, it can still work.
participants (2)
-
Ben Pope
-
Pablo Madoery