Free Software Foundation GPLv3 LGPL Electronic Frontier Foundation
Mozilla.org OpenOffice.org Linux.com
Learning a new framework always seems tedious at first, but once you have learned the basics, the rest becomes easier and easier. A simple, practical example will probably be best to get started, so let's develop a program that converts GXL files to the DOT fileformat. You'll be surprised how easy it is.

#include "opengt/opengt.h"

using namespace
OpenGraphtheory;
#include <fstream>
// for file writing

using namespace
std;

int
main(
int
argc,
char
** argv)
{
so let's instantiate a graph and load the file
    Graph G;
    G.LoadFromFile(
"myfile.gxl"
);
now open the file for writing the graph as .dot and write a .dot header
    ofstream os(
"myfile.dot"
);
    os <<
"// www.open-graphtheory.org\n"
;
    os <<
"graph gxl2dot {\n"
;
and now iterate through the vertices of G and write their dot-declaration. (We are using iterators here. Iterators are confusing at first and seem dispensable, but once you got used to them, you don't want to live without them anymore
    
for
(VertexIterator v = G.BeginVertices(); v != G.EndVertices(); v++)
        os <<
"   v"
<< v.GetID() <<
"[label=\""
<< v.GetLabel() <<
"\"];\n"
;
afterwards, write the edges to the dot-file (EdgeIterators have methods
First()
and
Second()
which return a VertexIterator for the two vertices that are connected by the edge)
    
for
(EdgeIterator e = G.BeginEdges(); e != G.EndEdges(); e++)
        os <<
"  v"
<< e.First().GetID() <<
" -- v"
<< e.Second().GetID()
           <<
" [label=\""
<< e.GetLabel() <<
"\"];\n"
;
now finish with the footer, close the dot file and we're done
    os <<
"}\n"
;
    os.close();
    
return
0
;
}
You expected it to be much more difficult, didn't you?

Save the sourcecode as gxl2dot.cpp and compile (for example using g++ from the GNU compiler collection), linking it against the open-graphtheory library libopengt.so
g++ -o gxl2dot gxl2dot.cpp -lopengt

Hints:

  • You can load graphs from any istream, like stdin (std::cin in the iostream header), stringstreams etc. using e.g.
    std::cin >> G;
  • You can save graphs to files using the method
    SaveToFile(string filename)
    or write them to any ostream, like stdout (std::cout in the iostream header) e.g.
    std::cout << G;
  • Maybe your program doesn't support hypergraphs, mixed graphs etc. You can use the boolean methods
    directed, undirected, mixed, hypergraph, simple, multigraph, multidigraph
    to sort out illegal inputs. Since the above program only supports undirected, simple graphs, we could make it more robust by adding
    if
    (!(G.undirected() && G.simple()))
    {
        cerr <<
    "illegal file\n"
    ;
        
    return
    1
    ;
    }
  • You can add vertices, edges and arcs using the methods
    AddVertex, AddEdge, AddArc
    .
    • These methods return a Vertex- or EdgeIterator respectively for the newly created object
    • AddEdge takes a set<VertexIterator> or just two VertexIterators as first parameter(s)
    • AddArc takes two set<VertexIterator> or just two VertexIterators as first parameter. The arc goes "from" the elements of the first set "to" the elements of the second set
    • For Vertices you can specify X- and Y-coordinates through the parameters
    • For all 3 types of objects, you can specify a Label and a Weight through the parameters
  • You can delete vertices, edges and arcs using the methods
    RemoveVertex, RemoveEdge, RemoveArc
    . These methods just take a Vertex- or EdgeIterator as parameter.
  • In general, open-graphtheory uses modern programming paradigms, that you should be familiar with if you don't want to end up maintaining legacy code. These paradigms are: The SOLID Principles Streaming, Exceptions, Iterators, XML, Generic Programming and thorough Encapsuling.
Contributors Disclaimer