Created
March 18, 2024 19:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//======================================================================= | |
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After
many of the examples making use of read_graphviz() do not even compile:
This demo works, and was the basis I started with for ending in this gist code:
This is the demo input graph: