graph_analysis
DirectedGraph.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_BOOST_GRAPH_DIRECTED_GRAPH_HPP
2 #define GRAPH_ANALYSIS_BOOST_GRAPH_DIRECTED_GRAPH_HPP
3 
4 #include "../Graph.hpp"
5 #include "../BaseGraph.hpp"
6 #include "../boost_graph/ArcIterator.hpp"
7 #include <boost/graph/adjacency_list.hpp>
8 #include <base-logging/Logging.hpp>
9 
10 #include <boost/unordered_map.hpp>
11 #include <vector>
12 #include <iostream>
13 
14 namespace graph_analysis {
16 namespace boost_graph {
17 
18 //typedef boost::undirectedS GType;
19 //typedef boost::directedS GType;
20 typedef boost::bidirectionalS GType;
21 
22 typedef boost::listS VRep;
23 typedef boost::vecS ERep;
24 // https://www.boost.org/doc/libs/1_60_0/libs/graph/doc/property.html
25 typedef boost::property< boost::vertex_index_t, std::size_t, Vertex::Ptr> VProp;
26 typedef boost::property< boost::edge_index_t, std::size_t, Edge::Ptr> EProp;
27 
28 typedef boost::adjacency_list<VRep, ERep, boost::undirectedS, VProp, EProp> UndirectedGraph;
29 typedef boost::adjacency_list<VRep, ERep, GType, VProp, EProp> BidirectionalGraph;
30 
31 typedef boost::graph_traits<BidirectionalGraph>::vertex_descriptor VertexDescriptor;
32 typedef boost::graph_traits<BidirectionalGraph>::vertex_iterator VertexIteratorImpl;
33 typedef boost::graph_traits<BidirectionalGraph>::vertices_size_type VertexIndex;
34 typedef std::pair<VertexIterator, VertexIterator> VertexRange;
35 
36 typedef boost::graph_traits<BidirectionalGraph>::edge_descriptor EdgeDescriptor;
37 typedef boost::graph_traits<BidirectionalGraph>::edge_iterator EdgeIteratorImpl;
38 typedef std::pair<EdgeIterator, EdgeIterator> EdgeRange;
39 
40 typedef boost::graph_traits<BidirectionalGraph>::adjacency_iterator AdjacencyIterator;
41 typedef std::pair<AdjacencyIterator, AdjacencyIterator> AdjacencyRange;
42 
43 class DirectedGraph : public TypedGraph< BidirectionalGraph, graph_analysis::BaseGraph>
44 {
47 public:
48  DirectedGraph();
49  virtual ~DirectedGraph();
50 
51  BaseGraph::Ptr copy() const;
53 
54  typedef shared_ptr<DirectedGraph> Ptr;
55  typedef boost::unordered_map<GraphElementId, VertexDescriptor> VertexMap;
56  typedef boost::unordered_map<GraphElementId, EdgeDescriptor> EdgeMap;
57 
60 
65  VertexDescriptor getVertexDescriptor(const Vertex::Ptr& vertex) const;
66 
71  EdgeDescriptor getEdgeDescriptor(const Edge::Ptr& edge) const;
72 
73  DirectedGraph(const DirectedGraph& other);
74 
75  void write(std::ostream& ostream = std::cout) const;
76 
81 
86 
91  EdgeIterator::Ptr getEdgeIterator(const Vertex::Ptr& vertex) const;
92 
98 
103 
108  SubGraph::Ptr createSubGraph(const BaseGraph::Ptr& baseGraph) const;
109 
115  std::vector<Edge::Ptr> getEdges(VertexDescriptor source, VertexDescriptor target) const;
116 
117  std::vector<Edge::Ptr> getEdges(const Vertex::Ptr& source, const Vertex::Ptr& target) const;
118 
119 protected:
120 
121  DirectedGraph::Ptr validateType(const BaseGraph::Ptr& baseGraph) const;
122 
127  virtual GraphElementId addVertexInternal(const Vertex::Ptr& vertex);
128 
132  virtual void removeVertexInternal(const Vertex::Ptr&);
133 
138  GraphElementId addEdgeInternal(const Edge::Ptr& edge, GraphElementId sourceVertexId, GraphElementId targetVertexId);
139 
144  virtual void removeEdgeInternal(const Edge::Ptr&);
145 
146  // Property maps to store data associated with vertices and edges
147  EdgeMap mEdgeMap;
148  VertexMap mVertexMap;
149  static std::map<GraphId, GraphElementId> msNewVertexId;
150  static std::map<GraphId, GraphElementId> msNewEdgeId;
151 };
152 
153 } // end namespace boost_graph
154 } // end namespace graph_analysis
155 
156 #endif // GRAPH_ANALYSIS_BOOST_DIRECTED_GRAPH_HPP
boost::adjacency_list< VRep, ERep, GType, VProp, EProp > BidirectionalGraph
Definition: DirectedGraph.hpp:29
boost::graph_traits< BidirectionalGraph >::vertex_iterator VertexIteratorImpl
Definition: DirectedGraph.hpp:32
boost::graph_traits< BidirectionalGraph >::vertex_descriptor VertexDescriptor
Definition: DirectedGraph.hpp:31
Definition: ArcIterator.hpp:98
SubGraph::Ptr identifyConnectedComponents(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:174
uint32_t GraphElementId
Definition: GraphElement.hpp:21
std::pair< VertexIterator, VertexIterator > VertexRange
Definition: DirectedGraph.hpp:34
Definition: TypedGraph.hpp:9
std::pair< EdgeIterator, EdgeIterator > EdgeRange
Definition: DirectedGraph.hpp:38
boost::property< boost::vertex_index_t, std::size_t, Vertex::Ptr > VProp
Definition: DirectedGraph.hpp:25
std::pair< AdjacencyIterator, AdjacencyIterator > AdjacencyRange
Definition: DirectedGraph.hpp:41
Vertex::Ptr getVertex(GraphElementId id) const
Get the vertex by id.
Definition: DirectedGraph.cpp:83
boost::property< boost::edge_index_t, std::size_t, Edge::Ptr > EProp
Definition: DirectedGraph.hpp:26
Definition: ArcIterator.hpp:51
GraphElementId addEdgeInternal(const Edge::Ptr &edge, GraphElementId sourceVertexId, GraphElementId targetVertexId)
Add an edge.
Definition: DirectedGraph.cpp:121
graph_analysis::EdgeIterator::Ptr getOutEdgeIterator(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:208
boost::unordered_map< GraphElementId, EdgeDescriptor > EdgeMap
Definition: DirectedGraph.hpp:56
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
virtual void removeVertexInternal(const Vertex::Ptr &)
Definition: DirectedGraph.cpp:74
std::vector< Edge::Ptr > getEdges(VertexDescriptor source, VertexDescriptor target) const
Definition: DirectedGraph.cpp:220
DirectedGraph()
Definition: DirectedGraph.cpp:30
VertexIterator::Ptr getVertexIterator() const
Definition: DirectedGraph.cpp:156
EdgeMap mEdgeMap
Definition: DirectedGraph.hpp:147
boost::adjacency_list< VRep, ERep, boost::undirectedS, VProp, EProp > UndirectedGraph
Definition: DirectedGraph.hpp:28
virtual ~DirectedGraph()
Definition: DirectedGraph.cpp:41
VertexMap mVertexMap
Definition: DirectedGraph.hpp:148
VertexDescriptor getVertexDescriptor(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:97
boost::vecS ERep
Definition: DirectedGraph.hpp:23
DirectedGraph::Ptr validateType(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:45
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
BaseGraph::Ptr newInstance() const
Definition: DirectedGraph.cpp:25
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
shared_ptr< DirectedGraph > Ptr
Definition: DirectedGraph.hpp:54
shared_ptr< EdgeIterator > Ptr
Definition: EdgeIterator.hpp:20
boost::graph_traits< BidirectionalGraph >::vertices_size_type VertexIndex
Definition: DirectedGraph.hpp:33
boost::graph_traits< BidirectionalGraph >::edge_iterator EdgeIteratorImpl
Definition: DirectedGraph.hpp:37
static std::map< GraphId, GraphElementId > msNewEdgeId
Definition: DirectedGraph.hpp:150
virtual GraphElementId addVertexInternal(const Vertex::Ptr &vertex)
Add a vertex.
Definition: DirectedGraph.cpp:56
shared_ptr< SubGraph > Ptr
Definition: SubGraph.hpp:52
EdgeDescriptor getEdgeDescriptor(const Edge::Ptr &edge) const
Definition: DirectedGraph.cpp:108
boost::graph_traits< BidirectionalGraph >::edge_descriptor EdgeDescriptor
Definition: DirectedGraph.hpp:36
shared_ptr< VertexIterator > Ptr
Definition: VertexIterator.hpp:21
BaseGraph::Ptr copy() const
Definition: DirectedGraph.cpp:20
boost::bidirectionalS GType
Definition: DirectedGraph.hpp:20
virtual void removeEdgeInternal(const Edge::Ptr &)
Definition: DirectedGraph.cpp:133
void write(std::ostream &ostream=std::cout) const
shared_ptr< BaseGraph > Ptr
Definition: BaseGraph.hpp:34
boost::graph_traits< BidirectionalGraph >::adjacency_iterator AdjacencyIterator
Definition: DirectedGraph.hpp:40
EdgeIterator::Ptr getEdgeIterator() const
Definition: DirectedGraph.cpp:162
Definition: DirectedGraph.hpp:43
static std::map< GraphId, GraphElementId > msNewVertexId
Definition: DirectedGraph.hpp:149
boost::unordered_map< GraphElementId, VertexDescriptor > VertexMap
Definition: DirectedGraph.hpp:55
SubGraph::Ptr createSubGraph(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:198
graph_analysis::EdgeIterator::Ptr getInEdgeIterator(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:214
Edge::Ptr getEdge(GraphElementId id) const
Get the edge by id.
Definition: DirectedGraph.cpp:142
boost::listS VRep
Definition: DirectedGraph.hpp:22