【问题标题】:Why does the read_graphviz() function of the boost graph library changes the index of the nodes为什么boost图库的read_graphviz()函数会改变节点的索引
【发布时间】:2020-05-20 19:25:27
【问题描述】:

使用 boost 图库 的read_graphviz() 读取图时,用于存储节点的内部索引与用于显示相同图的索引相比发生了变化。这在处理大图时是有问题的,因为很难将它们作为文本文件进行比较。

我写了一个小例子(见下面的源代码),我开始阅读一个小图

输入

digraph G {
0[index=0];
1[index=1];
2[index=2];
10[index=10];
}

但是,当使用 write_graphviz() 打印它时,输出顺序会发生变化:

输出

digraph G {
0[index=0];
1[index=1];
2[index=10]; /* <= mismatch here ! */
3[index=2];  /* <= and here !      */
}

虽然我理解为什么节点 index 10 会被重新影响为较小的值,例如所有索引都按升序排列,但我不明白为什么它会受到影响 index 2 而不是 3 因为它出现在索引 2 之后(在输入中)。

我猜这是因为使用的顺序必须是索引属性上的字典顺序,解释为字符串,但真的是这样吗?在我自己的程序中,节点索引超过 150,我得到以下重新排序:

0[index=0000];
1[index=0001];
2[index=0010];
3[index=0100];
4[index=0101];
5[index=0102];
6[index=0103];
7[index=0104];
8[index=0105];
9[index=0106];
10[index=0107];
11[index=0108];
12[index=0109];
13[index=0011];
14[index=0110];
15[index=0111];

我的问题:如何避免这种行为以使内部节点索引始终与我的自己的索引匹配?

这是这个简单示例的代码:

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/graphviz.hpp>

using namespace std;

struct Node { std::size_t index; };


/*! \brief DAG: Directed Acyclic Graph */
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Node> DAG;

template<class IndexMap> class node_writer {
public:
    node_writer(IndexMap n) : inm(n) {}
    template<class Vertex>  void operator()(std::ostream & out, const Vertex & v) {
        out << "[index=" << inm[v] << "]";
    }

private:
    IndexMap inm;
};

template<class IndexMap>
inline node_writer<IndexMap> make_node_writer(IndexMap inm) {
    return node_writer<IndexMap>(inm);
}

std::ostream & operator<<(std::ostream & o, const DAG & g) {
    boost::write_graphviz(o, g, make_node_writer(boost::get(&Node::index, g)));
    return o;
}

std::istream & operator>>(std::istream & i, DAG & g)
{
    boost::dynamic_properties dp(boost::ignore_other_properties);
    dp.property("index", get(&Node::index, g));
    read_graphviz(i, g, dp);
    return i;
}

int main(int argc, char * argv[])
{
    DAG * pTree = new DAG(0);
    std::stringstream  dag_file;
    dag_file << "digraph G {\n";
    dag_file << "0[index=0];\n";
    dag_file << "1[index=1];\n";
    dag_file << "2[index=2];\n"; 
    dag_file << "10[index=10];\n";
        dag_file << "}";
    std::cout << dag_file.str() << "\n";
    dag_file >> *pTree;
    std::cout <<  *pTree;
    return 0;
}

【问题讨论】:

    标签: c++ boost boost-graph


    【解决方案1】:

    我的问题:如何避免这种行为,以使内部节点索引始终与我自己的索引匹配?

    你必须让它打印为 node_id 以及 index 属性。我建议再次使用动态属性:

    std::ostream & operator<<(std::ostream & o, DAG& g) {
        boost::dynamic_properties dp(boost::ignore_other_properties);
        dp.property("node_id", get(&Node::index, g));
        dp.property("index", get(&Node::index, g));
        boost::write_graphviz_dp(o, g, dp);
        return o;
    }
    

    现在打印:Live On Coliru

    digraph G {
    0 [index=0];
    1 [index=1];
    10 [index=10];
    2 [index=2];
    }
    

    注意事项:

    • 出于技术原因,dynamic_properties 默认不接受只读属性映射。您可以解决此问题,因此您可以定义 operator&lt;&lt; 以采用 DAG const&amp;,请参阅 boost::dynamic_properties and immutable graph object
    • 您也可以使用node_id 阅读,但有一些注意事项。您当前的图形模型有一个顶点容器选择器 (vecS),它有一个与向量索引匹配的隐式 vertex_index。从index 属性中读取node_id 将导致“稀疏”图(顶点3..9 也将存在,没有任何边且没有有意义的索引属性)。

      您可以将选择器更改为其他内容,但是您需要 vertex_index 属性映射。将其隐式提供给相关算法的唯一优雅方法是

      • 使用内部属性(而不是您当前使用的属性包,这可能很麻烦)
      • 使用特化属性映射选择器的特征

      展示如何做到这一点,只是为了完整:Live On Coliru

      注意这里不再专门读取 index 属性,因为 node_id 已经直接填充了Node::index。

    完整列表

    上面的第一个解决方案:

    Live On Coliru

    #include <iostream>
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/graphviz.hpp>
    
    using namespace std;
    
    struct Node { std::size_t index; };
    
    /*! \brief DAG: Directed Acyclic Graph */
    typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS, Node> DAG;
    
    std::ostream & operator<<(std::ostream & o, DAG& g) {
        boost::dynamic_properties dp(boost::ignore_other_properties);
        dp.property("node_id", get(&Node::index, g));
        dp.property("index", get(&Node::index, g));
        boost::write_graphviz_dp(o, g, dp);
        return o;
    }
    
    std::istream & operator>>(std::istream & i, DAG & g) {
        boost::dynamic_properties dp(boost::ignore_other_properties);
        dp.property("index", get(&Node::index, g));
        read_graphviz(i, g, dp);
        return i;
    }
    
    int main() {
        auto const input = R"(digraph G {
        0[index=0];
        1[index=1];
        2[index=2];
        10[index=10];
    })";
        std::stringstream dag_file(input);
        std::cout << input << std::endl;
    
        {
            DAG tree;
            dag_file >> tree;
            std::cout << tree;
        }
    }
    

    【讨论】:

    • 我认为使用read_graphviz 并不容易/不可能,因为它读入了中间parse_result,将nodes 硬编码为std::map&lt;name, properties&gt;,这意味着顺序丢失。但是,如果您关心排序,则可以考虑手动解析图表。但是请注意,在一般情况下,您不能期望在不丢失信息的情况下往返整个源,因为 graphviz 描述语言非常通用,并且语义与格式不匹配。 IE。许多输入表示可以匹配同一个图。
    • 有关 graphviz 格式如何描述图形以及为什么需要非常精细的 AST 来对输入进行全保真建模的非常详细的背景信息)请参阅this older answer
    • 一个非常小的语法子集,它将 1. 读取您的示例输入(无边) 2. 保留顶点顺序可以在大约 25 行代码中完成,如下所示:coliru.stacked-crooked.com/a/17861a8a27017175 [注意: 不再链接到 boost_graph 或 boost_regex]
    • 如果您不担心阅读部分图的错误,那么您甚至可以使用语义操作来缩短:coliru.stacked-crooked.com/a/932d031a7c36c72f
    • 事后思考:省略所有ID_coliru.stacked-crooked.com/a/3e5bb9f7d0a58514的属性更简单
    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    相关资源
    最近更新 更多