简体   繁体   English

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

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

I am trying to practice some C++ for use with graphs/networks.我正在尝试练习一些用于图形/网络的 C++。 I thought I would write a quick program that reads a GraphML description of a network with edge weights and computes its diameter (or, to start with, just computes the shortest distance from some node to some other node).我想我会写一个快速的程序来读取一个带有边权重的网络的 GraphML 描述并计算它的直径(或者,首先,只计算从某个节点到另一个节点的最短距离)。 However, I don't get on with the Boost documentation.但是,我没有继续阅读 Boost 文档。 Reading up on graphs , associative , dynamic , and general property maps , I still don't understand how to bend the non-functional code below into submission so it reads a simple GraphML file like this阅读图形关联动态通用属性映射,我仍然不明白如何将下面的非功能代码弯曲成提交,所以它读取了一个像这样的简单 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>

and does some distance computations on it.并对其进行一些距离计算。 I don't even know how to do debug outputs (eg. so I can figure out whether dp has any entry for d1 , or whether the name "weight" governs where the properties would end up).我什至不知道如何进行调试输出(例如,我可以弄清楚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)));
}

Currently, executing this dies after “throwing an instance of 'boost::wrapexceptboost::bad_any_cast'”, how do I get it to work?目前,在“抛出一个'boost::wrapexceptboost::bad_any_cast'的实例”之后执行这个死了,我该如何让它工作?

You're on the right path.你在正确的道路上。 Few notes:几点注意事项:

  • property maps are usually adapting other datastructures, so instantiating that associative_property_map with no initialization is like an uninitalized reference属性映射通常会适应其他数据结构,因此在没有初始化的情况下实例化 associative_property_map 就像未初始化的引用
  • refer to the attribute name, not key引用属性名称,而不是键

Here I made it work, dumping the std::map as well as roundtripping the XML.在这里,我使它工作,转储std::map以及往返 XML。 Note that the roundtrips preserves information, not representation:请注意,往返保留信息,而不是表示:

Live On Coliru住在科利鲁

#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);
}

Prints印刷

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>

BONUS: Bundled Properties奖励:捆绑属性

As you suspected will usually be simpler:正如您所怀疑的,通常会更简单:

Live On Coliru 住在科利鲁

#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);
}

Prints印刷

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.

相关问题 boost :: dijkstra_shortest_paths覆盖内部图形权重? - boost::dijkstra_shortest_paths overwriting internal graph weights? boost的dijkstra_shortest_paths中的负边权重检查 - Negative edge weight check in boost's dijkstra_shortest_paths dijkstra_shortest_paths Boost Graph Lib 1.57.0失败 - dijkstra_shortest_paths Boost Graph Lib 1.57.0 fails Boost中的自定义图形距离dijkstra_shortest_paths - Custom graph distance in Boost dijkstra_shortest_paths 增强图:dijkstra_shortest_paths:无法形成对“ void”的引用 - Boost graph: dijkstra_shortest_paths: cannot form a reference to 'void' 当特定节点对之间存在多条最短路径时,boost graph dijkstra_shortest_paths 如何选择最短路径? - How does boost graph dijkstra_shortest_paths pick the shortest path when there are multiple shortest paths between a specific pair of nodes? 用boost :: graph包装我的自定义图并计算dijkstra_shortest_paths - Wrap my custom graph with boost::graph and calculate dijkstra_shortest_paths 我可以在“循环”有向图的 BGL 中使用 dijkstra_shortest_paths - Can I use dijkstra_shortest_paths in BGL on "cyclic" directed graph 在dijkstra_shortest_paths中使用捆绑属性作为权重映射 - Using bundled properties as the weight map in dijkstra_shortest_paths 提升BGL Dijkstra最短路径 - Boost BGL Dijkstra Shortest Paths
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM