如何访问提升子图'graph'属性?

How to access boost subgraph 'graph' properties?

本文关键字:graph 属性 子图 何访问 访问      更新时间:2023-10-16

我正在使用adjacency_list和子图适配器来创建我的图类型。

#include <boost/graph/subgraph.hpp>
#include <boost/graph/adjacency_list.hpp>
struct VertexProperties
{
    bool bIsExpandable;          
    string sId;
    string sCoord_X;
    string sCoord_Y;
    std::size_t order;
};
struct EdgeProperties
{
    string sId;
    bool bBidirectional;
};
//Graph properties
enum graph_index_t {graph_index=111};
namespace boost{
BOOST_INSTALL_PROPERTY(graph,index);
}
typedef boost::property<boost::vertex_index_t, std::size_t , VertexProperties> vertex_prop;
typedef boost::property<boost::edge_index_t, std::size_t , EdgeProperties> edge_prop;
typedef boost::property<graph_index_t, std::size_t> graph_prop;
typedef boost::adjacency_list<
boost::listS,
boost::vecS,
boost::bidirectionalS,
vertex_prop ,
edge_prop,
graph_prop>
Graph;
typedef boost::subgraph<Graph> Subgraph;

我正在为顶点和边使用捆绑属性。我尝试将捆绑属性赋予"graph",adjacency_list它工作正常但不能用于子图适配器,我发现提升子图适配器不支持它。所以我在图形属性中添加了graph_index_t,但我无法访问它。我已经写了以下属性映射来访问它,但似乎它不是正确的方式。

typedef property_map<Subgraph , graph_index_t>::type GraphIndexPropertyMap;

它在 adjacency_list.hpp 中给出错误

d:boost_1_53_0boostgraphdetailadjacency_list.hpp:2543: error: forming reference to void

我已经检查了boost 1.53文档,但找不到与此相关的方法。

所以我有两个问题:

1) 如何获得对graph_index属性的读写访问权限?

2)我可以以某种方式将捆绑属性用于带有提升子图的"图"吗?

谁能帮忙?

谢谢

普拉提克

这个答案来得很晚!但是我遇到了同样的问题,花了一段时间才弄清楚,所以我想分享我的解决方案。

捆绑属性不是一种选择。文档中没有任何内容说明为什么以下内容不起作用。但是,子图实现似乎不兼容,因为它无法编译。

Graph = boost::subgraph<boost::adjacency_list<
        boost::vecS,
        boost::vecS,
        boost::directedS,
        boost::property<boost::vertex_index_t, std::size_t>,
        boost::property<boost::edge_index_t, std::size_t>,
        std::string>>;
Graph graph;
graph[boost::graph_bundle] = "Graph bundle value";

但是,以下解决方案确实对我有用。在子图文档页面的底部,他们详细介绍了具有只读版本(返回 const ref)和可写版本(返回 ref)的 get_property 函数。

namespace boost{
enum graph_index_t {graph_index=111};
BOOST_INSTALL_PROPERTY(graph,index);
}
Graph = boost::subgraph<boost::adjacency_list<
        boost::vecS,
        boost::vecS,
        boost::directedS,
        boost::property<boost::vertex_index_t, std::size_t>,
        boost::property<boost::edge_index_t, std::size_t>,
        boost::property<boost::graph_index_t, std::size_t>>>;
Graph graph;
boost::get_property(graph, boost::graph_index) = 69;

这是您问题的解决方案,它考虑了使用捆绑属性和动态属性的 boost 子图中的访问方法,如下所示:

#include <QtCore/QCoreApplication>
#include <boost/config.hpp>
#include <iostream>
#include <algorithm>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map/property_map.hpp>
#include <string>
#include <boost/graph/subgraph.hpp>
#include <QMap>
using namespace std;
using namespace boost;
enum graph_IDproperty_t
{
   graph_IDproperty
};
namespace boost
{
  BOOST_INSTALL_PROPERTY(graph,IDproperty);
}
struct GraphProperties {
 std::string strName;
std::string id;
};
typedef boost::subgraph<boost::adjacency_list< boost::listS,
boost::vecS,
boost::bidirectionalS,
boost::property<boost::vertex_index_t, int , property<boost::vertex_color_t,   boost::default_color_type > > ,
boost::property<boost::edge_index_t,int, property<boost::edge_color_t , default_color_type> > ,
boost::property<graph_IDproperty_t,GraphProperties > > >
Graph;
Graph gMainGraph;
typedef QMap<Graph*,GraphProperties*> mapGraphToProperty;
mapGraphToProperty getMap(Graph& graph);
void graphMapRecur(mapGraphToProperty& map, Graph& graph);
int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
Graph& subG = gMainGraph.create_subgraph();
Graph& subG1 = gMainGraph.create_subgraph();
boost::ref_property_map<Graph*, GraphProperties>
        graph_propt1(boost::get_property(subG1,graph_IDproperty));
graph_propt1[&subG1].id = "SubG1";
cout<<graph_propt1[&subG1].id<<endl;
boost::ref_property_map<Graph*, GraphProperties>
        graph_propt(boost::get_property(subG,graph_IDproperty));
graph_propt[&subG].id = "SubG";
cout<<graph_propt[&subG].id<<endl;
boost::ref_property_map<Graph*, GraphProperties>
        graph_proptMain(boost::get_property(gMainGraph,graph_IDproperty));
graph_proptMain[&gMainGraph].id = "gMain";
cout<<graph_proptMain[&gMainGraph].id<<endl;
mapGraphToProperty map = getMap(gMainGraph);
boost::ref_property_map<Graph*, GraphProperties>
        graph_proptMain1(*(map.value(&gMainGraph)));
boost::ref_property_map<Graph*, GraphProperties>
        graph_proptsubG(*(map.value(&subG)));
boost::ref_property_map<Graph*, GraphProperties>
        graph_proptsubG1(*(map.value(&subG1)));
cout<<"Main G Value : "<<graph_proptMain1[&gMainGraph].id<<endl;
cout<<"Sub G Value : "<<graph_proptsubG[&subG].id<<endl;
cout<<"Sub G1 Value : "<<graph_proptsubG1[&subG1].id<<endl;

cout<<"Map Value Main: "<<(map.value(&gMainGraph))<<endl;
cout<<"Map Value SubG: "<<(map.value(&subG))<<endl;
cout<<"Map Value SubG1b: "<<(map.value(&subG1))<<endl;
return a.exec();
}
mapGraphToProperty getMap(Graph &graph)
{
mapGraphToProperty map;
graphMapRecur(map,graph);
return map;
}
void graphMapRecur(mapGraphToProperty &map, Graph &graph)
{
Graph::children_iterator itrSubgraph, itrSubgraph_end;
for (boost::tie(itrSubgraph, itrSubgraph_end) = (graph).children(); itrSubgraph != itrSubgraph_end; ++itrSubgraph)
{
    graphMapRecur(map,(*itrSubgraph));
}
GraphProperties* gp = &(get_property(graph,graph_IDproperty));
map.insert(&graph,gp);
cout<<"Recurrr"<<endl;
}