Advertisement
Guest User

Boost Graph Problem v2

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