3rdParty/boost/1.78.0/libs/graph/doc/grid_graph.html
A grid_graph represents a multi-dimensional, rectangular grid of vertices with user-defined dimension lengths and wrapping.
grid_graph models:
Defined in boost/graph/grid_graph.hpp with all functions in the boost namespace. A simple examples of creating and iterating over a grid_graph is available here libs/graph/example/grid_graph_example.cpp. An example of adding properties to a grid_graph is also available libs/graph/example/grid_graph_properties.cpp
template<std::size\_tDimensions,typenameVertexIndex=std::size\_t,typenameEdgeIndex=VertexIndex>classgrid_graph;
The constructor to grid_graph has several overloads to aid in configuring each dimension:
// Defines a grid\_graph that does not wrap.grid\_graph<...>(boost:array<VertexIndex,Dimensions> dimension_lengths);// Defines a grid\_graph where all dimensions are either wrapped or unwrapped.grid\_graph<...>(boost:array<VertexIndex,Dimensions> dimension_lengths,boolwrap_all_dimensions);// Defines a grid\_graph where the wrapping for each dimension is specified individually.grid\_graph<...>(boost:array<VertexIndex,Dimensions> dimension_lengths,boost:array<bool,Dimensions> wrap_dimension);
// Define dimension lengths, a 3x3 in this caseboost::array<std::size\_t,2> lengths = { {3,3} };// Create a 3x3 two-dimensional, unwrapped grid graph (Figure 1)grid\_graph<2> graph(lengths);// Create a 3x3 two-dimensional, wrapped grid graph (Figure 2)grid\_graph<2> graph(lengths,true);
_ Figure 1: A 3x3 two-dimensional, unwrapped grid graph_
_ Figure 2: A 3x3 two-dimensional, wrapped grid graph_
The grid_graph supports addressing vertices and edges by index. The following functions allow you to convert between vertices, edges, and their associated indices:
typedefgrid\_graph<...>Graph;typedefgraph\_traits<Graph>Traits;// Get the vertex associated with vertex\_indexTraits::vertex\_descriptorvertex(Traits::vertices\_size\_typevertex_index,constGraph&graph);// Get the index associated with vertexTraits::vertices\_size\_typeget(boost::vertex\_index\_t,constGraph&graph,Traits::vertex\_descriptorvertex);// Get the edge associated with edge\_indexTraits::edge\_descriptoredge_at(Traits::edges\_size\_typeedge_index,constGraph&graph);// Get the index associated with edgeTraits::edges\_size\_typeget(boost::edge\_index\_t,constGraph&graph,Traits::edge\_descriptoredge);// Get the out-edge associated with vertex and out\_edge\_indexTraits::edge\_descriptorout_edge_at(Traits::vertex\_descriptorvertex,Traits::degree\_size\_typeout_edge_index,constGraph&graph);// Get the out-edge associated with vertex and in\_edge\_indexTraits::edge\_descriptorin_edge_at(Traits::vertex\_descriptorvertex,Traits::degree\_size\_typein_edge_index,constGraph&graph);
typedefgrid\_graph<2>Graph;typedefgraph\_traits<Graph>Traits;// Create a 3x3, unwrapped grid\_graph (Figure 3)boost::array<std::size\_t,2> lengths = { {3,3} };Graphgraph(lengths);// Do a round-trip test of the vertex index functionsfor(Traits::vertices\_size\_typev_index =0;
v_index < num_vertices(graph); ++v_index) {// The two indices should always be equalstd::cout <<"Index of vertex "<< v_index <<" is "<<
get(boost::vertex_index, graph, vertex(v_index, graph)) <<std::endl;
}// Do a round-trip test of the edge index functionsfor(Traits::edges\_size\_typee_index =0;
e_index < num_edges(graph); ++e_index) {// The two indices should always be equalstd::cout <<"Index of edge "<< e_index <<" is "<<
get(boost::edge_index, graph, edge_at(e_index, graph)) <<std::endl;
}
_ Figure 3: 3x3 unwrapped grid_graph with vertex and edge indices shown._
There are several grid_graph specific member functions available:
typedefgrid\_graph<...>Graph;typedefgraph\_traits<Graph>Traits;// Returns the number of dimensionsstd::size\_tdimensions();// Returns the length of a dimensionTraits::vertices\_size\_typelength(std::size\_tdimension);// Returns true if the dimension wraps, false if notboolwrapped(std::size\_tdimension);// Returns the "next" vertex in a dimension at a given distance. If the dimension // is unwrapped, next will stop at the last vertex in the dimension. Traits::vertex\_descriptornext(Traits::vertex\_descriptorvertex,std::size\_tdimension,Traits::vertices\_size\_typedistance =1);// Returns the "previous" vertex in a dimension at a given distance. If the // dimension is unwrapped, previous will stop at the beginning vertex in the dimension. Traits::vertex\_descriptorprevious(Traits::vertex\_descriptorvertex,std::size\_tdimension,Traits::vertices\_size\_typedistance =1);
typedefgrid\_graph<3>Graph;typedefgraph\_traits<Graph>Traits;// Define a 3x5x7 grid\_graph where the second dimension doesn't wrapboost::array<std::size\_t,3> lengths = { {3,5,7} };boost::array<bool,3> wrapped = { {true,false,true} };Graphgraph(lengths, wrapped);// Print number of dimensionsstd::cout << graph.dimensions() <<std::endl;// prints "3"// Print dimension lengths (same order as in the lengths array)std::cout << graph.length(0) <<"x"<< graph.length(1) <<"x"<< graph.length(2) <<std::endl;// prints "3x5x7"// Print dimension wrapping (W = wrapped, U = unwrapped)std::cout << graph.wrapped(0) ?"W":"U"<<", "<<
graph.wrapped(1) ?"W":"U"<<", "<<
graph.wrapped(2) ?"W":"U"<<std::endl;// prints "W, U, W"// Define a simple function to print verticesvoidprint_vertex(Traits::vertex\_descriptorvertex_to_print) {std::cout <<"("<< vertex_to_print[0] <<", "<< vertex_to_print[1] <<", "<< vertex_to_print[2] <<")"<<std::endl;
}// Start with the first vertex in the graphTraits::vertex\_descriptorfirst_vertex = vertex(0, graph);
print_vertex(first_vertex);// prints "(0, 0, 0)"// Print the next vertex in dimension 0print_vertex(graph.next(first_vertex,0));// prints "(1, 0, 0)"// Print the next vertex in dimension 1print_vertex(graph.next(first_vertex,1));// prints "(0, 1, 0)"// Print the 5th next vertex in dimension 2print_vertex(graph.next(first_vertex,2,5));// prints "(0, 0, 5)"// Print the previous vertex in dimension 0 (wraps)print_vertex(graph.previous(first_vertex,0));// prints "(2, 0, 0)"// Print the previous vertex in dimension 1 (doesn't wrap, so it's the same)print_vertex(graph.previous(first_vertex,1));// prints "(0, 0, 0)"// Print the 20th previous vertex in dimension 2 (wraps around twice)print_vertex(graph.previous(first_vertex,2,20));// prints "(0, 0, 1)"
Copyright © 2009 Trustees of Indiana University