graph_analysis
BaseGraph.hpp
Go to the documentation of this file.
1 #ifndef GRAPH_ANALYSIS_BASE_GRAPH_HPP
2 #define GRAPH_ANALYSIS_BASE_GRAPH_HPP
3 
4 #include <set>
5 #include "SharedPtr.hpp"
6 #include "EdgeIterator.hpp"
7 #include "VertexIterator.hpp"
8 #include "Filter.hpp"
9 #include "SubGraph.hpp"
10 #include "Algorithms.hpp"
11 #include "BaseIterable.hpp"
12 #include "BaseGraphObserver.hpp"
13 #include "HyperEdge.hpp"
14 
18 namespace graph_analysis
19 {
20 
27 class BaseGraph : public enable_shared_from_this<BaseGraph>, public VertexIterable, public EdgeIterable, public Algorithms
28 {
29 
30 public:
32  static std::map<ImplementationType, std::string> ImplementationTypeTxt;
33 
34  typedef shared_ptr<BaseGraph> Ptr;
35 
39  virtual ~BaseGraph() {}
40 
46 
50  void addObserver(const BaseGraphObserver::Ptr& observer);
51 
55  void removeObserver(const BaseGraphObserver::Ptr& observer);
56 
73  void transactionEvent( TransactionType eventType );
74 
82  virtual BaseGraph::Ptr copy() const { throw std::runtime_error("BaseGraph::copy: not implemented"); }
83 
88  BaseGraph::Ptr clone() const;
89 
93  BaseGraph::Ptr cloneEdges() const;
94 
98  virtual BaseGraph::Ptr newInstance() const { throw std::runtime_error("BaseGraph::newInstance: not implemented"); }
99 
100 
107  static SubGraph::Ptr getSubGraph(const Ptr& graph);
108 
113  ImplementationType getImplementationType() const { return mImplementationType; }
114 
119  std::string getImplementationTypeName() const { return ImplementationTypeTxt[mImplementationType]; }
120 
125  bool empty() const;
126 
138  GraphElementId addVertex(const Vertex::Ptr& vertex);
139 
144  GraphElementId getVertexId(const Vertex::Ptr& vertex) const { (void) vertex; return dynamic_pointer_cast<GraphElement>(vertex)->getId( getId() ); }
145 
150  virtual Vertex::Ptr getVertex(GraphElementId id) const { (void) id; throw std::runtime_error("graph_analysis::BaseGraph::getVertex has not been implemented"); }
151 
157  void removeVertex(const Vertex::Ptr& vertex);
158 
163  GraphElementId addEdge(const Edge::Ptr& edge);
164 
170  void removeEdge(const Edge::Ptr& edge);
171 
176  virtual size_t removeEdges(const Vertex::Ptr& a, const Vertex::Ptr& b);
177 
182  GraphElementId addHyperEdge(const HyperEdge::Ptr& hyperEdge) { return addVertex(hyperEdge); }
183 
188  void removeHyperEdge(const HyperEdge::Ptr& hyperEdge) { removeVertex(hyperEdge); }
189 
194  bool contains(const Edge::Ptr& edge) const;
195 
200  bool contains(const Vertex::Ptr& vertex) const;
201 
206  GraphElementId getEdgeId(const Edge::Ptr& edge) const { (void) edge; return dynamic_pointer_cast<GraphElement>(edge)->getId( getId() ); }
207 
212  virtual Edge::Ptr getEdge(GraphElementId id) const { (void) id; throw std::runtime_error("BaseGraph::getEdge: not implemented"); }
213 
219  virtual std::vector<Edge::Ptr> getEdges(const Vertex::Ptr& source, const Vertex::Ptr& target) const;
220 
227  template<typename T>
228  std::vector< shared_ptr<T> > getEdges(const Vertex::Ptr& source, const Vertex::Ptr& target) const
229  {
230  std::vector< shared_ptr<T> > edges;
231  EdgeIterator::Ptr edgeIt = getEdgeIterator(source);
232  while(edgeIt->next())
233  {
234  shared_ptr<T> edge = dynamic_pointer_cast<T>( edgeIt->current() );
235  if(edge && edge->getTargetVertex() == target)
236  {
237  edges.push_back(edge);
238  }
239  }
240 
241  return edges;
242  }
243 
249  template<typename T>
250  std::vector< shared_ptr<T> > getVertices() const
251  {
252  std::vector< shared_ptr<T> > vertices;
254  while(vertexIt->next())
255  {
256  shared_ptr<T> vertex = dynamic_pointer_cast<T>( vertexIt->current() );
257  if(vertex)
258  {
259  vertices.push_back(vertex);
260  }
261  }
262 
263  return vertices;
264  }
265 
270  GraphId getId() const { return mId; }
271 
276  virtual VertexIterator::Ptr getVertexIterator() const { throw std::runtime_error("graph_analysis::BaseGraph::getVertexIterator has not been implemented"); }
278 
283  virtual EdgeIterator::Ptr getEdgeIterator() const { throw std::runtime_error("graph_analysis::BaseGraph::getEdgeIterator has not been implemented"); }
285 
291  virtual std::vector<Vertex::Ptr> getAllVertices() const;
292 
298  virtual std::vector<Edge::Ptr> getAllEdges() const;
299 
304  virtual EdgeIterator::Ptr getEdgeIterator(const Vertex::Ptr& vertex) const { (void) vertex; throw std::runtime_error("BaseGraph::getEdgeIterator: not implemented"); }
305 
310  static SubGraph::Ptr applyFilters(const Ptr& graph, const Filter<Vertex::Ptr>::Ptr& vertexFilter, const Filter<Edge::Ptr>::Ptr& edgeFilter);
311 
315  void clear();
316 
321  size_t size() const { return getAllEdges().size(); }
322 
327  size_t order() const { return getAllVertices().size(); }
328 
333  virtual EdgeIterator::Ptr getOutEdgeIterator(const Vertex::Ptr& vertex) const { return getEdgeIterator(vertex); }
334 
339  virtual EdgeIterator::Ptr getInEdgeIterator(const Vertex::Ptr& vertex) const { return getEdgeIterator(vertex); }
340 
344  std::vector<Edge::Ptr> getInEdges(const Vertex::Ptr& vertex) const;
345 
349  std::vector<Edge::Ptr> getOutEdges(const Vertex::Ptr& vertex) const;
350 
353 
357  bool isUndirected() const { return !isDirected(); }
358 
362  bool isDirected() const { return mDirected; }
363 
364 protected:
365 
369  BaseGraph(ImplementationType type, bool directed);
370 
375  virtual GraphElementId addVertexInternal(const Vertex::Ptr&) { throw std::runtime_error("BaseGraph::addVertexInternal: not implemented"); }
376 
381  virtual void removeVertexInternal(const Vertex::Ptr&) { throw std::runtime_error("BaseGraph::removeVertexInternal: not implemented"); }
382 
388  virtual GraphElementId addEdgeInternal(const Edge::Ptr& edge, GraphElementId sourceVertexId, GraphElementId edgeVertexId) { (void) edge; (void) sourceVertexId; (void) edgeVertexId; throw std::runtime_error("BaseGraph::addEdgeInternal: not implemented"); }
389 
394  virtual void removeEdgeInternal(const Edge::Ptr&) { throw std::runtime_error("BaseGraph::removeEdgeInternal: not implemented"); }
395 
400  virtual SubGraph::Ptr createSubGraph(const Ptr& baseGraph) const { (void) baseGraph; throw std::runtime_error("BaseGraph::createSubGraph: not implemented"); }
401 
402 private:
404  GraphId mId;
406  static GraphId msId;
407  ImplementationType mImplementationType;
408 
409  // Graph attribute
410  bool mDirected;
411 
412  // The current hook
413  std::set<BaseGraphObserver::Ptr> mObservers;
414 
415  // Notification of observers
416  void notifyAll(const Vertex::Ptr& vertex, const EventType& event);
417  void notifyAll(const Edge::Ptr& edge, const EventType& event);
418  void notifyAll(const TransactionType& event);
419 };
420 
421 } // end namespace graph_analysis
422 #endif // GRAPH_ANALYSIS_BASE_GRAPH_HPP
graph_analysis::BaseGraph::applyFilters
static SubGraph::Ptr applyFilters(const Ptr &graph, const Filter< Vertex::Ptr >::Ptr &vertexFilter, const Filter< Edge::Ptr >::Ptr &edgeFilter)
Definition: BaseGraph.cpp:364
graph_analysis::VertexIterable
Definition: VertexIterable.hpp:8
graph_analysis::BaseGraph::edges
virtual BaseIterable< EdgeIterator::Ptr, BaseGraph, Edge::Ptr > edges()
Definition: BaseGraph.hpp:284
graph_analysis::BaseGraph::clear
void clear()
Definition: BaseGraph.cpp:375
graph_analysis::BaseGraph::IMPLEMENTATION_TYPE_END
@ IMPLEMENTATION_TYPE_END
Definition: BaseGraph.hpp:31
HyperEdge.hpp
Filter.hpp
SharedPtr.hpp
graph_analysis::BaseGraph::removeVertex
void removeVertex(const Vertex::Ptr &vertex)
Remove vertex In order to reimplement, call the base function first BaseGraph::addVertex(v)
Definition: BaseGraph.cpp:181
graph_analysis::BaseGraph::LEMON_DIRECTED_GRAPH
@ LEMON_DIRECTED_GRAPH
Definition: BaseGraph.hpp:31
graph_analysis::BaseGraph::addVertexInternal
virtual GraphElementId addVertexInternal(const Vertex::Ptr &)
Definition: BaseGraph.hpp:375
graph_analysis::BaseGraph::addVertex
GraphElementId addVertex(const Vertex::Ptr &vertex)
Add a vertex.
Definition: BaseGraph.cpp:166
graph_analysis::BaseGraph::createSubGraph
virtual SubGraph::Ptr createSubGraph(const Ptr &baseGraph) const
Definition: BaseGraph.hpp:400
graph_analysis::BaseGraph::getAllEdges
virtual std::vector< Edge::Ptr > getAllEdges() const
Definition: BaseGraph.cpp:328
graph_analysis::BaseGraph
General Graph template that should be implemented to wrap specific graph library functionality We ass...
Definition: BaseGraph.hpp:27
graph_analysis::BaseIterable
Definition: BaseIterable.hpp:13
graph_analysis::GraphId
uint32_t GraphId
Definition: GraphElement.hpp:22
graph_analysis::BaseGraph::getOutEdgeIterator
virtual EdgeIterator::Ptr getOutEdgeIterator(const Vertex::Ptr &vertex) const
Definition: BaseGraph.hpp:333
BaseIterable.hpp
graph_analysis::BaseGraph::addHyperEdge
GraphElementId addHyperEdge(const HyperEdge::Ptr &hyperEdge)
Definition: BaseGraph.hpp:182
graph_analysis::BaseGraph::order
size_t order() const
Definition: BaseGraph.hpp:327
graph_analysis::BaseGraph::SNAP_DIRECTED_GRAPH
@ SNAP_DIRECTED_GRAPH
Definition: BaseGraph.hpp:31
graph_analysis::EdgeIterable
Definition: EdgeIterable.hpp:8
graph_analysis::BaseGraph::getOutEdges
std::vector< Edge::Ptr > getOutEdges(const Vertex::Ptr &vertex) const
Definition: BaseGraph.cpp:417
graph_analysis::BaseGraph::getVertices
std::vector< shared_ptr< T > > getVertices() const
Get all vertices of a given type Type should be a subclass of vertex If dynamic_pointer_cast returns ...
Definition: BaseGraph.hpp:250
graph_analysis::BaseGraph::getEdgeIterator
virtual EdgeIterator::Ptr getEdgeIterator(const Vertex::Ptr &vertex) const
Definition: BaseGraph.hpp:304
graph_analysis::BaseGraph::getImplementationType
ImplementationType getImplementationType() const
Definition: BaseGraph.hpp:113
graph_analysis::BaseGraphObserver::Ptr
shared_ptr< BaseGraphObserver > Ptr
Definition: BaseGraphObserver.hpp:39
graph_analysis::Filter::Ptr
shared_ptr< FilterType > Ptr
Definition: Filter.hpp:59
graph_analysis::BaseGraph::getEdges
virtual std::vector< Edge::Ptr > getEdges(const Vertex::Ptr &source, const Vertex::Ptr &target) const
Get edge by given vertices.
Definition: BaseGraph.cpp:262
graph_analysis::BaseGraph::isDirected
bool isDirected() const
Definition: BaseGraph.hpp:362
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::BaseGraph::getEdgeId
GraphElementId getEdgeId(const Edge::Ptr &edge) const
Get the edge id for this graph.
Definition: BaseGraph.hpp:206
graph_analysis::BaseGraph::removeObserver
void removeObserver(const BaseGraphObserver::Ptr &observer)
Definition: BaseGraph.cpp:50
graph_analysis::BaseGraph::getEdges
std::vector< shared_ptr< T > > getEdges(const Vertex::Ptr &source, const Vertex::Ptr &target) const
Get edges by given vertices and return only edges of a given type that end at target Type should be a...
Definition: BaseGraph.hpp:228
graph_analysis::Edge::Ptr
shared_ptr< Edge > Ptr
Definition: Edge.hpp:21
graph_analysis::BaseGraph::getId
GraphId getId() const
Definition: BaseGraph.hpp:270
Algorithms.hpp
graph_analysis::BaseGraph::getInstance
static Ptr getInstance(ImplementationType type=LEMON_DIRECTED_GRAPH)
Definition: BaseGraph.cpp:26
graph_analysis::BaseGraph::getEdge
virtual Edge::Ptr getEdge(GraphElementId id) const
Get the edge by id.
Definition: BaseGraph.hpp:212
graph_analysis::BaseGraph::~BaseGraph
virtual ~BaseGraph()
Default deconstructor.
Definition: BaseGraph.hpp:39
graph_analysis::BaseGraph::removeVertexInternal
virtual void removeVertexInternal(const Vertex::Ptr &)
Definition: BaseGraph.hpp:381
graph_analysis::BaseGraph::transactionEvent
void transactionEvent(TransactionType eventType)
issue a transaction event The transaction event is passed to all observers, which can implement the r...
Definition: BaseGraph.cpp:88
graph_analysis::BaseGraph::getEdgeIterator
virtual EdgeIterator::Ptr getEdgeIterator() const
Definition: BaseGraph.hpp:283
graph_analysis::BaseGraph::empty
bool empty() const
tests whether the graph is void/empty
Definition: BaseGraph.cpp:400
graph_analysis::BaseGraph::addEdge
GraphElementId addEdge(const Edge::Ptr &edge)
Add an edge Cannot be reimplemented directly, but see addEdge(Edge::Ptr, GraphElementId,...
Definition: BaseGraph.cpp:194
graph_analysis::BaseGraph::ImplementationType
ImplementationType
Definition: BaseGraph.hpp:31
graph_analysis::BaseGraph::getVertex
virtual Vertex::Ptr getVertex(GraphElementId id) const
Get the vertex by id.
Definition: BaseGraph.hpp:150
graph_analysis::BaseGraph::size
size_t size() const
Definition: BaseGraph.hpp:321
graph_analysis::BaseGraph::getAllVertices
virtual std::vector< Vertex::Ptr > getAllVertices() const
Definition: BaseGraph.cpp:317
VertexIterator.hpp
graph_analysis::GraphElementId
uint32_t GraphElementId
Definition: GraphElement.hpp:21
graph_analysis::BaseGraph::getVertexIterator
virtual VertexIterator::Ptr getVertexIterator() const
Definition: BaseGraph.hpp:276
graph_analysis::Vertex::Ptr
shared_ptr< Vertex > Ptr
Definition: Vertex.hpp:22
SubGraph.hpp
graph_analysis::BaseGraph::removeEdge
void removeEdge(const Edge::Ptr &edge)
Definition: BaseGraph.cpp:249
graph_analysis::BaseGraph::contains
bool contains(const Edge::Ptr &edge) const
Definition: BaseGraph.cpp:339
graph_analysis::BaseGraph::copy
virtual BaseGraph::Ptr copy() const
Definition: BaseGraph.hpp:82
graph_analysis::BaseGraph::getImplementationTypeName
std::string getImplementationTypeName() const
Definition: BaseGraph.hpp:119
graph_analysis::BaseGraph::ImplementationTypeTxt
static std::map< ImplementationType, std::string > ImplementationTypeTxt
Definition: BaseGraph.hpp:32
graph_analysis::BaseGraph::getInEdgeIterator
virtual EdgeIterator::Ptr getInEdgeIterator(const Vertex::Ptr &vertex) const
Definition: BaseGraph.hpp:339
graph_analysis::BaseGraph::inEdges
SpecializedIterable< EdgeIterator::Ptr, BaseGraph, Edge::Ptr, Vertex::Ptr > inEdges(const Vertex::Ptr &vertex) const
Definition: BaseGraph.cpp:429
graph_analysis::BaseGraph::removeEdgeInternal
virtual void removeEdgeInternal(const Edge::Ptr &)
Definition: BaseGraph.hpp:394
graph_analysis::BaseGraph::cloneEdges
BaseGraph::Ptr cloneEdges() const
Definition: BaseGraph.cpp:136
graph_analysis::TransactionType
TransactionType
Transaction events These events are triggers for the notification system to allow transactions to be ...
Definition: BaseGraphObserver.hpp:27
graph_analysis::BaseGraph::getInEdges
std::vector< Edge::Ptr > getInEdges(const Vertex::Ptr &vertex) const
Definition: BaseGraph.cpp:406
graph_analysis::BaseGraph::addObserver
void addObserver(const BaseGraphObserver::Ptr &observer)
Definition: BaseGraph.cpp:45
graph_analysis::BaseGraph::BaseGraph
BaseGraph(ImplementationType type, bool directed)
Definition: BaseGraph.cpp:19
graph_analysis::BaseGraph::isUndirected
bool isUndirected() const
Definition: BaseGraph.hpp:357
graph_analysis::BaseGraph::clone
BaseGraph::Ptr clone() const
Definition: BaseGraph.cpp:93
graph_analysis::SpecializedIterable
Definition: BaseIterable.hpp:126
graph_analysis::HyperEdge::Ptr
shared_ptr< HyperEdge > Ptr
Definition: HyperEdge.hpp:20
graph_analysis::SubGraph::Ptr
shared_ptr< SubGraph > Ptr
Definition: SubGraph.hpp:52
BaseGraphObserver.hpp
graph_analysis::BaseGraph::addEdgeInternal
virtual GraphElementId addEdgeInternal(const Edge::Ptr &edge, GraphElementId sourceVertexId, GraphElementId edgeVertexId)
Definition: BaseGraph.hpp:388
graph_analysis::BaseGraph::vertices
virtual BaseIterable< VertexIterator::Ptr, BaseGraph, Vertex::Ptr > vertices()
Definition: BaseGraph.hpp:277
EdgeIterator.hpp
graph_analysis::BaseGraph::outEdges
SpecializedIterable< EdgeIterator::Ptr, BaseGraph, Edge::Ptr, Vertex::Ptr > outEdges(const Vertex::Ptr &vertex) const
Definition: BaseGraph.cpp:434
graph_analysis::EdgeIterator::Ptr
shared_ptr< EdgeIterator > Ptr
Definition: EdgeIterator.hpp:20
graph_analysis::BaseGraph::BOOST_DIRECTED_GRAPH
@ BOOST_DIRECTED_GRAPH
Definition: BaseGraph.hpp:31
graph_analysis::VertexIterator::Ptr
shared_ptr< VertexIterator > Ptr
Definition: VertexIterator.hpp:21
graph_analysis::BaseGraph::getVertexId
GraphElementId getVertexId(const Vertex::Ptr &vertex) const
Get the vertex id for this graph.
Definition: BaseGraph.hpp:144
graph_analysis::Algorithms
Interface definition for a collection of standard algorithms.
Definition: Algorithms.hpp:14
graph_analysis::EventType
EventType
Types of events for the observer. Example: notify(vertex, EVENT_TYPE_ADDED, ...) is called whenever a...
Definition: BaseGraphObserver.hpp:15
graph_analysis::BaseGraph::removeEdges
virtual size_t removeEdges(const Vertex::Ptr &a, const Vertex::Ptr &b)
Definition: BaseGraph.cpp:291
graph_analysis::BaseGraph::newInstance
virtual BaseGraph::Ptr newInstance() const
Definition: BaseGraph.hpp:98
graph_analysis::BaseGraph::removeHyperEdge
void removeHyperEdge(const HyperEdge::Ptr &hyperEdge)
Definition: BaseGraph.hpp:188
graph_analysis::BaseGraph::getSubGraph
static SubGraph::Ptr getSubGraph(const Ptr &graph)
Definition: BaseGraph.cpp:161