[英]Modifying vertex properties in a Boost::Graph
我試圖弄清楚如何使用 boost::graph 來存儲一些信息。 但是,我希望將信息綁定到每個頂點。 盯着圖書館的文檔看,要么(a)寫得不好,要么(b),我顯然不像我想象的那樣擅長 C++。 選擇兩個。
我正在尋找一個簡單的示例使用。
捆綁的屬性易於使用:
using namespace boost;
struct vertex_info {
std::string whatever;
int othervalue;
std::vector<int> some_values;
};
typedef adjacency_list<vecS, vecS, undirectedS, vertex_info> graph_t;
graph_t g(n);
g[0].whatever = "Vertex 0";
[...]
等等。
另請參閱文檔。
另一種非常有用的頂點屬性是外部屬性。 您可以聲明適當大小的std::vectors
並將它們用作屬性。
我不喜歡 boost::graph 的嵌套模板屬性方法,所以我為所有內容編寫了一個小包裝器,它基本上允許將任何結構/類作為頂點/邊緣屬性。 可以訪問訪問結構成員的屬性。
為了保持靈活性,這些結構被定義為模板參數。
這里的代碼:
/* definition of basic boost::graph properties */
enum vertex_properties_t { vertex_properties };
enum edge_properties_t { edge_properties };
namespace boost {
BOOST_INSTALL_PROPERTY(vertex, properties);
BOOST_INSTALL_PROPERTY(edge, properties);
}
/* the graph base class template */
template < typename VERTEXPROPERTIES, typename EDGEPROPERTIES >
class Graph
{
public:
/* an adjacency_list like we need it */
typedef adjacency_list<
setS, // disallow parallel edges
listS, // vertex container
bidirectionalS, // directed graph
property<vertex_properties_t, VERTEXPROPERTIES>,
property<edge_properties_t, EDGEPROPERTIES>
> GraphContainer;
/* a bunch of graph-specific typedefs */
typedef typename graph_traits<GraphContainer>::vertex_descriptor Vertex;
typedef typename graph_traits<GraphContainer>::edge_descriptor Edge;
typedef std::pair<Edge, Edge> EdgePair;
typedef typename graph_traits<GraphContainer>::vertex_iterator vertex_iter;
typedef typename graph_traits<GraphContainer>::edge_iterator edge_iter;
typedef typename graph_traits<GraphContainer>::adjacency_iterator adjacency_iter;
typedef typename graph_traits<GraphContainer>::out_edge_iterator out_edge_iter;
typedef typename graph_traits<GraphContainer>::degree_size_type degree_t;
typedef std::pair<adjacency_iter, adjacency_iter> adjacency_vertex_range_t;
typedef std::pair<out_edge_iter, out_edge_iter> out_edge_range_t;
typedef std::pair<vertex_iter, vertex_iter> vertex_range_t;
typedef std::pair<edge_iter, edge_iter> edge_range_t;
/* constructors etc. */
Graph()
{}
Graph(const Graph& g) :
graph(g.graph)
{}
virtual ~Graph()
{}
/* structure modification methods */
void Clear()
{
graph.clear();
}
Vertex AddVertex(const VERTEXPROPERTIES& prop)
{
Vertex v = add_vertex(graph);
properties(v) = prop;
return v;
}
void RemoveVertex(const Vertex& v)
{
clear_vertex(v, graph);
remove_vertex(v, graph);
}
EdgePair AddEdge(const Vertex& v1, const Vertex& v2, const EDGEPROPERTIES& prop_12, const EDGEPROPERTIES& prop_21)
{
/* TODO: maybe one wants to check if this edge could be inserted */
Edge addedEdge1 = add_edge(v1, v2, graph).first;
Edge addedEdge2 = add_edge(v2, v1, graph).first;
properties(addedEdge1) = prop_12;
properties(addedEdge2) = prop_21;
return EdgePair(addedEdge1, addedEdge2);
}
/* property access */
VERTEXPROPERTIES& properties(const Vertex& v)
{
typename property_map<GraphContainer, vertex_properties_t>::type param = get(vertex_properties, graph);
return param[v];
}
const VERTEXPROPERTIES& properties(const Vertex& v) const
{
typename property_map<GraphContainer, vertex_properties_t>::const_type param = get(vertex_properties, graph);
return param[v];
}
EDGEPROPERTIES& properties(const Edge& v)
{
typename property_map<GraphContainer, edge_properties_t>::type param = get(edge_properties, graph);
return param[v];
}
const EDGEPROPERTIES& properties(const Edge& v) const
{
typename property_map<GraphContainer, edge_properties_t>::const_type param = get(edge_properties, graph);
return param[v];
}
/* selectors and properties */
const GraphContainer& getGraph() const
{
return graph;
}
vertex_range_t getVertices() const
{
return vertices(graph);
}
adjacency_vertex_range_t getAdjacentVertices(const Vertex& v) const
{
return adjacent_vertices(v, graph);
}
int getVertexCount() const
{
return num_vertices(graph);
}
int getVertexDegree(const Vertex& v) const
{
return out_degree(v, graph);
}
/* operators */
Graph& operator=(const Graph &rhs)
{
graph = rhs.graph;
return *this;
}
protected:
GraphContainer graph;
};
使用它,您可以訪問如下屬性:
struct VertexProperties {
int i;
};
struct EdgeProperties {
};
typedef Graph<VertexProperties, EdgeProperties> MyGraph;
MyGraph g;
VertexProperties vp;
vp.i = 42;
MyGraph::Vertex v = g.AddVertex(vp);
g.properties(v).i = 23;
當然,您可能對圖形結構有其他需求,但是修改上面的代碼應該很容易。
下面是我用來將一些屬性附加到頂點、邊和圖形的代碼。 請注意,頂點名稱和圖形名稱是預定義的屬性(完整列表請參見 boost/properties.hpp),因此已經定義了vertex_name_t
和graph_name_t
。 但是, vertex_location_t
、 edge_length_t
和graph_notes_t
是我自己的屬性,因此需要定義。 我從各種示例和文檔中拼湊出這段代碼,我不確定BOOST_INSTALL_PROPERTY
究竟做了什么,但代碼似乎工作正常。
// Define custom properties
enum vertex_location_t { vertex_location };
enum edge_length_t { edge_length };
enum graph_notes_t { graph_notes };
namespace boost
{
BOOST_INSTALL_PROPERTY(vertex, location);
BOOST_INSTALL_PROPERTY(edge, length );
BOOST_INSTALL_PROPERTY(graph, notes );
}
// Define vertex properties: vertex name and location
typedef property<vertex_name_t, string,
property<vertex_location_t, Point3> >
VertexProperties;
// Define edge properties: length
typedef property<edge_length_t, double> EdgeProperties;
// Define graph properties: graph name and notes
typedef property<graph_name_t, string,
property<graph_notes_t, string> >
GraphProperties;
// Define a graph type
typedef adjacency_list
<
vecS, // edge container type
vecS, // vertex container type
undirectedS,
VertexProperties,
EdgeProperties,
GraphProperties
> Graph;
我認為 Boost.Graph 有一個非常好的文檔,但對於這件事的初學者來說並不是真正的。 所以這里有一個例子,我希望它足夠簡單!
//includes
// Create a name for your information
struct VertexInformation
{
typedef boost::vertex_property_type type;
};
// Graph type, customize it to your needs
// This is when you decide what information will be attached to vertices and/or edges
// of MyGraph objects
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
boost::property<VertexInformation, double> > MyGraph;
int main()
{
MyGraph graph;
// Create accessor for information
typedef boost::property_map<MyGraph, VertexInformation>::type InformationAccessor;
InformationAccessor information( get( VertexInformation(), graph ) );
// Create a vertex (for example purpose)
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;
MyVertex vertex = add_vertex( graph );
// Now you can access your information
put( information, vertex, 1. );
// returns 1 !
get( information, vertex );
return 0;
}
我發現這些例子非常有用。 在 windows 上,它將位於您的 \Program Files\boost\boost_1_38\libs\graph\example 目錄中。
kevin_bacon2.cpp 使用頂點屬性來存儲演員的名字。
您的頂點和邊屬性可以存儲在常規結構或類中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.