簡體   English   中英

如何找到圖中兩個頂點之間的最短路徑?

[英]How to find the shortest path between two vertives in a 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 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*)?
    }
};

現在,當我要找到兩個頂點之間的路徑時,我真的很困。

我查閱了dijkstra的文檔示例 ,但我不明白。

其他任何算法似乎都難以設置。

如何找到最短的路徑? 所有的參數,函數和內容都很令人困惑。我想切換以增強功能,擺脫“我自己的自制慢速”庫。

這段代碼將為您提供每個節點按照最短路徑到達源所必須遵循的節點:(摘自Boost中的示例代碼)

  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.. 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM