首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Boost中执行深度优先搜索和广度优先搜索

在Boost中执行深度优先搜索和广度优先搜索
EN

Stack Overflow用户
提问于 2012-09-03 16:32:31
回答 1查看 3.3K关注 0票数 1

对于Boost库和c++语言,我都是新手。

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

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

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

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

提前感谢你了。

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

代码语言:javascript
运行
复制
#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;


}
EN

回答 1

Stack Overflow用户

发布于 2012-09-03 16:59:26

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

理论上,如果所有边的成本都为1,那么BFS会更好,但在boost::graph中,它需要一些努力(它只是一个空的shell,您需要实现自己的访问者,以便存储到原点的距离)。

但是,为了使用dijkstra,您需要添加边缘属性,并在算法中使用它。

在下面的示例http://www.boost.org/doc/libs/1_51_0/libs/graph/example/dijkstra-example.cpp中,您可以通过查看前置映射来展开从s到t的最短路径: pt是最短路径中t之前的节点。

我希望这就足够了:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12244428

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档