C++ 使用Graphml实现和导出Boost子图
我正在使用Boost1.53.0,我已经用BoostforSubgraph实现了一个小演示。 我需要导出graphml文件中的子图信息,而导出它将在父图中创建所有节点,但无法保存有关其子图的信息。 所以,如果有任何方法可以保存有关子图的信息,请帮助我?。 我的实现正在导出,如下所示:C++ 使用Graphml实现和导出Boost子图,c++,boost,qt-creator,C++,Boost,Qt Creator,我正在使用Boost1.53.0,我已经用BoostforSubgraph实现了一个小演示。 我需要导出graphml文件中的子图信息,而导出它将在父图中创建所有节点,但无法保存有关其子图的信息。 所以,如果有任何方法可以保存有关子图的信息,请帮助我?。 我的实现正在导出,如下所示: enter code here <?xml version="1.0" encoding="UTF-8"?> <graphml xmlns="http://graphml.graphdrawi
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是主要的父图形。下面是处理上述问题的方法。通过结合使用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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间boost;
枚举图属性
{
图的性质
};
名称空间提升
{
BOOST_安装_属性(图形、IDproperty);
}
结构图形属性{
std::字符串strName;
std::字符串id;
};
typedef boost::子图>
图表
格拉夫曲线图;
typedef QMap mapGraphToProperty;
mapGraphToProperty getMap(图形和图形);
void graphMapRecur(mapGraphToProperty&map、Graph&Graph);
int main(int argc,char*argv[])
{
qcorea应用程序(argc、argv);
Graph&subG=gMainGraph.create_subgraph();
Graph&subG1=gMainGraph.create_subgraph();
boost::ref_属性映射
图_propt1(boost::get_属性(subG1,图_IDproperty));
图1[&subG1].id=“subG1”;
库特