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 
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
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
graph_analysis::boost_graph::VertexDescriptor
boost::graph_traits< BidirectionalGraph >::vertex_descriptor VertexDescriptor
Definition: DirectedGraph.hpp:31
graph_analysis::boost_graph::EdgeRange
std::pair< EdgeIterator, EdgeIterator > EdgeRange
Definition: DirectedGraph.hpp:38
graph_analysis::boost_graph::AdjacencyRange
std::pair< AdjacencyIterator, AdjacencyIterator > AdjacencyRange
Definition: DirectedGraph.hpp:41
graph_analysis::boost_graph::VRep
boost::listS VRep
Definition: DirectedGraph.hpp:22
graph_analysis::boost_graph::DirectedGraph::addVertexInternal
virtual GraphElementId addVertexInternal(const Vertex::Ptr &vertex)
Add a vertex.
Definition: DirectedGraph.cpp:56
graph_analysis::boost_graph::BidirectionalGraph
boost::adjacency_list< VRep, ERep, GType, VProp, EProp > BidirectionalGraph
Definition: DirectedGraph.hpp:29
graph_analysis::boost_graph::DirectedGraph::removeVertexInternal
virtual void removeVertexInternal(const Vertex::Ptr &)
Definition: DirectedGraph.cpp:74
graph_analysis::boost_graph::DirectedGraph::Ptr
shared_ptr< DirectedGraph > Ptr
Definition: DirectedGraph.hpp:54
graph_analysis::boost_graph::DirectedGraph::getInEdgeIterator
graph_analysis::EdgeIterator::Ptr getInEdgeIterator(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:214
graph_analysis::boost_graph::VertexRange
std::pair< VertexIterator, VertexIterator > VertexRange
Definition: DirectedGraph.hpp:34
graph_analysis::boost_graph::EProp
boost::property< boost::edge_index_t, std::size_t, Edge::Ptr > EProp
Definition: DirectedGraph.hpp:26
graph_analysis::boost_graph::VProp
boost::property< boost::vertex_index_t, std::size_t, Vertex::Ptr > VProp
Definition: DirectedGraph.hpp:25
graph_analysis::boost_graph::OutArcIterator
Definition: ArcIterator.hpp:51
graph_analysis::boost_graph::DirectedGraph::removeEdgeInternal
virtual void removeEdgeInternal(const Edge::Ptr &)
Definition: DirectedGraph.cpp:133
graph_analysis::boost_graph::DirectedGraph
Definition: DirectedGraph.hpp:43
graph_analysis::boost_graph::DirectedGraph::~DirectedGraph
virtual ~DirectedGraph()
Definition: DirectedGraph.cpp:41
graph_analysis::boost_graph::DirectedGraph::msNewVertexId
static std::map< GraphId, GraphElementId > msNewVertexId
Definition: DirectedGraph.hpp:149
graph_analysis::boost_graph::DirectedGraph::write
void write(std::ostream &ostream=std::cout) const
graph_analysis
Main namespace of the graph analysis library.
Definition: BFS.cpp:3
graph_analysis::BaseGraph::Ptr
shared_ptr< BaseGraph > Ptr
Definition: BaseGraph.hpp:34
graph_analysis::boost_graph::DirectedGraph::getVertexIterator
VertexIterator::Ptr getVertexIterator() const
Definition: DirectedGraph.cpp:156
graph_analysis::boost_graph::DirectedGraph::VertexMap
boost::unordered_map< GraphElementId, VertexDescriptor > VertexMap
Definition: DirectedGraph.hpp:55
graph_analysis::Edge::Ptr
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
graph_analysis::boost_graph::DirectedGraph::newInstance
BaseGraph::Ptr newInstance() const
Definition: DirectedGraph.cpp:25
graph_analysis::boost_graph::UndirectedGraph
boost::adjacency_list< VRep, ERep, boost::undirectedS, VProp, EProp > UndirectedGraph
Definition: DirectedGraph.hpp:28
graph_analysis::boost_graph::DirectedGraph::identifyConnectedComponents
SubGraph::Ptr identifyConnectedComponents(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:174
graph_analysis::boost_graph::DirectedGraph::EdgeMap
boost::unordered_map< GraphElementId, EdgeDescriptor > EdgeMap
Definition: DirectedGraph.hpp:56
graph_analysis::boost_graph::DirectedGraph::getOutEdgeIterator
graph_analysis::EdgeIterator::Ptr getOutEdgeIterator(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:208
graph_analysis::boost_graph::ERep
boost::vecS ERep
Definition: DirectedGraph.hpp:23
graph_analysis::boost_graph::DirectedGraph::getEdge
Edge::Ptr getEdge(GraphElementId id) const
Get the edge by id.
Definition: DirectedGraph.cpp:142
graph_analysis::boost_graph::EdgeIteratorImpl
boost::graph_traits< BidirectionalGraph >::edge_iterator EdgeIteratorImpl
Definition: DirectedGraph.hpp:37
graph_analysis::boost_graph::DirectedGraph::getEdgeDescriptor
EdgeDescriptor getEdgeDescriptor(const Edge::Ptr &edge) const
Definition: DirectedGraph.cpp:108
graph_analysis::GraphElementId
uint32_t GraphElementId
Definition: GraphElement.hpp:21
graph_analysis::boost_graph::DirectedGraph::copy
BaseGraph::Ptr copy() const
Definition: DirectedGraph.cpp:20
graph_analysis::boost_graph::DirectedGraph::getEdges
std::vector< Edge::Ptr > getEdges(VertexDescriptor source, VertexDescriptor target) const
Definition: DirectedGraph.cpp:220
graph_analysis::boost_graph::VertexIndex
boost::graph_traits< BidirectionalGraph >::vertices_size_type VertexIndex
Definition: DirectedGraph.hpp:33
graph_analysis::boost_graph::DirectedGraph::createSubGraph
SubGraph::Ptr createSubGraph(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:198
graph_analysis::boost_graph::EdgeDescriptor
boost::graph_traits< BidirectionalGraph >::edge_descriptor EdgeDescriptor
Definition: DirectedGraph.hpp:36
graph_analysis::Vertex::Ptr
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
graph_analysis::boost_graph::DirectedGraph::DirectedGraph
DirectedGraph()
Definition: DirectedGraph.cpp:30
graph_analysis::boost_graph::DirectedGraph::addEdgeInternal
GraphElementId addEdgeInternal(const Edge::Ptr &edge, GraphElementId sourceVertexId, GraphElementId targetVertexId)
Add an edge.
Definition: DirectedGraph.cpp:121
graph_analysis::boost_graph::DirectedGraph::validateType
DirectedGraph::Ptr validateType(const BaseGraph::Ptr &baseGraph) const
Definition: DirectedGraph.cpp:45
graph_analysis::boost_graph::AdjacencyIterator
boost::graph_traits< BidirectionalGraph >::adjacency_iterator AdjacencyIterator
Definition: DirectedGraph.hpp:40
graph_analysis::TypedGraph
Definition: TypedGraph.hpp:9
graph_analysis::boost_graph::DirectedGraph::getVertexDescriptor
VertexDescriptor getVertexDescriptor(const Vertex::Ptr &vertex) const
Definition: DirectedGraph.cpp:97
graph_analysis::boost_graph::DirectedGraph::getEdgeIterator
EdgeIterator::Ptr getEdgeIterator() const
Definition: DirectedGraph.cpp:162
graph_analysis::boost_graph::DirectedGraph::getVertex
Vertex::Ptr getVertex(GraphElementId id) const
Get the vertex by id.
Definition: DirectedGraph.cpp:83
graph_analysis::boost_graph::GType
boost::bidirectionalS GType
Definition: DirectedGraph.hpp:20
graph_analysis::boost_graph::DirectedGraph::msNewEdgeId
static std::map< GraphId, GraphElementId > msNewEdgeId
Definition: DirectedGraph.hpp:150
graph_analysis::SubGraph::Ptr
shared_ptr< SubGraph > Ptr
Definition: SubGraph.hpp:52
graph_analysis::EdgeIterator::Ptr
shared_ptr< EdgeIterator > Ptr
Definition: EdgeIterator.hpp:20
graph_analysis::VertexIterator::Ptr
shared_ptr< VertexIterator > Ptr
Definition: VertexIterator.hpp:21
graph_analysis::boost_graph::DirectedGraph::mEdgeMap
EdgeMap mEdgeMap
Definition: DirectedGraph.hpp:147
graph_analysis::boost_graph::DirectedGraph::mVertexMap
VertexMap mVertexMap
Definition: DirectedGraph.hpp:148
graph_analysis::boost_graph::InArcIterator
Definition: ArcIterator.hpp:98
graph_analysis::boost_graph::VertexIteratorImpl
boost::graph_traits< BidirectionalGraph >::vertex_iterator VertexIteratorImpl
Definition: DirectedGraph.hpp:32