简体   繁体   English

boost :: subgraph如何工作? 我们可以使用过滤图吗?

[英]How does boost::subgraph work? Can we use filtered graph?

I use boost::subgraph, and wanted to know how it works. 我使用boost :: subgraph,想知道它是如何工作的。

Graph G;
add_vertex(A, G);
add_vertex(B, G);
add_vertex(C, G);
add_edge(A, B, G);
add_edge(A, C, G);
add_edge(C, B, G);
Graph &G0 = G.createSubgraph();
add_vertex(A, G0);
add_vertex(B, G0);

What is the memory cost of G0? G0的内存成本是多少? I guess G0 has to store all vertices added for G0. 我猜G0必须存储为G0添加的所有顶点。 Does G0 also need to store edges on G0. G0是否还需要在G0上存储边缘。

When traversing G0, do we actually traverse on G? 遍历G0时,我们是否实际遍历G? For each edge, we need to check if its target node is on G0. 对于每个边缘,我们需要检查其目标节点是否在G0上。 If not, we skip the node. 如果没有,我们跳过该节点。 So we have this additional check cost. 所以我们有额外的支票费用。 Is it how it works? 它是如何工作的?

Boost also has the filtered graph http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/filtered_graph.html How o we decide using a subgraph or a filtered graph? Boost还有过滤图http://www.boost.org/doc/libs/1_55_0/libs/graph/doc/filtered_graph.html我们如何决定使用子图还是过滤图?

Thank you, 谢谢,

Your problem with the reference is a C++ problem and has nothing to do with Boost Graph. 引用的问题是C ++问题,与Boost Graph无关。

You can not create an unbound reference (and you cannot rebind it either. Assignment assigns the object referred to. 您无法创建未绑定的引用(也无法重新绑定它。分配会指定引用的对象

So in principle what you had, but fixed: 所以原则上你有什么,但修正了:

Graph G;
add_vertex(A, G);
add_vertex(B, G);
add_vertex(C, G);
add_edge(A, B, G);
add_edge(A, C, G);
add_edge(C, B, G);

Graph& G0 = G.createSubgraph();
add_vertex(A, G0);
add_vertex(B, G0);

I suggest to read a bit more about C++ because not knowing the language essentials is going to cause you a lot of trouble when you use an advanced generic library like Boost Graph. 我建议您阅读更多关于C ++的内容,因为当您使用Boost Graph等高级通用库时,不了解语言要点会给您带来很多麻烦。

Suggested reading: The Definitive C++ Book Guide and List 推荐阅读: 最终C ++书籍指南和列表

UPDATE UPDATE

The algorithmic complexities aren't affected, but the constants can be expected to increase linearly with the depth of the tree of subgraphs. 算法的复杂性不受影响,但可以预期常数会随着子图树的深度线性增加。

The mechanism in which the subgraphs work is not very complicated (it's just a lot of proxying). 子图工作的机制并不是很复杂(它只是很多代理)。 The key is in the way mappings are stored inside non-root subgraphs: 关键在于映射存储在非根子图中:

Graph m_graph;
subgraph<Graph>* m_parent;
edge_index_type m_edge_counter; // for generating unique edge indices
ChildrenList m_children;
GlobalVertexList m_global_vertex; // local -> global
LocalVertexMap m_local_vertex;  // global -> local
GlobalEdgeList m_global_edge;              // local -> global
LocalEdgeMap m_local_edge; // global -> local

As you can see there is considerable overhead mapping subgraph descriptors to parent ("locally global") descriptors. 正如您所看到的,将子图描述符映射到父(“本地全局”)描述符的开销很大。

Exactly how bad things are depends on use-cases and you will want to profile it: 究竟有多糟糕取决于用例,您需要对其进行分析:

  • as subgraphs are nested more deeply, performance will suffer more 由于子图嵌套得更深,性能会受到更多影响
  • as subgraphs get larger relative to the parent, memory consumption will rise more proportionately 随着子图相对于父图变大,内存消耗将按比例增加
  • as properties are smaller, the difference in memory usage will be more visible 由于属性较小,内存使用的差异将更明显
  • if mutations happen on lower-nested subgraphs, the ripple effect is going to have more slowdown effect. 如果在较低嵌套的子图上发生突变,则涟漪效应将具有更多的减速效果。 Interestingly 有趣的是

    • using vecS on the root graph's VertexContainer should usually lead to worse insertion/deletion times; 在根图的VertexContainer上使用vecS通常会导致更糟的插入/删除时间;
    • However for iteration the advantage is with vecS for memory locality 然而,对于迭代,优点是vecS用于存储器局部性

    I think both effects will be lessened: 我认为两种效果都会减少:

    • the lookup/translation maps hurt locality anyways and cannot be customized 查找/翻译地图无论如何都会伤害地方,无法定制
    • child graphs use vector storage for some collections regardless; 子图使用矢量存储一些集合,无论如何; so the invalidation/reallocation costs associated with the vectors are there 所以与矢量相关的失效/重新分配成本就在那里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM