title: Creat boost grd graph in order to run dijkstra shortest_path and A* algorithm fro dijkstra
body: <p>I am new in Boost and I'm trying to write a program to Dijkstra-SP and Dijkstra with A*. The graph take random edge weight between [0,100]. I have many problems as we can see and I need some suggestions in order to solve it.</p>
  
  <pre><code>#include &lt;boost/graph/grid_graph.hpp&gt;
  #include &lt;boost/graph/adjacency_list.hpp&gt;
  #include &lt;iostream&gt;
  
  typedef boost::grid_graph&lt;2&gt; grid;
  typedef boost::graph_traits&lt;grid&gt;::vertices_size_type vertices_size_type;
  typedef boost::property_map&lt;grid, boost::vertex_index_t&gt;::const_type GridIndexMapType;
  
  GridIndexMapType gridIndexMap(get(boost::vertex_index, graph));
  
  shared_array_property_map &lt; vertex_descriptor, property_map&lt;graph_t, vertex_index_t&gt;::const_type&gt;
      p_map(num_vertices(graph), get(vertex_index, graph));
  shared_array_property_map&lt;double, property_map&lt;graph_t, vertex_index_t&gt;::const_type&gt;
      d_map(num_vertices(graph), get(vertex_index, graph));
  
  
  struct edge_weight_map;
  namespace boost 
  {
      template&lt;&gt;
      struct property_map&lt; graph_t, edge_weight_t &gt; {
          typedef edge_weight_map type;
          typedef edge_weight_map const_type;
      };
  }
  
  /*
   Map from edges to weight values
  */
  struct edge_weight_map 
  {
      typedef double value_type;
      typedef value_type reference;
      typedef edge_descriptor key_type;
      typedef boost::readable_property_map_tag category;
  
      const graph_t&amp; m_graph;
  
      edge_weight_map(const graph_t&amp; g)
      :m_graph(g) { }
      reference operator[](key_type e) const; // implemented below
  };
  
  typedef boost::property_map&lt;graph_t, boost::edge_weight_t&gt;::const_type
      const_edge_weight_map;
  typedef boost::property_traits&lt;const_edge_weight_map&gt;::reference
      edge_weight_map_value_type;
  typedef boost::property_traits&lt;const_edge_weight_map&gt;::key_type
      edge_weight_map_key;
  
  namespace boost{
      // PropertyMap valid expressions
      edge_weight_map_value_type get(const_edge_weight_map pmap, 
      edge_weight_map_key e) {
          return pmap[e]; }
      // ReadablePropertyGraph valid expressions
      const_edge_weight_map get(boost::edge_weight_t, const graph_t&amp;g) {
          return const_edge_weight_map(g); }
      edge_weight_map_value_type get(boost::edge_weight_t tag, const graph_t&amp; g, 
      edge_weight_map_key e) 
      { return get(tag, g)[e]; }
  }
  
  
  edge_weight_map::reference
  edge_weight_map::operator[](key_type e) const {
      vertex_descriptor t = target(e,m_graph);
      vertex_descriptor s = source(e,m_graph);
       // f = f(t,s)
      return f;
  }
  
  class grid_graph {
      public:
      friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const grid_graph&amp;);
      //friend grid_graph random_maze(std::size_t, std::size_t);
  
       grid_graph():m_grid(create_grid(0, 0)) {};
       grid_graph(std::size_t x, std::size_t y):m_grid(create_grid(x, y)){};
  
      // The length of the grid_graph along the specified dimension.
      vertices_size_type length(std::size_t d) const {return m_grid.length(d);}
  
  
      private:
      // Create the underlying rank-2 grid with the specified dimensions.
      grid create_grid(std::size_t x, std::size_t y) {
          boost::array&lt;std::size_t, 2&gt; lengths = { {x, y} };
          return grid(lengths);
      } 
  
      // The grid underlying the grid_graph
      grid m_grid;  
  };
  
  
  // Euclidean heuristic for a grid
  //
  // This calculates the Euclidean distance between a vertex and a goal
  // vertex.
  class euclidean_heuristic {
       // public boost::astar_heuristic&lt;filtered_grid, double&gt;
  
      public:
       euclidean_heuristic(vertex_descriptor goal):m_goal(goal) {};
  
      double operator()(vertex_descriptor v) {
          return sqrt(pow(m_goal[0] - v[0], 2) + pow(m_goal[1] - v[1], 2));
      }
  
      private:
      vertex_descriptor m_goal;
  };
  
  // Exception thrown when the goal vertex is found
  struct found_goal {};
  
  // Visitor that terminates when we find the goal vertex
  struct astar_goal_visitor:public boost::default_astar_visitor 
  {
      astar_goal_visitor(vertex_descriptor goal):m_goal(goal) {};
  
      void examine_vertex(vertex_descriptor u, const filtered_grid&amp;) {
          if (u == m_goal)
              throw found_goal();
      }
  
       private:
       vertex_descriptor m_goal;
  };
  
      void find_shortest(int start, int end, std::vector&lt;std::map&lt;ulong,unlong&gt;&gt;&amp; 
      graph)
      {
          std::set&lt;unlong&gt;                              searchedList;
          std::priority_queue&lt;std::pair&lt;ulong, ulong&gt;&gt;  frontierList;
  
          frontierList.push(std::make_pair(0, start));
  
          while(!frontierList.empty())
          {
              std::pair&lt;ulong, ulong&gt; next = frontierList.top();
              frontierList.pop();
  
              if (next.second == end) {
                  std::cout &lt;&lt; "Min Cost: " &lt;&lt; next.first &lt;&lt; "\n";
                  return;
              }
              if (searchedList.find(next.second) != searchedList.end()) {
                  continue; 
              }
  
              searchedList.insert(next.second);
  
              for(std::map&lt;ulong,unlong&gt;::const_iterator loop = graph[next.second].begin(); loop != graph[next.second].end(); ++loop)
              {    
                  frontierList.push(std::make_pair(next.first + loop-&gt;second, loop-&gt;first));
              }
          }
  
          boolgrid_graph::solve() {
  
          // The predecessor map is a vertex-to-vertex mapping.
          typedef boost::unordered_map&lt;vertex_descriptor,
                                 vertex_descriptor,
                                 vertex_hash&gt; pred_map;
          pred_map predecessor;
          boost::associative_property_map&lt;pred_map&gt; pred_pmap(predecessor);
          // The distance map is a vertex-to-distance mapping.
          typedef boost::unordered_map&lt;vertex_descriptor,
                                 distance,
                                 vertex_hash&gt; dist_map;
          dist_map distance;
          boost::associative_property_map&lt;dist_map&gt; dist_pmap(distance);
  
          vertex_descriptor s = source();
          vertex_descriptor g = goal();
          euclidean_heuristic heuristic(g);
          astar_goal_visitor visitor(g);
  
          try {
              astar_search(m_barrier_grid, s, heuristic,
                   boost::weight_map(weight).
                   predecessor_map(pred_pmap).
                   distance_map(dist_pmap).
                   visitor(visitor) );
          } catch(found_goal fg) {
              // Walk backwards from the goal through the predecessor chain adding
              // vertices to the solution path.
              for (vertex_descriptor u = g; u != s; u = predecessor[u])
                  m_solution.insert(u);
  
              m_solution.insert(s);
              m_solution_length = distance[g];
  
              return true;
          }
        return false;
      }
  
  int main(int,char*[]){
  
      boost::array&lt;std::size_t, 2&gt; lengths = { { 30,50 } };
      GraphType grid(lengths);
  
      if (grid_graph.solve())
          output &lt;&lt; std::endl &lt;&lt; "Solution length " &lt;&lt; m.m_solution_length;
  }
  </code></pre>