Performance 在这段代码中,boost::property_映射运算符[]的时间复杂度是否为O(1)?

Performance 在这段代码中,boost::property_映射运算符[]的时间复杂度是否为O(1)?,performance,boost,boost-graph,Performance,Boost,Boost Graph,我是boost的新手,也是boost图形库的新手。谁能解释一下属性映射背后的实现是什么,以及下面代码中运算符[]的时间复杂性是什么 谢谢 #include <string> #include <boost/graph/adjacency_list.hpp> int main() { using namespace boost; typedef adjacency_list<listS, listS, directedS, property<ve

我是boost的新手,也是boost图形库的新手。谁能解释一下属性映射背后的实现是什么,以及下面代码中运算符[]的时间复杂性是什么

谢谢

#include <string>
#include <boost/graph/adjacency_list.hpp>
int main()
{
  using namespace boost;
  typedef adjacency_list<listS, listS, directedS,
    property<vertex_name_t, std::string> > graph_t;
  graph_t g;
  graph_traits<graph_t>::vertex_descriptor u = add_vertex(g);
  property_map<graph_t, vertex_name_t>::type name_map = get(vertex_name, g);
  name_map[i] = "Joe";
  return EXIT_SUCCESS;
}
#包括
#包括
int main()
{
使用名称空间boost;
typedef邻接列表图;
图g;
图\特征::顶点\描述符u=添加\顶点(g);
属性映射::类型名称映射=get(顶点名称,g);
name_map[i]=“Joe”;
返回退出成功;
}

您可以通过为属性映射指定一个属性来创建属性映射。因此,时间和空间的复杂性可能与底层相同。您可能想更深入地了解a的STL文档。

我自己也很想知道这一点,但奇怪的是boost::graph文档没有明确说明这一点,因为此类问题与性能关键型算法/应用程序高度相关

总之,我相信答案是肯定的,它是O(1)时间复杂性。我的理由如下

因为这些只是概念,所以它不能保证复杂性。因此,我们必须查看属性映射的邻接列表实现,以了解其复杂性。我相信有关守则载于:;搜索“顶点属性贴图”

模板
结构调整列表顶点属性映射
:public boost::put\u get\u helper<
参考,
调整列表顶点属性映射
>
{
typedef typename图形::存储的顶点存储的vertex;
类型定义值类型值\u类型;
typedef参考;
typedef typename图形::顶点描述符键类型;
typedef boost::左值属性映射标记类别;
内联adj_列表_顶点_属性_映射(const Graph*=0,Tag Tag=Tag()):m_Tag(Tag){
内联引用运算符[](键类型v)常量{
StoredVertex*sv=(StoredVertex*)v;
返回get_property_值(sv->m_property,m_标签);
}
内联引用运算符()(键类型v)常量{
返回此->运算符[](v);
}
标签m_标签;
};
我相信这是一个属性映射,用于邻接列表的内部属性,邻接列表是用列表顶点列表类型实例化的,如您的示例中所示。您可以看到,操作符[]使用一个Graph::vertex_描述符,它看起来像是一个句柄,可能是一个迭代器,直接访问属性结构而无需查找,
sv->m_property
,因此时间恒定。当您有多个与每个顶点关联的属性时,调用
get\u property\u value();在你的情况下,你只有一个。标记查找通常也是固定时间

实例化具有VecS VertexList类型的属性的邻接_列表似乎也会在属性映射查找中提供O(1)时间复杂性。这里使用的类型似乎是
vec_adj_list_vertex_property_map
,并且操作符[]在每个顶点的属性向量中使用Graph::vector_描述符,因此O(1)

回想起来,我想我会期望,因为库工作非常努力以确保性能,它将确保这也是性能

template <class Graph, class ValueType, class Reference, class Tag>
struct adj_list_vertex_property_map
  : public boost::put_get_helper<
      Reference,
      adj_list_vertex_property_map<Graph, ValueType, Reference, Tag>
    >
{
  typedef typename Graph::stored_vertex StoredVertex;
  typedef ValueType value_type;
  typedef Reference reference;
  typedef typename Graph::vertex_descriptor key_type;
  typedef boost::lvalue_property_map_tag category;
  inline adj_list_vertex_property_map(const Graph* = 0, Tag tag = Tag()): m_tag(tag) { }
  inline Reference operator[](key_type v) const {
    StoredVertex* sv = (StoredVertex*)v;
    return get_property_value(sv->m_property, m_tag);
  }
  inline Reference operator()(key_type v) const {
    return this->operator[](v);
  }
  Tag m_tag;
};