Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Created March 18, 2024 19:55
Show Gist options
  • Save Hermann-SW/fc6ce25be40b72579c4ff05a38a9a30a to your computer and use it in GitHub Desktop.
Save Hermann-SW/fc6ce25be40b72579c4ff05a38a9a30a to your computer and use it in GitHub Desktop.
Using C++ Boost lib "read_graphviz()" to convert a graph int LEDA graph format
//=======================================================================
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
/*
f=dot2leda
g++ -O3 -Wall -pedantic -Wextra $f.cpp -o $f -lboost_graph
cpplint --filter=-legal/copyright,-build/namespaces $f.cpp
cppcheck --enable=all --suppress=missingIncludeSystem $f.cpp --check-config
*/
//=======================================================================
#include <boost/graph/graphviz.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
using namespace std;
int main(int argc, char *argv[]) {
assert(argc == 2);
typedef adjacency_list< vecS, vecS, undirectedS,
property< vertex_name_t, string > > graph_t;
graph_t graph(0);
dynamic_properties dp;
property_map< graph_t, vertex_name_t >::type vname
= get(vertex_name, graph);
dp.property("node_id", vname);
ifstream gvgraph(argv[1]);
assert(read_graphviz(gvgraph, graph, dp, "node_id"));
cout << "LEDA.GRAPH" << "\n";
cout << "string" << "\n";
cout << "int" << "\n";
cout << num_vertices(graph) << "\n";
BOOST_FOREACH(graph_t::vertex_descriptor v, vertices(graph)) {
cout << get("node_id", dp, v) << "\n";
}
cout << num_edges(graph) << "\n";
BOOST_FOREACH(graph_t::edge_descriptor e, edges(graph)) {
cout << 1+source(e, graph) << " " << 1+target(e, graph)
<< " " << 0 << "\n";
}
return 0;
}
@Hermann-SW
Copy link
Author

Hermann-SW commented Mar 19, 2024

After

sudo apt install libboost-doc
cd
cp -r /usr/share/doc/libboost1.74-doc/examples/libs/graph/example .
cd example

many of the examples making use of read_graphviz() do not even compile:

$ head -14 scc.cpp | tail -6
/*
   IMPORTANT!!!
   ~~~~~~~~~~~~
   This example uses interfaces that have been deprecated and removed from
   Boost.Grpah. Someone needs to update it, as it does NOT compile.
*/
$ 

This demo works, and was the basis I started with for ending in this gist code:

$ g++ graph-thingie.cpp -lboost_graph
$ ./a.out 
graph GRAPH (CX2A1Z)

vertex a (NODE_A)
vertex b (NODE_B)
vertex c ()
$

This is the demo input graph:

$ head -89 graph-thingie.cpp | tail -12
    const char* dot = "digraph \
{ \
  graph [name=\"GRAPH\", identifier=\"CX2A1Z\"] \
    \
    a [label=\"NODE_A\", root=\"1\"] \
    b [label=\"NODE_B\", root=\"0\"] \
 \
 a -> b [label=\"EDGE_1\"] \
 b -> c [label=\"EDGE_2\"] \
}";

    istringstream gvgraph(dot);
$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment