【问题标题】:Using vertex_name when reading a GraphML file with Boost Graph使用 Boost Graph 读取 GraphML 文件时使用 vertex_name
【发布时间】:2013-05-21 09:57:34
【问题描述】:

我正在尝试加载一个简单的 GraphML 文件,以便每个顶点都有一个存储在 GraphML 中的顶点名称。我可以更改 GraphML,重要的是之后我可以从代码中访问 vertex_name。

这是我可以提取的最小示例,但仍然显示问题:

#include <iostream>
#include <string>
#include <fstream>

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

int main()
{
    using namespace boost;
    typedef adjacency_list<vecS, vecS, directedS,property<vertex_name_t,std::string> > BoostGraphType;
    typedef dynamic_properties BoostDynamicProperties;

    std::string fn = "simple.graphml";
    std::ifstream is(fn.c_str()); 
    if (!is.is_open())
    {
        std::cout << "loading file '" << fn << "'failed." << std::endl;
        throw "Could not load file.";
    }

    BoostGraphType g;
    BoostDynamicProperties dp ;
    const std::string vn = "vertex_name";
    dp.property(vn,get(vertex_name,g));
    read_graphml(is, g, dp);

    for (auto vp = vertices(g); vp.first != vp.second; ++vp.first)
    {
        std::cout << "index '" << get(vertex_index,g,*vp.first) << "' ";
        std::cout << "name '" << get(vertex_name,g,*vp.first) << "'"
        << std::endl;
    }

    return 0;
}

我正在使用以下 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="vertex_name" attr.type="string"/>
  <graph id="G" edgedefault="directed">
    <node id="A"> <data key="d0">A</data> </node>
    <node id="B"> <data key="d0">B</data> </node>
    <edge id="0" source="A" target="B"/>
   </graph>
</graphml>

read_graphml 抛出异常并显示有用的消息 (e.what()):

 parse error: unrecognized type "   

问题似乎与 vertex_name 关联(我从previous question of mine 的评论中得到)有关。

如果我删除

<data key="d0">A</data>

从节点,它可以工作。

但是,我需要能够通过 vertex_name 来识别顶点。

我该如何解决这个问题,以便它正确解析 graphml 而不会抛出?我做错了什么?

【问题讨论】:

  • 在来自维基百科的基本示例 GraphML 上试用 -en.wikipedia.org/wiki/GraphML。您将知道错误是否与格式错误的文件有关。
  • 我认为问题部分与格式错误的文件有关,但我无法找出我到底做错了什么。我更新了问题。

标签: c++ boost graph boost-graph


【解决方案1】:

您的代码在我运行时运行良好。

>wilbert.exe
index '0' name 'A'
index '1' name 'B'

这是在 Windows 7 上使用 boost v1.52

【讨论】:

  • 我在 windows 上试过了,使用 boost 1.53。您使用的是什么操作系统和 boost?
猜你喜欢
  • 2017-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 2014-02-21
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多