繁体   English   中英

如何从 GraphML 获取边权重到 boost::dijkstra_shortest_paths?

[英]How do I get edge weights from GraphML into boost::dijkstra_shortest_paths?

我正在尝试练习一些用于图形/网络的 C++。 我想我会写一个快速的程序来读取一个带有边权重的网络的 GraphML 描述并计算它的直径(或者,首先,只计算从某个节点到另一个节点的最短距离)。 但是,我没有继续阅读 Boost 文档。 阅读图形关联动态通用属性映射,我仍然不明白如何将下面的非功能代码弯曲成提交,所以它读取了一个像这样的简单 GraphML 文件

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"  
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns 
        http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="d0" for="node" attr.name="color" attr.type="string">
    <default>yellow</default>
  </key>
  <key id="d1" for="edge" attr.name="weight" attr.type="double"/>
  <graph id="G" edgedefault="undirected">
    <node id="n0"/> <node id="n1"/> <node id="n2"/>
    <edge id="e0" source="n0" target="n1">
      <data key="d1">3.2</data>
    </edge>
    <edge id="e1" source="n0" target="n2"/>
  </graph>
</graphml>

并对其进行一些距离计算。 我什至不知道如何进行调试输出(例如,我可以弄清楚dp是否有任何条目d1 ,或者name “权重”是否决定了属性的最终位置)。

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/graph_concepts.hpp>
#include <boost/graph/graph_selectors.hpp>
#include <boost/graph/graphml.hpp>
#include <boost/graph/named_function_params.hpp>
#include <ios>
#include <iostream>
#include <list>
#include <map>

#include "cmcmc/tmp.hpp"
#include <string>

using namespace std;

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using Vertex = boost::graph_traits<Graph>::vertex_descriptor;
using Edge = boost::graph_traits<Graph>::edge_descriptor;
using IndexMap = boost::property_map<Graph, boost::vertex_index_t>::type;
using VertexIter = boost::graph_traits<Graph>::vertex_iterator;

int main(int argc, char *argv[])
{
  if (argc <= 1)
  {
    cout << "No GraphML file given.";
    return 1;
  }

  Graph g;
  boost::dynamic_properties dp{ boost::ignore_other_properties };
  // Read the d1 property of the graph
  boost::associative_property_map<std::map<Edge, double>> d1map{};
  dp.property("d1", d1map);
  ifstream in{ argv[1] };
  boost::read_graphml(in, g, dp);

  IndexMap index = get(boost::vertex_index, g);
  Vertex random_node = *vertices(g).first;
  boost::dijkstra_shortest_paths(g, random_node, boost::distance_map(boost::get(d1map, g)));
}

目前,在“抛出一个'boost::wrapexceptboost::bad_any_cast'的实例”之后执行这个死了,我该如何让它工作?

你在正确的道路上。 几点注意事项:

  • 属性映射通常会适应其他数据结构,因此在没有初始化的情况下实例化 associative_property_map 就像未初始化的引用
  • 引用属性名称,而不是键

在这里,我使它工作,转储std::map以及往返 XML。 请注意,往返保留信息,而不是表示:

住在科利鲁

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/property_map/property_map.hpp>

#define FMT_DEPRECATED_OSTREAM
#include <fmt/ranges.h>
#include <fmt/ostream.h>

using G = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS>;
using V = G::vertex_descriptor;
using E = G::edge_descriptor;

int main() {
  G g;
  std::map<E, double> d1;

  boost::dynamic_properties dp(boost::ignore_other_properties);
  dp.property("weight", boost::make_assoc_property_map(d1));

  {
      std::ifstream ifs("input.xml");
      read_graphml(ifs, g, dp);
  }

  fmt::print("Weight map: {}\n", d1);

  // roundtrip
  write_graphml(std::cout, g, dp);
}

印刷

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp -lboost_graph -lfmt && ./a.out
Weight map: {(0,1): 3.2}
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="key0" for="edge" attr.name="weight" attr.type="double" />
  <graph id="G" edgedefault="undirected" parse.nodeids="free" parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0">
    </node>
    <node id="n1">
    </node>
    <node id="n2">
    </node>
    <edge id="e0" source="n0" target="n1">
      <data key="key0">3.2</data>
    </edge>
    <edge id="e1" source="n0" target="n2">
      <data key="key0">0</data>
    </edge>
  </graph>
</graphml>

奖励:捆绑属性

正如您所怀疑的,通常会更简单:

住在科利鲁

#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphml.hpp>
#include <boost/property_map/dynamic_property_map.hpp>
#include <boost/property_map/property_map.hpp>

struct VertexProperties {
    double weight;
};
struct EdgeProperties {
    double weight;
};
using G = boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
                                VertexProperties, EdgeProperties>;
using V = G::vertex_descriptor;
using E = G::edge_descriptor;

int main() {
  G g;

  boost::dynamic_properties dp(boost::ignore_other_properties);
  dp.property("weight", get(&EdgeProperties::weight, g));

  {
      std::ifstream ifs("input.xml");
      read_graphml(ifs, g, dp);
  }

  // roundtrip
  write_graphml(std::cout, g, dp);
}

印刷

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp -lboost_graph && ./a.out
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="key0" for="edge" attr.name="weight" attr.type="double" />
  <graph id="G" edgedefault="undirected" parse.nodeids="free" parse.edgeids="canonical" parse.order="nodesfirst">
    <node id="n0">
    </node>
    <node id="n1">
    </node>
    <node id="n2">
    </node>
    <edge id="e0" source="n0" target="n1">
      <data key="key0">3.2</data>
    </edge>
    <edge id="e1" source="n0" target="n2">
      <data key="key0">0</data>
    </edge>
  </graph>
</graphml>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM