[BGL] passing a graph from one function to another
Hi all, I have a graph defined as follows (the same as in the Dijkstra shortest path example code): typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, float > > graph_t; The following works fine: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes); However, now I want to do the above line inside a function (say, fun_A), and I want another function (say, fun_B) to have access to "myGraph". How should I do that? I wish I could do something like this: graph_t myGraph; fun_A(graph_t &myGraph) { myGraph(array, edge_array + num_arcs, weights, num_nodes); } fun_B(graph_t &myGraph) { // using myGraph to run some graph algorithm... } but it doesn't work. Any suggestions? Thanks! -- View this message in context: http://www.nabble.com/-BGL--passing-a-graph-from-one-function-to-another-tp2... Sent from the Boost - Users mailing list archive at Nabble.com.
On Nov 19, 2008, at 8:54 AM, r89 wrote:
Hi all,
I have a graph defined as follows (the same as in the Dijkstra shortest path example code):
typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, float > > graph_t;
The following works fine: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
However, now I want to do the above line inside a function (say, fun_A), and I want another function (say, fun_B) to have access to "myGraph".
How should I do that? I wish I could do something like this:
graph_t myGraph;
fun_A(graph_t &myGraph) { myGraph(array, edge_array + num_arcs, weights, num_nodes); }
fun_B(graph_t &myGraph) { // using myGraph to run some graph algorithm... }
If you don't have the graph typedef as a global, then a reasonable option is to use a function template for fun_B: template<typename GraphT> fun_B( GraphT& graph ){ // operate on graph... }
James Sutherland-4 wrote:
On Nov 19, 2008, at 8:54 AM, r89 wrote:
Hi all,
I have a graph defined as follows (the same as in the Dijkstra shortest path example code):
typedef adjacency_list < listS, vecS, directedS, no_property, property < edge_weight_t, float > > graph_t;
The following works fine: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
However, now I want to do the above line inside a function (say, fun_A), and I want another function (say, fun_B) to have access to "myGraph".
How should I do that? I wish I could do something like this:
graph_t myGraph;
fun_A(graph_t &myGraph) { myGraph(array, edge_array + num_arcs, weights, num_nodes); }
fun_B(graph_t &myGraph) { // using myGraph to run some graph algorithm... }
If you don't have the graph typedef as a global, then a reasonable option is to use a function template for fun_B:
template<typename GraphT> fun_B( GraphT& graph ){ // operate on graph... }
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I do have graph typedef as a global. The original problem was, I am not allowed to define graph_t myGraph; without all the arguments like this: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes); Since I don't know the arguments before fun_A, I need something like a "resize" operator, so that i can define graph_t outside the function, but declare it later inside the function. Any suggestions? Thanks! -- View this message in context: http://www.nabble.com/-BGL--passing-a-graph-from-one-function-to-another-tp2... Sent from the Boost - Users mailing list archive at Nabble.com.
I do have graph typedef as a global. The original problem was, I am not allowed to define graph_t myGraph; without all the arguments like this: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
Since I don't know the arguments before fun_A, I need something like a "resize" operator, so that i can define graph_t outside the function, but declare it later inside the function.
You are definitely allowed to declare graph_t without any arguments, and you can use add_vertex and add_edge to insert data as needed. The graph will by dynamically resized as you add elements. Andrew Sutton andrew.n.sutton@gmail.com
Andrew Sutton-2 wrote:
I do have graph typedef as a global. The original problem was, I am not allowed to define graph_t myGraph; without all the arguments like this: graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
Since I don't know the arguments before fun_A, I need something like a "resize" operator, so that i can define graph_t outside the function, but declare it later inside the function.
You are definitely allowed to declare graph_t without any arguments, and you can use add_vertex and add_edge to insert data as needed. The graph will by dynamically resized as you add elements.
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Yes, I tried using add_vertex and add_edge and it works fine. But I assume that add_vertex and add_edge is much slower than using graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes); Is this true or not? Does anyone have experience on that? My graph has over 10^6 edges. Thanks! -- View this message in context: http://www.nabble.com/-BGL--passing-a-graph-from-one-function-to-another-tp2... Sent from the Boost - Users mailing list archive at Nabble.com.
Yes, I tried using add_vertex and add_edge and it works fine. But I assume that add_vertex and add_edge is much slower than using graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
Is this true or not? Does anyone have experience on that? My graph has over 10^6 edges.
It should not be. The cost of insertions is constant. You'll see a slight speedup if you're using VertexList == vecS, but since you're not, it shouldn't matter. Andrew Sutton andrew.n.sutton@gmail.com
On Wed, Nov 19, 2008 at 12:39 PM, r89
Yes, I tried using add_vertex and add_edge and it works fine. But I assume that add_vertex and add_edge is much slower than using graph_t myGraph(array, edge_array + num_arcs, weights, num_nodes);
Is this true or not? Does anyone have experience on that? My graph has over 10^6 edges.
It's possible to make it just as fast using some details of BGL. There is a feature request for .reserve() functionality to be added to BGL, but for now you can just do this: graph.m_vertices.reserve(max_junctions); graph.m_edges.reserve(max_edges); // for directed graphs, also do in_edges graph.m_vertices[i].m_out_edges.reserve(num_edges); With those I can load graphs with close to 2.5 million edges in about 8 seconds (not *too* bad considering the graph is coming from a database). --Michael Fawcett
On Wed, Nov 19, 2008 at 2:19 PM, Michael Fawcett
With those I can load graphs with close to 2.5 million edges in about 8 seconds (not *too* bad considering the graph is coming from a database).
Sorry, I forgot to include timing information for loading a graph without first reserving space. Without calling reserve() ever, it takes me 15 seconds to load the graph. --Michael Fawcett
How should I do that? I wish I could do something like this:
graph_t myGraph;
fun_A(graph_t &myGraph) { myGraph(array, edge_array + num_arcs, weights, num_nodes); }
fun_B(graph_t &myGraph) { // using myGraph to run some graph algorithm... }
You can't invoke a constructor on an object that is already created - well, you probably can, but I seriously doubt that you want to. Instead, just return myGraph from fun_A: graph_t fun_A() { return graph_t(...); } func_B(graph_t& g) { ... } main() { graph_t g = fun_A(); fun_B(g); } It might look like there's an unnecessary copy, but there won't be. Andrew Sutton andrew.n.sutton@gmail.com
Andrew Sutton-2 wrote:
How should I do that? I wish I could do something like this:
graph_t myGraph;
fun_A(graph_t &myGraph) { myGraph(array, edge_array + num_arcs, weights, num_nodes); }
fun_B(graph_t &myGraph) { // using myGraph to run some graph algorithm... }
You can't invoke a constructor on an object that is already created - well, you probably can, but I seriously doubt that you want to. Instead, just return myGraph from fun_A:
graph_t fun_A() { return graph_t(...); }
func_B(graph_t& g) { ... }
main() { graph_t g = fun_A(); fun_B(g); }
It might look like there's an unnecessary copy, but there won't be.
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I tried as you suggested, and it does work in Release mode (Visual Studio 2005), but NOT in Debug mode. Very strange. The CPU keeps running at the line return myGraph; and does not go further. Any suggestions? Thanks! -- View this message in context: http://www.nabble.com/-BGL--passing-a-graph-from-one-function-to-another-tp2... Sent from the Boost - Users mailing list archive at Nabble.com.
main() { graph_t g = fun_A(); fun_B(g); }
I tried as you suggested, and it does work in Release mode (Visual Studio 2005), but NOT in Debug mode. Very strange. The CPU keeps running at the line return myGraph; and does not go further. Any suggestions? Thanks!
Debug it and break the process while its "stuck". That should give you some idea why it may not be working. It sounds like it may be broken copy constructor or some place variable wasn't initialized correctly. It's also hard to say without more context. You might try posting your code or something similar to it. Andrew Sutton andrew.n.sutton@gmail.com
Andrew Sutton-2 wrote:
main() { graph_t g = fun_A(); fun_B(g); }
I tried as you suggested, and it does work in Release mode (Visual Studio 2005), but NOT in Debug mode. Very strange. The CPU keeps running at the line return myGraph; and does not go further. Any suggestions? Thanks!
Debug it and break the process while its "stuck". That should give you some idea why it may not be working. It sounds like it may be broken copy constructor or some place variable wasn't initialized correctly.
It's also hard to say without more context. You might try posting your code or something similar to it.
Andrew Sutton andrew.n.sutton@gmail.com
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi,
As suggested above, in my main function, I first create a graph, and then
pass the graph to the second function. The problem is, the program gets
stuck at the "return" statement in the first function, and never goes to the
second function. The strange thing is, the program works fine in VC2005
Release mode, but not in Debug mode. Here is the code:
main() {
graph_t g = BuildGraphShortestPath(Nnodes, N3mat, Nedge);
RunDijkstra(startPtInd, m_g);
}
graph_t BuildGraphShortestPath(unsigned long Nnodes, vnl_matrix<float>
&N3mat, int Nedge) {
graph_t g(Nnodes);
vertex_desc u, v;
float w = 0.0;
for (int i=0; i
participants (5)
-
Andrew Sutton
-
Dmitry Bufistov
-
James Sutherland
-
Michael Fawcett
-
r89