带字符串顶点的Boost图

时间:2013-09-16 07:54:39

标签: c++ boost graph boost-graph

我知道如何在Boost Graph中创建一个带整数或字符顶点的图形(请参阅下面的注释代码)。问题是如何重写此代码以使用字符串顶点?

#include <string>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

int main (int argc, char **argv)
{
        typedef adjacency_list <vecS, vecS, undirectedS> vector_graph_t;

        //works
        //typedef std::pair <int, int> E;
        //E edges[] = { E (2, 5), E (5, 3), E (3, 1), E (5, 1)};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4);

        //works
        //typedef std::pair <char, char> E;
        //E edges[] = { E ('a', 'b'), E ('a', 'c'), E ('x', 'a'), E ('b', 'x')};
        //vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4);

        //does not work
        typedef std::pair <std::string, std::string> E;
        E edges[] = { E ("aaa", "bbb"), E ("bbb", "ccc"), E ("aaa", "xxx"), E ("bbb", "ccc")};
        vector_graph_t g (&edges[0],&edges[0] + sizeof(edges) / sizeof(E), 4); 

        return 0;
}

上面的程序编译错误:

/usr/local/include/boost/graph/detail/adjacency_list.hpp:2076: error: no matching function for call to ‘add_edge(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>&)’
/usr/local/include/boost/graph/detail/adjacency_list.hpp:1030: note: candidates are: std::pair<typename Config::edge_descriptor, bool> boost::add_edge(typename Config::vertex_descriptor, typename Config::vertex_descriptor, boost::undirected_graph_helper<C>&) [with Config = boost::detail::adj_list_gen<boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>, boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, boost::no_property, boost::no_property, boost::listS>::config]

1 个答案:

答案 0 :(得分:4)

我认为它适用于int和char,因为这两种类型用作顶点索引。但是,在注释代码中,将从顶点8开始的边添加到包含4个顶点的图...

显式方法可能会给您正确的结果。如果希望顶点属性由图形本身存储,则应声明属性并将其链接到图形类型。您还应该使用它们的索引来访问顶点,这些顶点不能直接从它们的属性中完成(不同的顶点可能具有相同的属性)。

#include <string>
#include <map>
#include <boost/graph/adjacency_list.hpp>

using namespace boost;

int main (int argc, char **argv)
{
  typedef property<vertex_name_t, std::string> VertexProperty;

  typedef adjacency_list <vecS, vecS, undirectedS, VertexProperty> vector_graph_t;

  typedef std::pair <std::string, std::string> E;
  E edges[] = { E ("aaa", "bbb"), E ("bbb", "ccc"), E ("aaa", "xxx"), E ("bbb", "ccc")};

  const char* vertices[] = {"aaa", "bbb", "ccc", "xxx"};
  std::map<std::string, vector_graph_t::vertex_descriptor> indexes;

  const int nb_vertices = sizeof(vertices)/sizeof(vertices[0]);

  // creates a graph with 4 vertices
  vector_graph_t g (nb_vertices); 

  // fills the property 'vertex_name_t' of the vertices
  for(int i = 0; i < nb_vertices; i++)
  {
    boost::put(vertex_name_t(), g, i, vertices[i]); // set the property of a vertex
    indexes[vertices[i]] = boost::vertex(i, g);     // retrives the associated vertex descriptor
  }

  // adds the edges
  // indexes[edges[0].first] maps "aaa" to the associated vertex index
  for(int i = 0; i < sizeof(edges)/sizeof(edges[0]); i++)
  {
    boost::add_edge(indexes[edges[i].first], indexes[edges[i].second], g);
  }

  return 0;
}