0

I am making a GPS system for a game, which will allow you to pick the shortest path between two point on the roads.

As for now I hae made a class which looks as follows:

#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*)?
    }
};

Now I am really stuck when it gets to finding the path between two vertices.

I looked up the documentation and example for dijkstra but I just don't get it..

Any other algorithms seem harder to setup.

How can I find the shortest path? All the parameters and functions and stuff is very confusing.. I want to switch to boost, get away from "my own home-cooked and slow" libraries..

  • 1
    Dijkstra isn't very complicated. Which part is giving you trouble? – Beta May 23 '13 at 02:25
  • I don't know what parameters to specify, what to add, and how to retrieve the shortest path :/ –  May 23 '13 at 02:29
  • 1
    Do you understand the algorithm? – Beta May 23 '13 at 02:30
  • Understand the algorithm? Isn't boost supposed to hide away the algorithm and just return me the shortest path? And from what I know dijkstra is used to find the shortest path to all nodes, as for A* I even can't find an example which I understand. Other libraries I used just had functions GetPath(from,to) and all that simple stuff... –  May 23 '13 at 02:31
  • 2
    Sorry, my mistake. I didn't realize that this whole question was about how to use *Boost's dijkstra_shortest_paths function*. – Beta May 23 '13 at 02:35
  • Have you searched other questions? Read this might help: http://stackoverflow.com/questions/8903516/c-boost-graph-library-dijkstra-example – SaintLike May 23 '13 at 02:50
  • I have nothing against other shortest path functions –  May 23 '13 at 07:32
  • If you're planning to use external code, you might as well try to use A*, as it will give you better point-to-point query results. – Origin May 27 '13 at 10:10
  • [This sample for `boost::astar`](http://www.boost.org/doc/libs/1_41_0/libs/graph/example/astar-cities.cpp) (which is probably better anyway) seems slightly easier to read... – Mooing Duck May 27 '13 at 15:48
  • @MooingDuck like I said on the chat yesterday, post it as an answer hehe, I'll accept it :) –  May 27 '13 at 16:17
  • @GamErix: It's pretty terrible as a standalone answer. Hopefully someone will post you a REAL answer. – Mooing Duck May 27 '13 at 19:24
  • @MooingDuck 1) it does answer the question and 2) else I will answer it myself XD just ad some blabla and comments in your answer with what you said to me in the chat and it will be a valuable answer for others too :) –  May 27 '13 at 20:21

1 Answers1

0

This piece of code will give you, for each node, which node you have to follow to reach the source, following the shortest path: (taken from the sample code in 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;
    }

So all you have to do, is to do

 n= dest;
 while (n!=src) {
 path.push_back(n);
 n = p[n]; // you're one step closer to the source.. 
}
alecail
  • 3,993
  • 4
  • 33
  • 52