Limbo 3.5.4
Loading...
Searching...
No Matches
GraphUtility.h
Go to the documentation of this file.
1
10
11#ifndef LIMBO_ALGORITHMS_GRAPHUTILITY_H
12#define LIMBO_ALGORITHMS_GRAPHUTILITY_H
13
14#include <fstream>
15#include <string>
16#include <algorithm>
17#include <map>
18#include <boost/graph/graph_concepts.hpp>
19#include <boost/graph/iteration_macros.hpp>
20
22namespace limbo
23{
25namespace algorithms
26{
27
33template <typename GraphType>
34void complement_graph(GraphType const& g, GraphType& gp,
35 std::map<typename boost::graph_traits<GraphType>::vertex_descriptor,
36 typename boost::graph_traits<GraphType>::vertex_descriptor>& mCompG2G)
37{
38 typedef typename boost::graph_traits<GraphType>::vertex_descriptor vertex_descriptor;
39 std::map<vertex_descriptor, vertex_descriptor> vmap;
40
41 BGL_FORALL_VERTICES_T(v, g, GraphType)
42 {
43 vertex_descriptor u = boost::add_vertex(gp);
44 vmap[v] = u;
45 mCompG2G[u] = v;
46 }
47
48 BGL_FORALL_VERTICES_T(u, g, GraphType)
49 {
50 std::vector<vertex_descriptor> neighbors(
51 adjacent_vertices(u, g).first,
52 adjacent_vertices(u, g).second
53 );
54 std::sort(neighbors.begin(), neighbors.end());
55 BGL_FORALL_VERTICES_T(v, g, GraphType)
56 {
57 // Might want to check for self-loops
58 if (v != u && !std::binary_search(neighbors.begin(), neighbors.end(), v))
59 boost::add_edge(vmap[u], vmap[v], gp);
60 }
61 }
62}
63
66template <typename GraphType>
68{
70 typedef GraphType graph_type;
71 typedef typename boost::graph_traits<graph_type>::vertex_descriptor vertex_descriptor;
73
74 graph_type const& g;
75
78 VertexLabelWriter(graph_type const& _g) : g(_g) {}
81 vertex_descriptor label(vertex_descriptor v) const {return v;}
82};
83
86template <typename GraphType>
88{
90 typedef GraphType graph_type;
91 typedef typename boost::graph_traits<graph_type>::edge_descriptor edge_descriptor;
92 typedef typename boost::property_traits<typename boost::property_map<graph_type, boost::edge_weight_t>::const_type>::value_type edge_weight_type;
94
95 graph_type const& g;
96
99 EdgeLabelWriter(graph_type const& _g) : g(_g) {}
102 edge_weight_type label(edge_descriptor e) const {return boost::get(boost::edge_weight, g, e);}
104 std::string color(edge_descriptor ) const {return "black";}
106 std::string style(edge_descriptor ) const {return "solid";}
107};
108
118template <typename GraphType, typename VertexLabelType, typename EdgeLabelType>
119void write_graph(std::ofstream& out, GraphType const& g, VertexLabelType const& vl, EdgeLabelType const& el)
120{
121 out << "graph D { \n"
122 << " randir = LR\n"
123 << " size=\"4, 3\"\n"
124 << " ratio=\"fill\"\n"
125 << " edge[style=\"bold\",fontsize=200]\n"
126 << " node[shape=\"circle\",fontsize=200]\n";
127
128 //output nodes
129 uint32_t vertex_num = boost::num_vertices(g);
130 for(uint32_t k = 0; k < vertex_num; ++k)
131 {
132 // output vertex label
133 out << " " << k << "[shape=\"circle\""<< ",label=\"" << vl.label(k) << "\"]\n";
134 }//end for
135
136 //output edges
137 typename boost::graph_traits<GraphType>::edge_iterator ei, eie;
138 for (boost::tie(ei, eie) = boost::edges(g); ei != eie; ++ei)
139 {
140 out << " " << boost::source(*ei, g) << "--" << boost::target(*ei, g)
141 << "[label=" << el.label(*ei) << ",color=\"" << el.color(*ei) << "\",style=\"" << el.style(*ei) << "\",penwidth=3]\n";
142 }
143 out << "}";
144}
145
150inline void graphviz2pdf(std::string const& filename, const char* suffix = ".gv")
151{
152 char cmd[100];
153 sprintf(cmd, "dot -Tpdf %s%s -o %s.pdf", filename.c_str(), suffix, filename.c_str());
154 system(cmd);
155}
156
157} // namespace algorithms
158} // namespace limbo
159
160#endif
namespace for Limbo.algorithms
void complement_graph(GraphType const &g, GraphType &gp, std::map< typename boost::graph_traits< GraphType >::vertex_descriptor, typename boost::graph_traits< GraphType >::vertex_descriptor > &mCompG2G)
get the complement graph of original graph
void graphviz2pdf(std::string const &filename, const char *suffix=".gv")
convert graphviz format to pdf. The input filename should be filename+suffix
void write_graph(std::ofstream &out, GraphType const &g, VertexLabelType const &vl, EdgeLabelType const &el)
write graph to graphviz format and convert to pdf. Although Boost.Graph has write_graphviz component,...
namespace for Limbo
graph_type const & g
bind graph object
edge_weight_type label(edge_descriptor e) const
std::string style(edge_descriptor) const
std::string color(edge_descriptor) const
EdgeLabelWriter(graph_type const &_g)
graph_type const & g
bind graph object
VertexLabelWriter(graph_type const &_g)
vertex_descriptor label(vertex_descriptor v) const