奇怪的错误导入点文件 [英] Odd Error Importing DOT files

查看:150
本文介绍了奇怪的错误导入点文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的C ++程序,我需要使用Boost图,后来又输出点文件点文件中读取。不过,我遇到了读入阶段这实在是拧紧了我的程序一个奇怪的错误。

For my C++ program, I need to read in a DOT file using Boost Graph, and later output another DOT file. However, I am encountering a weird error in the read-in stage which is really screwing up my program.

我读入code(Graph类型是一个双向升压图​​的类型定义)

My read-in code (Graph type is a typedef of a bidirectional Boost graph)

void readGraph(Graph& graph, string filename) {

    boost::dynamic_properties dp(boost::ignore_other_properties);

    ifstream fin(filename.c_str());
    boost::read_graphviz(fin, graph, dp);

}

好了,问题的是,在.DOT文件中的节点在错误的顺序读取的 的!我用一个简单的例子.DOT文件,试了一下:

Ok, so the problem is that the nodes in the .DOT file are read in in the wrong order! I tried it with a simple example .DOT file:

digraph G {
0; 1; 2; 3; 4; 5; 6; 7; 8; 9; 10;
0->1; 1->0;
1->2; 2->1;
2->3; 3->2;
3->4; 4->3;
4->5; 5->4;
5->6; 6->5;
6->7; 7->6;
7->8; 8->7;
8->9; 9->8;
9->10; 10->9;
}

这是从节点0到节点10但是,如果我读使用Boost图,并将其输出文件的双向链立即无需改动,就变成:

This is a bi-directional chain from node 0 to node 10. However if I read this file using Boost Graph and output it immediately without changes, it becomes:

digraph G {
0;
1;
2;
3;
4;
5;
6;
7;
8;
9;
10;
0->1 ;
1->3 ;
3->4 ;
4->5 ;
5->6 ;
6->7 ;
7->8 ;
8->9 ;
9->10 ;
10->2 ;
1->0 ;
3->1 ;
4->3 ;
5->4 ;
6->5 ;
7->6 ;
8->7 ;
9->8 ;
10->9 ;
2->10 ;
}

注意,节点2现在莫名其妙地连接到结点10,并且是在链的末端。我曾经做过的没有的阅读和输出图形之间。

Notice, node 2 is now inexplicably connected to node 10, and is at the end of the chain. I have done nothing in between reading and outputting the graph.

注:


  • 当我尝试这更复杂.DOT文件,图的拓扑结构是一样的,它只是节点已经被置换了一些奇怪的原因。

  • When I try this with more complicated .DOT files, the topology of the graph remains the same, it's just that the nodes have been permuted for some odd reason.

我知道这是一个读,不会写错误,因为当我输出在节目中的顶点和边,他们已经搞砸了。

I know it is a read, not write error, because when I output the vertices and edges during the program, they are already screwed up.

谁能帮我了解并解决这一问题?谢谢你。

Can anyone help me understand and fix this? Thanks.

推荐答案

如果你读了图形和打印(以​​graphviz的格式)再次产生,你会发现,图形是等价(或同构

If you read the graph and print the resulting again (in graphviz format), you'll find that the graphs are equivalent (or isomorphic):

<大骨节病> 住在Coliru

#include <boost/graph/graphviz.hpp>
#include "/Archive2/45/a4410ef1bd3024/main.cpp" // alias <libs/graph/src/read_graphviz_new.

using Graph = boost::adjacency_list<boost::vecS, boost::vecS, boost::directedS>;

void readGraph(Graph &graph, std::string filename) {
    boost::dynamic_properties dp(boost::ignore_other_properties);

    std::ifstream fin(filename.c_str());
    boost::read_graphviz(fin, graph, dp);
}

int main() {
    Graph g;
    readGraph(g, "input.dot");

    boost::write_graphviz(std::cout, g);
}

通过您的输入:

在这里输入的形象描述

的输出是显然异Morphic环

The output is clearly iso-morphic:

在这里输入的形象描述

注意在你自己的问题中显示的输出是一个简单的呢!

NOTE The output shown in your own question is simply that too!

你真正想要的是顶点的ID被保留。

What you actually want is for the vertex ID's to be retained.

在为了做到这一点,你必须存放顶点ID(从点文件中读取)到属性明确。下面是一个例子:

In order to do that, you'll have to store the vertex ID (as read from the dot file) into a property explicitly. Here is an example:

住在Coliru

Live On Coliru

#include <boost/graph/graphviz.hpp>
#include "/Archive2/45/a4410ef1bd3024/main.cpp" // alias <libs/graph/src/read_graphviz_new.

using namespace boost;

struct MyVertex {
    int id;
};

using Graph = adjacency_list<
    vecS, vecS, directedS, 
    MyVertex
>;

void readGraph(Graph &graph, std::string filename) {
    boost::dynamic_properties dp(boost::ignore_other_properties);
    dp.property("node_id", boost::get(&MyVertex::id, graph));

    std::ifstream fin(filename.c_str());
    boost::read_graphviz(fin, graph, dp);
}

int main() {
    Graph g;
    readGraph(g, "input.dot");

    boost::dynamic_properties dp(boost::ignore_other_properties);
    dp.property("node_id", boost::get(&MyVertex::id, g));
    boost::write_graphviz_dp(std::cout, g, dp);
}

生成:

在这里输入的形象描述

这篇关于奇怪的错误导入点文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆