Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. /*
  2.  *
  3.  */
  4.  
  5. #include <boost/config.hpp>
  6.  
  7. #include <boost/graph/graph_traits.hpp>
  8. #include <boost/graph/adjacency_list.hpp>
  9. #include <boost/graph/dijkstra_shortest_paths.hpp>
  10.  
  11. #include <algorithm> //copy
  12. #include <vector>
  13.  
  14. #include "dijkstra.h" //path_type_t
  15.  
  16. using namespace std;
  17. using namespace boost;
  18.  
  19.  
  20. struct Edge
  21. {
  22.   int id;
  23.   float8 cost;
  24. };
  25.  
  26. template <class G>
  27. static void
  28. graph_add_edge(G &graph, int id, int source, int target, float8 cost)
  29. {
  30.   typedef typename graph_traits<G>::edge_descriptor E;
  31.   E e;
  32.   bool inserted;
  33.  
  34.   if (cost < 0) // edges are not inserted in the graph if cost is negative
  35.     return;
  36.  
  37.   tie(e, inserted) = add_edge(source, target, graph);
  38.  
  39.   graph[e].cost = cost;
  40.   graph[e].id = id;
  41. }
  42.  
  43. template <class G>
  44. static int
  45. get_edge_id(G &graph, int source, int target)
  46. {
  47.   typedef typename graph_traits<G>::edge_descriptor E;
  48.   E e;
  49.   bool found;
  50.  
  51.   tie(e, found) = boost::edge(source, target, graph);
  52.  
  53.   return found ? graph[e].id : -1;
  54. }
  55.  
  56. template <class G>
  57. static float8
  58. get_edge_cost(G &graph, int source, int target)
  59. {
  60.   typedef typename graph_traits<G>::edge_descriptor E;
  61.   E e;
  62.   bool found;
  63.  
  64.   tie(e, found) = boost::edge(source, target, graph);
  65.  
  66.   return found ? graph[e].cost : -1;
  67. }
  68.  
  69. int
  70. boost_dijkstra_nodes(edge_t *edges, unsigned int count, int source_vertex_id,
  71.                     double rdistance, bool directed, bool has_reverse_cost,
  72.                     path_element_t **path, int *path_count, char **err_msg)
  73. {
  74.   typedef adjacency_list < listS, vecS, directedS, Vertex, Edge > graph_t;
  75.   typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;
  76.   typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;
  77.  
  78.   graph_t graph;
  79.  
  80.   for (std::size_t j = 0; j < count; ++j)
  81.   {
  82.      Edge& edge = edges[j];
  83.      
  84.      graph_add_edge(graph, edge.id, edge.source, edge.target, edge.cost);
  85.  
  86.      if (!directed || (directed && has_reverse_cost))
  87.         {
  88.           if (has_reverse_cost)
  89.             {
  90.               graph_add_edge(graph, edge.id, edge.target, edge.source, edge.rcost);
  91.             }
  92.           else
  93.             {
  94.               graph_add_edge(graph, edge.id, edge.target, edge.source, edge.cost);
  95.             }
  96.         }
  97.     }
  98.  
  99.  
  100.   std::vector<vertex_descriptor> predecessors(num_vertices(graph));
  101.   std::vector<float8> distances(num_vertices(graph));
  102.  
  103.   dijkstra_shortest_paths(graph, source_vertex_id,
  104.                           predecessor_map(&predecessors[0])
  105.                           .weight_map(get(&Edge::cost, graph))
  106.                           .distance_map(&distances[0]));
  107.  
  108.  
  109.   graph_traits < graph_t >::vertex_iterator vi, vend;
  110.   vector<path_element_t> path_vector;
  111.  
  112.   for(tie(vi, vend) = vertices(graph); vi != vend; vi++) {
  113.  
  114.     if( (double)distances[*vi] <= rdistance ) {
  115.  
  116.       path_element_t pe;
  117.  
  118.       vertex_descriptor p;
  119.  
  120.       p = predecessors[*vi];
  121.  
  122.       pe.vertex_id = *vi;
  123.       pe.parent_id = p;
  124.       pe.edge_id   = get_edge_id(graph, p, *vi);
  125.       pe.cost      = distances[*vi];
  126.    
  127.       path_vector.push_back( pe );
  128.     }
  129.   }
  130.  
  131.   if( path_vector.size() == 0 ) {
  132.     *err_msg = (char *)"No path found";
  133.     return 0;
  134.   }
  135.  
  136.   // Why +1?
  137.   *path = (path_element_t *) malloc( sizeof(path_element_t) *
  138.                                      (path_vector.size() + 1) );
  139.   *path_count = path_vector.size();
  140.   std::copy(path_vector.begin(), path_vector.end(), path);
  141.  
  142.  
  143.   return EXIT_SUCCESS;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement