2013-06-26 41 views
0

我正在使用boost 1.53.0,我已經實現了一個使用子圖的boost小演示。 我需要在graphml文件中導出子圖信息,導出時會創建父圖中的所有節點,但無法保存其子圖的信息。 所以請幫助我,如果有任何方法來保存關於子圖的信息? 我實現出口如下: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> 

Actully節點N0,N1,N2的子圖G1和N4的成員,N5是subgrph G2的成員。 G0是主父圖。

回答

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; 
}