c++ - boost图遍历显示 "hidden"节点

标签 c++ boost graph

我开始尝试 boost 图类。为此,我创建了一个简单的示例,如下所示。当通过深度优先搜索算法遍历图形时,我没有添加一个节点。这是代码:

#include <boost\graph\adjacency_list.hpp>
#include <boost\graph\depth_first_search.hpp>
#include <iostream>

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> GraphType;
typedef boost::graph_traits<GraphType>::vertex_descriptor VertexType;

class VertexVisitor : public boost::default_dfs_visitor
{
public:
  void discover_vertex(VertexType v, GraphType g)
  {
    std::cout << v << std::endl;
  }
};

int main() 
{ 
  GraphType g;
  boost::add_edge(1,2,g);
  boost::add_edge(1,3,g);
  boost::add_edge(2,3,g);
  boost::add_edge(1,4,g);
  boost::add_edge(4,5,g);

  VertexVisitor vis;
  boost::depth_first_search(g, boost::visitor(vis));

  return 0;
}

这个的输出是

0
1
2
3
4
5

但是 0 是从哪里来的,我从来没有添加过它?这是某种虚拟节点吗?但如果是这样,为什么在遍历时会访问它,我如何才能实现所需的行为?

编辑 1: 在尝试之后,根据 PlasmaHH 的建议,并通过我发现的 boost 代码进行调试,boost::add_edge 调用了图形顶点结构的大小调整。因此搜索算法添加和访问了更多元素,尽管它们彼此没有连接。顺便说一句:我正在使用 boost 1.47。

编辑 2: 它表明,depth_first_search 的行为(除了它的原生差异)不同于 breadth_first_search 算法,因为DFS遍历图中的所有节点,即使它们没有连接。我看不到这样做的好处,因为我只想找到一条从一个节点到另一个节点的路径,该路径与该节点相连,但是没关系。如前所述,我的问题的解决方案是使用 BFS 算法,该算法不会遍历所有子图。对于那些有兴趣的人,我添加一个小例子:

#include <boost\graph\adjacency_list.hpp>
#include <boost\graph\depth_first_search.hpp>
#include <boost\graph\breadth_first_search.hpp>
#include <iostream>

typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS>  GraphType;
typedef boost::graph_traits<GraphType>::vertex_descriptor VertexType;

class DFSVertexVisitor : public boost::default_dfs_visitor
{
public:
  void discover_vertex(GraphType::vertex_descriptor v, GraphType g)
  {
    std::cout << v << std::endl;
  }
};

class BFSVertexVisitor : public boost::default_bfs_visitor
{
public:
  void discover_vertex(GraphType::vertex_descriptor v, GraphType g)
  {
    std::cout << v << std::endl;
  }
};


int main(int argc, char *argv[])
{
  GraphType g;
  boost::add_edge(1, 2, g);
  boost::add_edge(2, 3, g);
  boost::add_edge(1, 3, g);
  boost::add_edge(4, 5, g);

  std::cout << "Performing BFS" << std::endl;
  BFSVertexVisitor bfsVisitor;
  boost::breadth_first_search(g, boost::vertex(1, g), boost::visitor(bfsVisitor));

  std::cout << "Performing DFS" << std::endl;
  DFSVertexVisitor dfsVisitor;
  boost::depth_first_search(g, boost::visitor(dfsVisitor).root_vertex(1));

  return 0;
}

注意,节点 4 和 5 没有连接到节点 1、2 和 3!

输出:

Performing BFS
1
2
3
Performing DFS
1
2
3
0
4
5

编辑 3: 我不得不重新考虑。我用 add_edge 连接的数字不是节点本身,而只是它们的索引,正如 n.m 刚刚建议的那样。因此,我认为仅添加边并不是最终解决方案,因为删除其中一个顶点无法按预期工作。

最佳答案

来自文档:

If the VertexList of the graph is vecS, then the graph has a builtin vertex indices accessed via the property map for the vertex_index_t property. The indices fall in the range [0, num_vertices(g)) and are contiguous. When a vertex is removed the indices are adjusted so that they retain these properties.

我认为文档没有明确说明 vertex_descriptor 只是索引。文档中的一些示例表明确实如此。其他示例将 vertex_descriptor 视为黑盒。

关于c++ - boost图遍历显示 "hidden"节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11468837/

相关文章:

c++ - emscripten C++11 与 boost 支持问题

c++ - 使用 `A<int>::template B<int> x;` 定义变量是否符合 C++ 标准?

c++ - .eof() 在使用数组时替换

c++ - C++ 中的 RTTI 和虚函数。 gcc的实现方式是必须的吗?

java - 如何组织图形节点以使用 java 2d 绘制

iphone - Iphone 中的股票图表?

r - R中的重叠圆形多个条形图

c++ - 可变参数扩展可以用作逗号运算符调用链吗?

c++ - 如何使用结构序列化 map

c++ - 我不明白为什么 boost::asio::read_until 读取定界符后面的字符