【问题标题】:How to read DIMACS Vertex-coloring graphs in C++?如何在 C++ 中读取 DIMACS 顶点着色图?
【发布时间】:2015-05-23 17:04:18
【问题描述】:

我正在尝试重现在 this paper 中进行的实验,在 DIMACS Vertex-Coloring 基准图上测量算法的性能,可以在 here 找到。

这些图表采用 DIMACS 标准格式,我想将它们解析为 C++ Boost 图形库格式,以便在它们上运行我的算法。

我已经尝试使用现有的 Boost DIMACS 函数来解析它们,但是关于它们的文档相当稀少,所以我不清楚如何准确地使用这些函数。当我将图表打印到 Graphviz 时,结果似乎与 DIMACS 文件不匹配。

我想知道:

  1. 我在使用 Boost 解析函数时做错了什么? (见下例)

  2. 是否有更好或替代的 C++ 库可以轻松解析 DIMACS 标准图形格式?

这是我解析和打印图表的尝试:

#include <cstdlib>
#include <iostream>

#include <boost/property_map/property_map.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>
#include <boost/graph/dimacs.hpp>

#include <fstream>

using namespace boost::graph;

typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS > Graph;
typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; 


int main()
{
std::ifstream inGraphFile;
inGraphFile.open("myciel4.col");


dimacs_basic_reader reader(inGraphFile, false);
dimacs_basic_reader end;
dimacs_edge_iterator<dimacs_basic_reader> dimacsStart(reader);
dimacs_edge_iterator<dimacs_basic_reader> endIter(end);


Graph g2(dimacsStart, endIter, reader.n_vertices());
boost::write_graphviz(std::cout, g2);

}

【问题讨论】:

    标签: c++ boost graph boost-graph graph-coloring


    【解决方案1】:

    呵呵。这将是今天报告的第三个提升错误。

    dimacs 解析代码在我看来真的很邪恶。接口很不方便,实现......好吧,你发现它非常错误:

    在read_edge_line 中有这个循环:

    if ('e' == linebuf[0]) {
        while (*tmp != '\n' && *tmp != '\0') {
            if (*tmp == ' ') { 
                *tmp = '\0';
                ts = ++tmp;
                break;
            }
            tmp++;
        }
        *tmp = '\0';                                    /* LINE 156 */
        if (NULL == fs || NULL == ts) return false;
        from = atoi(fs); to = atoi(ts); weight = 0;
    
    } else // ...
    

    当然,通过c_str() char const* 指针写入是一开始就未定义的行为(强制转换(char*) buf.c_str() 是不可取的,尽管大多数库实现在这里都会做预期的事情)。

    但是,第 156 行中的 *tmp = '\0'; 会丢弃目标顶点编号。 atoi 静默失败,to 中有不确定数据。

    手动解析器

    我真的建议您自己编写解析,而不是这种混乱。可以很简单:

    Live On Coliru

    #include <string>
    #include <istream>
    #include <sstream>
    
    template <typename Graph>
    bool read_coloring_problem(std::istream& dimacs, Graph& g) {
        size_t vertices = 0, edges = 0;
    
        std::string line;
        while (getline(dimacs, line))
        {
            std::istringstream iss(line);
            char ch;
            if (iss >> ch)
            {
                size_t from, to;
                std::string format;
    
                switch(ch) {
                    case 'c': break;
                    case 'p': 
                        if (vertices||edges) return false;
                        if (iss >> format >> vertices >> edges) {
                            if ("edge" != format) return false;
                        }
                        break;
                    case 'e': 
                        if (edges-- && (iss >> from >> to) && (add_edge(from-1, to-1, g).second))
                            break;
                    default: 
                        return false;
                }
            }
        }
    
        return !(edges || !dimacs.eof());
    }
    

    Boost Spirit 解析器

    但我更喜欢使用像 Boost Spirit 这样的解析生成器:

    Live On Coliru

    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/phoenix.hpp>
    #include <boost/spirit/include/qi_match.hpp>
    
    template <typename Graph>
    bool read_coloring_problem(std::istream& dimacs, Graph& g) {
        auto add_edge_ = [&g](size_t from, size_t to) { add_edge(from, to, g); };
        size_t vertices = 0, edges = 0;
    
        using namespace boost::spirit::qi;
        namespace px = boost::phoenix;
        uint_parser<size_t> num_;
    
        auto eoil      = eol | eoi;
        auto comment   = boost::proto::deep_copy(lexeme["c " >> *(char_ - eol) >> eoil] | eol);
        auto vertices_ = px::ref(vertices);
        auto edges_    = px::ref(edges);
    
        dimacs >> std::noskipws >> phrase_match(
                *comment >>
                ("p" >> lit("edge") >> num_ [vertices_ = _1] >> num_ [edges_ = _1] >> eoil) >>
                repeat(edges_) [ 
                    *comment >> ("e" >> num_ >> num_ >> eoil) [ px::bind(add_edge_, _1-1, _2-1) ]
                ] 
                >> *comment >> eoi
                , blank);
    
        return dimacs;
    }
    

    输出

    这两个版本的结果都在这个图表中:

    【讨论】:

    • 我有updated the "simple" parser。它有错误。这就是我更喜欢解析器生成器方法的原因。
    • 谢谢!我以前写过解析器,但从来没有用 C++ 写过,而且我对 DIMACS 格式的细节也不是很熟悉。我会马上试试这些。非常感谢!
    • 我用这个mat.gsia.cmu.edu/COLOR/general/ccformat.ps描述作为参考
    猜你喜欢
    • 1970-01-01
    • 2018-05-28
    • 2012-08-05
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多