QList<unsigned> ShortestPathComputer::getShortestPathVertices(const unsigned startVertexIndex, const unsigned endVertexIndex, float *pathLength/* = NULL*/)
{
    QList<unsigned> shortestPathVertices;

    VertexDescriptor startVertex = vertex(startVertexIndex, mGraph);
    std::vector<VertexDescriptor> predecessorMap(num_vertices(mGraph));
    std::vector<float> distanceMap(num_vertices(mGraph));
    dijkstra_shortest_paths(mGraph, startVertex,
        predecessor_map(boost::make_iterator_property_map(predecessorMap.begin(), get(boost::vertex_index, mGraph))).
        distance_map(boost::make_iterator_property_map(distanceMap.begin(), get(boost::vertex_index, mGraph))));

    if (pathLength)
    {
        *pathLength = distanceMap.at(endVertexIndex);
    }

    unsigned vertexIndex1 = endVertexIndex;
    unsigned vertexIndex2 = predecessorMap.at(vertexIndex1);
    while (vertexIndex2 != vertexIndex1)
    {
        shortestPathVertices.push_front(vertexIndex2);
        vertexIndex1 = vertexIndex2;
        vertexIndex2 = predecessorMap.at(vertexIndex1);
    }
    shortestPathVertices.pop_front(); //去掉起点

    return shortestPathVertices;
}
Пример #2
0
int main(int, char *[])
{
  typedef float Weight;
  typedef boost::property<boost::edge_weight_t, Weight> WeightProperty;
  typedef boost::property<boost::vertex_name_t, std::string> NameProperty;

  typedef boost::adjacency_list < boost::listS, boost::vecS, boost::directedS,
    NameProperty, WeightProperty > Graph;

  typedef boost::graph_traits < Graph >::vertex_descriptor Vertex;

  typedef boost::property_map < Graph, boost::vertex_index_t >::type IndexMap;
  typedef boost::property_map < Graph, boost::vertex_name_t >::type NameMap;

  typedef boost::iterator_property_map < Vertex*, IndexMap, Vertex, Vertex& > PredecessorMap;
  typedef boost::iterator_property_map < Weight*, IndexMap, Weight, Weight& > DistanceMap;


  // Create a graph
  Graph g;

  // Add named vertices
  Vertex v0 = add_vertex(std::string("v0"), g);
  Vertex v1 = add_vertex(std::string("v1"), g);
  Vertex v2 = add_vertex(std::string("v2"), g);
  Vertex v3 = add_vertex(std::string("v3"), g);

  // Add weighted edges
  Weight weight0 = 5;
  Weight weight1 = 3;
  Weight weight2 = 2;
  Weight weight3 = 4;

  add_edge(v0, v1, weight0, g);
  add_edge(v1, v3, weight1, g);
  add_edge(v0, v2, weight2, g);
  add_edge(v2, v3, weight3, g);

  // At this point the graph is
  /*    v0
         .
        / \ 2
       /   \
      /     . v2
    5/       \
    /         \ 4
   /           \
  v1----------- v3
      3
  */

  // Create things for Dijkstra
  std::vector<Vertex> predecessors(num_vertices(g)); // To store parents
  std::vector<Weight> distances(num_vertices(g)); // To store distances
/* works
//////////////
  IndexMap indexMap = get(boost::vertex_index, g);
  PredecessorMap predecessorMap(&predecessors[0], indexMap);
  DistanceMap distanceMap(&distances[0], indexMap);

  // Compute shortest paths from v0 to all vertices, and store the output in predecessors and distances
  boost::dijkstra_shortest_paths(g, v0, boost::predecessor_map(predecessorMap).distance_map(distanceMap));
//////////////
*/

//////////////
  IndexMap indexMap = get(boost::vertex_index, g);
  
  DistanceMap distanceMap(&distances[0], indexMap);

  PredecessorMap predecessorMap(&predecessors[0], indexMap);
  boost::predecessor_map(predecessorMap).distance_map(distanceMap);
  // Compute shortest paths from v0 to all vertices, and store the output in predecessors and distances
  boost::dijkstra_shortest_paths(g, v0, boost::predecessor_map(predecessorMap));
//////////////
  
  
  // Output results
  std::cout << "distances and parents:" << std::endl;
  NameMap nameMap = get(boost::vertex_name, g);

  BGL_FORALL_VERTICES(v, g, Graph) 
  {
    std::cout << "distance(" << nameMap[v0] << ", " << nameMap[v] << ") = " << distanceMap[v] << ", ";
    std::cout << "predecessor(" << nameMap[v] << ") = " << nameMap[predecessorMap[v]] << std::endl;
  }