本文介绍了使用boost将非平行边缘图保存到graphml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将图形保存到graphml文件中。有用!。但是当我替换 typedef adjacency_list< vecS,vecS,directedS, typedef adjacency_list< setS,setS,directedS,,以便没有重复的顶点或边被插入图中,它抱怨。

Following code save a graph into a graphml file. It works!. But when I replace typedef adjacency_list< vecS, vecS, directedS, with typedef adjacency_list< setS, setS, directedS, so that no duplicate vertex or edge is inserted into the graph, it complains.

#include <boost/graph/graphml.hpp>

using namespace std;
typedef struct {
  string name;
  string label;
} vertex_type_t;

int main(int,char*[])
{

  using namespace boost;

  typedef adjacency_list< vecS, vecS, directedS, 
      vertex_type_t > graph_t;

  graph_t g;
  graph_t::vertex_descriptor v1 = add_vertex(g);
  graph_t::vertex_descriptor v2 = add_vertex(g);

  dynamic_properties dp;
  dp.property("name", get(&vertex_type_t::name, g));

  write_graphml(std::cout, g, dp, true);

  return 0;
}

我无法充分利用错误。我相信以下是主要错误。

I can not make much out of the error. I believe following is the main error.

/usr/include/boost/graph/graphml.hpp: In function ‘void boost::write_graphml(std::ostream&, const Graph&, VertexIndexMap, const boost::dynamic_properties&, bool) [with Graph = boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, VertexIndexMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, std::ostream = std::basic_ostream<char>]’:
/usr/include/boost/graph/graphml.hpp:345:5:   instantiated from ‘void boost::write_graphml(std::ostream&, const Graph&, const boost::dynamic_properties&, bool) [with Graph = boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, std::ostream = std::basic_ostream<char>]’
write_graphviz.cpp:24:39:   instantiated from here
/usr/include/boost/graph/graphml.hpp:301:9: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & out), ((const char*)"    <node id=\"n")) << boost::get [with PropertyMap = boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t>, Reference = const boost::detail::error_property_not_found&, K = void*]((*(const boost::put_get_helper<const boost::detail::error_property_not_found&, boost::adj_list_vertex_property_map<boost::adjacency_list<boost::setS, boost::setS, boost::directedS, vertex_type_t>, boost::detail::error_property_not_found, const boost::detail::error_property_not_found&, boost::vertex_index_t> >*)(& vertex_index)), (* & v.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = void*, std::_Rb_tree_const_iterator<_Tp>::reference = void* const&]()))’
/usr/include/boost/graph/graphml.hpp:301:9: note: candidates are:
/usr/include/c++/4.6/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/include/c++/4.6/ostream:110:7: note:   no known conversion for argument 1 from ‘const boost::detail::error_property_not_found’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&) {aka std::basic_ostream<char>& (*)(std::basic_ostream<char>&)}’
/usr/include/c++/4.6/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]



我真的想使用setS作为边框容器。

I really want to use setS as edge containers. I am not sure how to modify this program such that it starts working since error message is not making much sense to me.

推荐答案

我不知道如何修改这个程序,因为它开始工作,因为错误信息对我没有什么意义。问题是在您的图中需要一个VertexIndexMap,并且只有 adjacency_list 具有 VertexList = vecS 如果您使用 listS setS ,您必须自己创建一个。

The problem is that write_graphml requires a VertexIndexMap in your graph and only adjacency_lists with VertexList=vecS have one created by default. You have to create one yourself if you use listS or setS.

如果您只需在 setS > OutEdgeList 您可以简单地使用:

If you only need to use setS in your OutEdgeList you can simply use:

typedef adjacency_list< setS, vecS, directedS, 
  vertex_type_t > graph_t;

如果必须使用 setS 您的计划应该(基于):

If you must use setS for both then your program should be (based on this answer):

#include <boost/graph/graphml.hpp>
#include <boost/graph/iteration_macros.hpp>

#include <map>

using namespace std;
typedef struct
{
    string name;
    string label;
} vertex_type_t;

int main ( int, char*[] )
{

    using namespace boost;

    typedef adjacency_list < setS, vecS, directedS,
            vertex_type_t > graph_t;

    typedef graph_t::vertex_descriptor NodeID; //define your Vertex Index Map
    typedef std::map<NodeID, size_t> IndexMap;
    IndexMap mapIndex;
    boost::associative_property_map<IndexMap> propmapIndex ( mapIndex );

    graph_t g;
    graph_t::vertex_descriptor v1 = add_vertex ( g );
    graph_t::vertex_descriptor v2 = add_vertex ( g );

    int i = 0;                                  //fill your Vertex Index Map
    BGL_FORALL_VERTICES ( v, g, graph_t )
    {
        put ( propmapIndex, v, i++ );
    }

    g[v1].name="FirstVertex";
    g[v2].name="SecondVertex";

    dynamic_properties dp;
    dp.property ( "name", get ( &vertex_type_t::name, g ) );

    write_graphml ( std::cout, g, propmapIndex, dp, true );

    return 0;
}

这篇关于使用boost将非平行边缘图保存到graphml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:16