【问题标题】:Performing a Depth first search and breadth first search in Boost在 Boost 中执行深度优先搜索和广度优先搜索
【发布时间】:2012-09-03 08:32:31
【问题描述】:

我对 Boost 库和 c++ 语言都很陌生。

我使用 Boost 创建了一个图,并添加了顶点和边,并在 graphviz 中输出了该图。

现在我想执行从一个顶点到图中所有其他顶点的广度优先和深度优先搜索。

结果应该是图中从起始顶点到其他顶点的最短路径。

如何在 Boost 中实现这一点?任何帮助将不胜感激。

非常感谢。

我还添加了我的源代码供您参考。

#include "stdafx.h"
#include <iostream>

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/breadth_first_search.hpp>
#include <boost/pending/indirect_cmp.hpp>
#include <boost/range/irange.hpp>
#include <boost/graph/graphviz.hpp>

int main()
{
            using namespace std;
            using namespace boost;


        // Property types
            typedef property<vertex_name_t, std::string,
            property<vertex_index2_t, int> > VertexProperties;

        // Graph type
            typedef adjacency_list<vecS, vecS, undirectedS,
            VertexProperties> Graph;

        // Graph instance
            Graph g;

        // Property accessors
            property_map<Graph, vertex_name_t>::type
            profinet_name = get(vertex_name, g);
            property_map<Graph, vertex_index2_t>::type
            profinet_index2 = get(vertex_index2, g);

        // Create the vertices
            typedef graph_traits<Graph>::vertex_descriptor Vertex;
            Vertex u1;
            u1 = add_vertex(g);
            profinet_name[u1] = "Profinet 1";
            profinet_index2[u1] = 1;

            Vertex u2;
            u2 = add_vertex(g);
            profinet_name[u2] = "Profinet 2";
            profinet_index2[u2] = 2;

            Vertex u3;
            u3 = add_vertex(g);
            profinet_name[u3] = "Profinet 3";
            profinet_index2[u3] = 3;

            Vertex u4;
            u4 = add_vertex(g);
            profinet_name[u4] = "Profinet 4";
            profinet_index2[u4] = 4;

            Vertex u5;
            u5 = add_vertex(g);
            profinet_name[u5] = "Profinet 5";
            profinet_index2[u5] = 5;

            Vertex u6;
            u6 = add_vertex(g);
            profinet_name[u6] = "Profinet 6";
            profinet_index2[u6] = 6;


        // Create the edges
            typedef graph_traits<Graph>::edge_descriptor Edge;
            Edge e1;
            e1 = (add_edge(u1, u2, g)).first;

            Edge e2;
            e2 = add_edge(u1, u4, g).first;

            Edge e3;
            e3 = (add_edge(u1, u6, g)).first;

            Edge e4;
            e4 = (add_edge(u2, u3, g)).first;

            Edge e5;
            e5 = (add_edge(u2, u4, g)).first;

            Edge e6;
            e6 = (add_edge(u2, u5, g)).first;

            Edge e7;
            e7 = (add_edge(u3, u6, g)).first;

            Edge e8;
            e8 = (add_edge(u6, u5, g)).first;

            Edge e9;
            e9 = (add_edge(u5, u4, g)).first;


            write_graphviz(cout, g);

            return 0;


}

【问题讨论】:

  • documentation 描述得不够好?
  • @jogojapan 这不是很清楚。我尝试阅读文档,但示例代码没有注释,很难理解发生了什么。

标签: c++ boost depth-first-search breadth-first-search boost-graph


【解决方案1】:

如果你想执行从 a 到 b 的最短路径,你应该使用 Dijkstra 算法。

理论上,如果所有边的成本为 1,BFS 会更好,但在 boost::graph 中它需要一些努力(它只是一个空壳,您需要实现自己的访问者以存储与起源)。

但是,为了使用 dijkstra,您需要在边上添加带有权重的边属性并在算法中使用它。

在以下示例http://www.boost.org/doc/libs/1_51_0/libs/graph/example/dijkstra-example.cpp 中,您可以通过查看前辈地图展开从 s 到 t 的最短路径:p[t] 是最短路径中 t 之前的节点。

我希望这就够了:)

【讨论】:

  • 感谢您的回复。但我在这里真正想要的是,找到从给定顶点可到达的顶点并希望显示路径。我也想执行 Dijkstra 算法,但最初,我想从 DFS 和 BFS 开始。
  • 那么,缺少什么?使用 Dijkstra,所有可到达的节点 u 都是 p[u] != u 的节点。在 boost 中使用 DFS 和 BFS 需要一些额外的工作,因为您需要实现自己的访问者:查看示例 boost.org/doc/libs/1_36_0/libs/graph/example/bfs-example.cpp
  • 感谢您的回复,但我如何实现自己的访问者?有没有例子告诉我怎么做?
  • 在我在上一个回复中给出的网址中……
猜你喜欢
  • 2011-01-31
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
相关资源
最近更新 更多