Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在图中找到两个顶点之间的最短路径?_C++_Shortest Path_Boost Graph - Fatal编程技术网

C++ 如何在图中找到两个顶点之间的最短路径?

C++ 如何在图中找到两个顶点之间的最短路径?,c++,shortest-path,boost-graph,C++,Shortest Path,Boost Graph,我正在为一个游戏制作一个GPS系统,它可以让你在道路上的两点之间选择最短的路径 目前,我已经制作了一个类,如下所示: #include <boost/graph/graph_traits.hpp> #include <boost/graph/adjacency_list.hpp> #include <boost/graph/dijkstra_shortest_paths.hpp> using namespace boost; using namespace

我正在为一个游戏制作一个GPS系统,它可以让你在道路上的两点之间选择最短的路径

目前,我已经制作了一个类,如下所示:

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>

using namespace boost;
using namespace std;

class GPS
{
public:
    typedef         boost::property<boost::edge_weight_t, float>                        Distance;
    typedef         adjacency_list<vecS, vecS, directedS, boost::no_property, Distance> Graph;
    typedef         int                                                                 Node;
    typedef         std::pair<int, int>                                                 Edge;
    typedef         property_map<Graph, edge_weight_t>::type                            weightmap_t;                
    typedef         graph_traits < Graph >::vertex_descriptor                           vertex_descriptor;
    typedef         graph_traits < Graph >::edge_descriptor                             edge_descriptor;
private:
    vector<Edge>                            Edges;
    Graph                                   Nodes;
public:
    GPS() 
    {

    }
    ~GPS()
    {

    }
    //returns amount of edges added: 0, 1 or 2
    char AddEdge(Node from, Node to, Distance weight = 0.0f, bool BothDirections = false)
    {
        char added = 0;
        if(add_edge(from,to,weight,Nodes).second)
            ++added;
        if(BothDirections)
        {
            if(add_edge(to,from,weight,Nodes).second)
                ++added;
        }
        return added;
    }
    //returns the added node, 
    //specify your own vertex identificator if wanted 
    //(for maintaining backwards compatibility with old graphs saved in gps.dat files)
    Node AddNode(int id = -1)
    {
        if(id == -1)
            return add_vertex(Nodes);
        else
            return vertex(id,Nodes);
    }
    //get the shortest path between 'from' and 'to' by adding all nodes which are traversed into &path
    void Path(Node from, Node to, vector<Node> &path)
    {
        std::vector<vertex_descriptor> p(num_vertices(Nodes));
        std::vector<int> d(num_vertices(Nodes));
        weightmap_t weightmap = get(edge_weight, Nodes);
        vertex_descriptor s = vertex(from, Nodes);
        dijkstra_shortest_paths(Nodes, s, predecessor_map(&p[0]).distance_map(&d[0]));

        //what here? and are there faster algorithms in boost graph than dijkstra (except A*)?
    }
};
#包括
#包括
#包括
使用名称空间boost;
使用名称空间std;
类GPS
{
公众:
typedef boost::属性距离;
typedef邻接列表图;
typedef int节点;
typedef std::对边;
typedef属性映射::type weightmap\u t;
typedef graph_traits:顶点描述符顶点描述符;
typedef graph\u traits:边描述符边描述符;
私人:
矢量边;
图节点;
公众:
全球定位系统()
{
}
~GPS()
{
}
//返回添加的边数:0、1或2
char AddEdge(节点开始,节点结束,距离权重=0.0f,布尔BothDirections=false)
{
添加的字符=0;
if(添加_边(从、到、权重、节点)。秒)
++增加;
如果(两个方向)
{
if(添加_边(到、从、权重、节点)。秒)
++增加;
}
增加了退货;
}
//返回添加的节点,
//如果需要,请指定自己的顶点标识符
//(用于与保存在gps.dat文件中的旧图形保持向后兼容性)
节点AddNode(int id=-1)
{
如果(id==-1)
返回add_顶点(节点);
其他的
返回顶点(id,节点);
}
//通过添加遍历到&path中的所有节点,获取“from”和“to”之间的最短路径
无效路径(节点起点、节点终点、向量和路径)
{
向量p(顶点数(节点));
std::向量d(num_顶点(节点));
weightmap\u t weightmap=get(边权重,节点);
顶点\描述符s=顶点(从,节点);
dijkstra_最短路径(节点、s、前置_映射(&p[0])。距离_映射(&d[0]);
//这是什么?boost图中有没有比dijkstra更快的算法(除了A*)?
}
};
现在我真的很难找到两个顶点之间的路径

我查了一下和,寻找迪杰斯特拉,但我就是不明白

任何其他算法似乎都很难设置


我怎样才能找到最短的路径?所有的参数、函数和内容都非常混乱。。我想切换到boost,远离“我自己的家常菜和慢”库。

这段代码将告诉您,对于每个节点,您必须遵循哪个节点才能到达源代码,遵循最短路径:(取自boost中的示例代码)


Dijkstra不是很复杂。哪一部分给您带来了麻烦?我不知道要指定哪些参数,添加哪些参数,以及如何检索最短路径:/n您了解算法吗?了解算法吗?boost不是应该隐藏算法并返回最短路径吗?据我所知,dijkstra被用来寻找到所有节点的最短路径,至于A,我甚至找不到一个我能理解的例子。我使用的其他库只有函数GetPath(from,to)和所有那些简单的东西……对不起,我的错误。我没有意识到整个问题是关于如何使用Boost的dijkstra_最短路径函数。
  std::cout << "distances and parents:" << std::endl;
    graph_traits < graph_t >::vertex_iterator vi, vend;
    for (boost::tie(vi, vend) = vertices(g); vi != vend; ++vi) {
        std::cout << "distance(" << *vi << ") = " << d[*vi] << ", ";
        std::cout << "parent(" << *vi << ") = " << p[*vi] << std::
        endl;
    }
 n= dest;
 while (n!=src) {
 path.push_back(n);
 n = p[n]; // you're one step closer to the source.. 
}