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; }
std::vector< double > mitk::ConnectomicsNetwork::GetShortestDistanceVectorFromLabel( std::string targetLabel ) const { std::vector< VertexDescriptorType > predecessorMap( boost::num_vertices( m_Network ) ); int numberOfNodes( boost::num_vertices( m_Network ) ); std::vector< double > distanceMatrix; distanceMatrix.resize( numberOfNodes ); boost::graph_traits<NetworkType>::vertex_iterator iterator, end; boost::tie(iterator, end) = boost::vertices( m_Network ); while( (iterator != end) && (m_Network[ *iterator ].label != targetLabel) ) { ++iterator; } if( iterator == end ) { MITK_WARN << "Label not found"; return distanceMatrix; } boost::dijkstra_shortest_paths(m_Network, *iterator, boost::predecessor_map(&predecessorMap[ 0 ]).distance_map(&distanceMatrix[ 0 ]).weight_map( boost::get( &NetworkEdge::edge_weight ,m_Network ) ) ) ; return distanceMatrix; }
std::vector<mitk::TubeGraph::TubeDescriptorType> mitk::TubeGraph::SearchAllPathBetweenVertices(const mitk::TubeGraph::TubeDescriptorType& startTube, const mitk::TubeGraph::TubeDescriptorType& endTube/*, std::vector<unsigned long> barrier*/ ) { //http://lists.boost.org/boost-users/att-9001/maze.cpp //http://www.boost.org/doc/libs/1_49_0/libs/graph/example/bfs.cpp typedef std::map<VertexDescriptorType, EdgeDescriptorType> EdgeMap; typedef boost::associative_property_map<EdgeMap> PredecessorMap; typedef boost::edge_predecessor_recorder<PredecessorMap, boost::on_tree_edge> PredecessorVisitor; typedef boost::dfs_visitor< std::pair<PredecessorVisitor, boost::null_visitor> > DFSVisitor; EdgeMap edgesMap; PredecessorMap predecessorMap(edgesMap); PredecessorVisitor predecessorVisitor(predecessorMap); boost::null_visitor nullVisitor; DFSVisitor visitor = boost::make_dfs_visitor(std::make_pair(predecessorVisitor, nullVisitor)); std::map<VertexDescriptorType, boost::default_color_type> vertexColorMap; std::map<EdgeDescriptorType, boost::default_color_type> edgeColorMap; boost::undirected_dfs(m_Graph, visitor, make_assoc_property_map(vertexColorMap), make_assoc_property_map(edgeColorMap), startTube.second ); std::vector<TubeDescriptorType> solutionPath; solutionPath.push_back(endTube); VertexDescriptorType pathEdgeSource = endTube.first; VertexDescriptorType pathEdgeTarget; MITK_INFO << "Source: ["<< startTube.first<<","<<startTube.second<<"] Target: ["<< endTube.first<<","<<endTube.second<<"]"; MITK_INFO<< "tube ["<<endTube.first<<","<<endTube.second<<"]"; do { if(pathEdgeSource == 10393696) break; EdgeDescriptorType edge = get(predecessorMap, pathEdgeSource); pathEdgeSource = boost::source(edge, m_Graph); pathEdgeTarget = boost::target(edge, m_Graph); TubeDescriptorType tube (pathEdgeSource, pathEdgeTarget); MITK_INFO<< "tube ["<<tube.first<<","<<tube.second<<"]"; solutionPath.push_back(tube); } while (pathEdgeSource != startTube.second); return solutionPath; }
void mitk::ConnectomicsShortestPathHistogram::CalculateUnweightedUndirectedShortestPaths( NetworkType* boostGraph ) { std::vector< DescriptorType > predecessorMap( boost::num_vertices( *boostGraph ) ); int numberOfNodes( boost::num_vertices( *boostGraph ) ); m_DistanceMatrix.resize( numberOfNodes ); for(unsigned int index(0); index < m_DistanceMatrix.size(); index++ ) { m_DistanceMatrix[ index ].resize( numberOfNodes ); } IteratorType iterator, end; boost::tie(iterator, end) = boost::vertices( *boostGraph ); for ( int index(0) ; iterator != end; ++iterator, index++) { boost::dijkstra_shortest_paths(*boostGraph, *iterator, boost::predecessor_map(&predecessorMap[ 0 ]).distance_map(&m_DistanceMatrix[ index ][ 0 ]).weight_map( boost::get( &mitk::ConnectomicsNetwork::NetworkEdge::edge_weight ,*boostGraph ) ) ) ; } }
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; }