【问题标题】:Boost: Create Graph from shortest PathBoost:从最短路径创建图
【发布时间】: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


    【解决方案1】:

    我会考虑在原始图上做一个过滤图适配器,过滤掉所有在有趣路径中未访问的顶点/边。

    然后是一个简单的copy_graph 来创建新图表。

    如果您将图表类型更改为 labeled_graphfiltered_graph,那么您甚至不需要副本,具体取决于您的性能权衡。

    【讨论】:

      猜你喜欢
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-19
      相关资源
      最近更新 更多