0

我正在使用 boost 1.53.0,我已经使用 boost for subgraph 实现了一个小演示。我需要在 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 是主父图。

4

1 回答 1

0

以下是处理上述问题的方法。这个问题通过结合使用 boost 中的动态属性和使用 boost 中的捆绑属性来解决。此代码适用于 boost 1_53_0。参考属性图用于存储升压子图的属性。

#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;
}
于 2014-05-21T13:07:14.430 回答