【发布时间】:2017-03-15 13:09:45
【问题描述】:
目前我正在使用 boost 图形库。 我的图由自定义顶点和边属性组成:
typedef boost::labeled_graph<boost::adjacency_list<
boost::listS, boost::vecS, boost::directedS, Vertex, Edge>, int> Graph;
Graph g;
我需要计算最短路径(Dijkstra)的功能,因此用户必须选择一个或多个开始和结束节点。在选择节点并计算每个开始和结束节点之间的最短路径后,应该创建一个新图。最后,新图应该包含位于每条最短路径上的所有顶点/边。
我的想法是:
1:我对计算出的最短路径类型进行回溯
typedef std::vector< VertexDescriptor> Path;
2:我检查顶点是否已经包含在新图表中。 (我不知道如何处理),如果是这样,我将顶点复制到新图表中。
3:我检查边是否已经包含在新图形中,如果是,我将边复制到新图形中。
Graph graphPaths;
Path::reverse_iterator rit;
VertexDescriptor lastNode = *path.rbegin();
for (rit = path.rbegin(); rit != path.rend(); ++rit) {
// Vertex v =
// check if vertices already exist in new GraphPath
if (graphPaths[indexMap[*rit]] == NULL) {
Vertex v = g[indexMap[*rit]];
VertexDescriptor vd = boost::add_vertex(indexMap[*rit], graphPaths);
graphPaths[indexMap[*rit]] = v;
}
// check if edge is already included in new Graph
if (!boost::edge(lastNode, *rit, graphPaths).second) {
Graph::edge_descriptor ep = boost::edge(lastNode, *rit, g).first;
boost::add_edge_by_label(indexMap[lastNode], indexMap[*rit], g[ep],
graphPaths);
}
}
lastNode = *rit;
}
如何检查图中是否存在顶点。您是否有其他想法来改进流程或解决问题。
谢谢 迈克尔
【问题讨论】:
标签: c++ boost graph shortest-path dijkstra