【问题标题】:Traversal of boost graph shows "hidden" node提升图的遍历显示“隐藏”节点
【发布时间】:2012-07-13 10:37:27
【问题描述】:

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

#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 刚刚建议的那样。因此,仅添加边并不是我认为的最终解决方案,因为删除其中一个顶点不会按预期工作。

【问题讨论】:

  • 我没有使用 boost 图的经验,但我注意到在尝试这个测试用例时(恭喜你创建了一个 btw!),不需要添加超过第一个边来获得一个 0 节点。但是当在 0,1 处添加一条边时,只有 0,1。因此,我尝试在 8,9 处仅添加一个,而我从 0 到 9。也许这会有所帮助。
  • 这确实很奇怪,但感谢您的提示。我会对此进行更多调查。

标签: c++ boost graph


【解决方案1】:

来自文档:

如果图的VertexList是vecS,那么图有一个内建的 通过 vertex_index_t 的属性映射访问的顶点索引 财产。索引在 [0, num_vertices(g)) 范围内,并且是 连续的。当一个顶点被删除时,索引会被调整,以便它们保留 这些属性。

我不认为文档明确指出vertex_descriptor 只是索引。文档中的一些示例表明情况确实如此。其他示例将 vertex_descriptor 视为黑盒。

【讨论】:

  • 我认为你是对的。底层结构非常灵活,既可以用作包装器,也可以用作容器。我只使用 bfs 而不是 dfs 解决了我的问题,因为它的行为不同于 dfs,它遍历图中的所有节点,即使它们没有连接。
  • 好吧,我不得不重新考虑。你是对的。我连接的数字不是“实际”节点,而是这些节点的索引。
猜你喜欢
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2010-11-23
相关资源
最近更新 更多