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.
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);
}
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;
}
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 ;
}
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.
Notes:
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.