我正在使用boost 1.53.0,我已经使用boost为子图实现了一个小的演示。 我需要在graphml文件中导出子图信息,而导出它将创建父图中的所有节点,但不能保存有关它的子图的信息。 如果有任何方法可以保存有关子图的信息,请帮助我。 我的实现导出如下:
enter code here
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
<graph id="G" edgedefault="undirected" parse.nodeids="free"
parse.edgeids="canonical" parse.order="nodesfirst">
<node id="n0">
</node>
<node id="n1">
</node>
<node id="n2">
</node>
<node id="n3">
</node>
<node id="n4">
</node>
<node id="n5">
</node>
<edge id="e0" source="n0" target="n1">
</edge>
<edge id="e1" source="n1" target="n2">
</edge>
<edge id="e2" source="n1" target="n3">
</edge>
<edge id="e3" source="n4" target="n1">
</edge>
<edge id="e4" source="n4" target="n5">
</edge>
<edge id="e5" source="n5" target="n3">
</edge>
<edge id="e6" source="n2" target="n5">
</edge>
</graph>
</graphml>
实际上,节点n0,n1,n2是子图G1和n4的成员,n5是子图G2的成员。 G0是主要父图。
答案 0 :(得分:0)
以下是处理上述问题的方法。通过结合使用来自boost的动态属性以及使用来自boost的捆绑属性来解决此问题。 此代码适用于boost 1_53_0。 引用属性映射用于存储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;
}