14 #include <boost/graph/graph_traits.hpp>
15 #include <boost/graph/adjacency_list.hpp>
16 #include <boost/graph/subgraph.hpp>
17 #include <boost/graph/graphviz.hpp>
18 #include <boost/bimap.hpp>
23 namespace graphPolicy {
37 template <
class T_VertexProperty = SimpleProperty,
class T_EdgeProperty = SimpleProperty>
41 using GraphType = boost::adjacency_list<boost::vecS,
43 boost::bidirectionalS,
44 boost::property<boost::vertex_index_t, size_t, std::pair<graybat::graphPolicy::VertexID, T_VertexProperty> >,
45 boost::property<boost::edge_index_t, size_t, std::pair<graybat::graphPolicy::EdgeID, T_EdgeProperty> > >;
47 using BGLGraph = boost::subgraph<GraphType>;
48 using BglVertexID =
typename BGLGraph::vertex_descriptor;
49 using BglEdgeID =
typename BGLGraph::edge_descriptor;
50 using VertexID = graybat::graphPolicy::VertexID;
51 using EdgeID = graybat::graphPolicy::EdgeID;
52 using GraphID = graybat::graphPolicy::GraphID;
56 using VertexProperty = T_VertexProperty;
57 using EdgeProperty = T_EdgeProperty;
58 using VertexDescription = graybat::graphPolicy::VertexDescription<BGL>;
59 using EdgeDescription = graybat::graphPolicy::EdgeDescription<BGL>;
60 using GraphDescription = graybat::graphPolicy::GraphDescription<BGL>;
61 using InEdgeIter =
typename boost::graph_traits<BGLGraph>::in_edge_iterator;
62 using OutEdgeIter =
typename boost::graph_traits<BGLGraph>::out_edge_iterator;
63 using AdjacentVertexIter =
typename boost::graph_traits<BGLGraph>::adjacency_iterator;
64 using AllVertexIter =
typename boost::graph_traits<BGLGraph>::vertex_iterator;
68 std::shared_ptr<BGLGraph> graph;
69 std::vector<BGL<VertexProperty, EdgeProperty>> subGraphs;
70 boost::bimap<EdgeID, BglEdgeID> edgeIdBiMap;
81 BGL(GraphDescription graphDesc) :
84 std::vector<VertexDescription> vertices = graphDesc.first;
85 std::vector<EdgeDescription> edges = graphDesc.second;
87 graph = std::make_shared<BGLGraph>(vertices.size());
89 unsigned edgeCount = 0;
92 for(EdgeID edgeId = 0; edgeId < edges.size(); ++edgeId){
93 BglVertexID srcVertex = std::get<0>(edges[edgeId].first);
94 BglVertexID targetVertex = std::get<1>(edges[edgeId].first);
95 BglEdgeID bglEdgeId = boost::add_edge(srcVertex, targetVertex, (*graph)).first;
96 edgeIdBiMap.insert(
typename boost::bimap<EdgeID, BglEdgeID>::value_type(edgeId, bglEdgeId));
97 setEdgeProperty(bglEdgeId, std::make_pair(edgeCount++, edges[edgeId].second));
101 for(VertexDescription &v : vertices){
102 setVertexProperty(v.first, std::make_pair(v.first, v.second));
116 return boost::vertices((*graph));
124 std::pair<EdgeID, bool>
getEdge(
const VertexID source,
const VertexID target){
125 std::pair<BglEdgeID, bool> bglEdgeId = boost::edge(source, target, *graph);
126 return std::make_pair(edgeIdBiMap.right.at(bglEdgeId.first), bglEdgeId.second);
134 return boost::adjacent_vertices(
id, *graph);
141 std::pair<OutEdgeIter, OutEdgeIter>
getOutEdges(
const VertexID
id){
142 return boost::out_edges((*graph).global_to_local(
id), (*graph));
149 std::pair<InEdgeIter, InEdgeIter>
getInEdges(
const VertexID
id){
150 return boost::in_edges((*graph).global_to_local(
id), (*graph));
159 return (*graph).global_to_local(vertex.id);
162 void setVertexProperty(BglVertexID vertex, VertexProperty value){
163 std::pair<VertexID, VertexProperty> propPair = (*graph)[vertex];
164 (*graph)[vertex] = std::make_pair<propPair.first, value>;
167 void setVertexProperty(BglVertexID vertex, std::pair<VertexID, VertexProperty> propPair){
168 (*graph)[vertex] = propPair;
172 void setEdgeProperty(BglEdgeID edge, EdgeProperty value){
173 std::pair<EdgeID, EdgeProperty> propPair = (*graph)[edge];
174 (*graph)[edge] = std::make_pair<propPair.first, value>;
177 void setEdgeProperty(BglEdgeID edge, std::pair<EdgeID, EdgeProperty> propPair){
178 (*graph)[edge] = propPair;
186 return (*graph)[vertex];
195 return (*graph)[edge];
207 return boost::target(edge, (*graph));
219 return boost::source(edge, (*graph));
std::pair< AdjacentVertexIter, AdjacentVertexIter > getAdjacentVertices(const VertexID id)
Returns all vertices, that are adjacent (connected) to vertex
Definition: BGL.hpp:133
BGL(GraphDescription graphDesc)
The graph has to be described by edges (source Vertex ==> target Vertex) and the vertices of this gra...
Definition: BGL.hpp:81
std::pair< VertexID, VertexProperty > & getVertexProperty(const VertexID vertex)
Returns the property of vertex.
Definition: BGL.hpp:185
std::pair< AllVertexIter, AllVertexIter > getVertices()
Returns all vertices of the graph.
Definition: BGL.hpp:115
BglVertexID getEdgeTarget(const BglEdgeID edge)
Return the vertex to which edge points to.
Definition: BGL.hpp:206
BglVertexID getLocalID(VertexProperty vertex)
Returns the local id of vertex in this graph.
Definition: BGL.hpp:158
std::pair< EdgeID, EdgeProperty > & getEdgeProperty(const BglEdgeID edge)
Return the property of edge.
Definition: BGL.hpp:194
std::pair< OutEdgeIter, OutEdgeIter > getOutEdges(const VertexID id)
Returns all outgoing edgpes of srcVertex paired with its target vertex.
Definition: BGL.hpp:141
BglVertexID getEdgeSource(const BglEdgeID edge)
Return the vertex to which edge points from.
Definition: BGL.hpp:218
A class to describe directed graphs.
Definition: BGL.hpp:38
std::pair< InEdgeIter, InEdgeIter > getInEdges(const VertexID id)
Returns all incoming edges to targetVertex paired with its source vertex.
Definition: BGL.hpp:149
std::pair< EdgeID, bool > getEdge(const VertexID source, const VertexID target)
Returns the edge between source and target vertex.
Definition: BGL.hpp:124