bpp-core3  3.0.0
Graph.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_GRAPH_GRAPH_H
6 #define BPP_GRAPH_GRAPH_H
7 
8 #include <algorithm>
9 #include <iostream>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 
18 // forward declaration to avoid circular dependancies.
19 // since we do not need its size in this header file (only using pointers to it)
20 namespace bpp
21 {
22 class GraphObserver;
23 }
24 
25 namespace bpp
26 {
27 class Graph
28 {
29 public:
30  typedef unsigned int NodeId;
31  typedef unsigned int EdgeId;
32 
33 
34  virtual ~Graph(){}
35 
36 protected:
42  virtual void setRoot(NodeId newRoot) = 0;
43 
44 public:
49  virtual NodeId getRoot() const = 0;
50 
62  virtual void makeDirected() = 0;
63 
74  virtual void makeUndirected() = 0;
75 
76  // /@}
77 
78 
82  // /@{
83 
89  virtual NodeId createNode() = 0;
90 
97  virtual NodeId createNodeFromNode(NodeId origin) = 0;
98 
105  virtual NodeId createNodeOnEdge(NodeId edge) = 0;
106 
107 
114  virtual NodeId createNodeFromEdge(NodeId origin) = 0;
115 
116 protected:
124  virtual EdgeId link(NodeId nodeA, NodeId nodeB) = 0;
125 
134  virtual void link(Graph::NodeId nodeA, Graph::NodeId nodeB, Graph::EdgeId edgeID) = 0;
135 
143  virtual std::vector<EdgeId> unlink(NodeId nodeA, NodeId nodeB) = 0;
144 
145 public:
151  virtual void deleteNode(NodeId node) = 0;
152 
153  // /@}
154 
158  // /@{
159 
165  virtual void registerObserver(GraphObserver* observer) = 0;
166 
172  virtual void unregisterObserver(GraphObserver* observer) = 0;
173 
174 // /@}
175 
179  // /@{
180 
187  {
188 public:
189  virtual ~NodeIterator() {}
190 
191  virtual void next() = 0;
192  virtual bool end() const = 0;
193  virtual void start() = 0;
194 
195  virtual NodeId operator*() = 0;
196  };
197 
203  struct ALLGRAPHITER {};
206 
207  /*
208  * @brief builds iterator on all Nodes of the graph
209  *
210  */
211 
212  virtual std::unique_ptr<NodeIterator> allNodesIterator() = 0;
213 
214  virtual std::unique_ptr<NodeIterator> allNodesIterator() const = 0;
215 
216  /*
217  * @brief builds iterator on the outgoing neighbor nodes of a Node
218  *
219  */
220 
221  virtual std::unique_ptr<NodeIterator> outgoingNeighborNodesIterator(NodeId node) = 0;
222 
223  /*
224  * @brief builds iterator on the incoming neighbor nodes of a Node
225  *
226  */
227 
228  virtual std::unique_ptr<NodeIterator> incomingNeighborNodesIterator(NodeId node) = 0;
229 
230 
235  virtual size_t getNumberOfNodes() const = 0;
236 
241  virtual size_t getNumberOfEdges() const = 0;
242 
249  virtual size_t getDegree(NodeId node) const = 0;
250 
255  virtual bool isLeaf(NodeId node) const = 0;
256 
263  virtual size_t getNumberOfNeighbors(NodeId node) const = 0;
264 
271  virtual size_t getNumberOfOutgoingNeighbors(NodeId node) const = 0;
272 
279  virtual size_t getNumberOfIncomingNeighbors(const NodeId node) const = 0;
280 
287  virtual std::vector<NodeId> getNeighbors(const NodeId node) const = 0;
288 
296  virtual std::vector<NodeId> getOutgoingNeighbors(const NodeId node) const = 0;
297 
305  virtual std::vector<NodeId> getIncomingNeighbors(NodeId node) const = 0;
306 
315  virtual std::vector<NodeId> getLeavesFromNode(NodeId node, unsigned int maxDepth) const = 0;
316 
323  virtual std::vector<NodeId> getAllLeaves() const = 0;
324 
325  virtual std::set<NodeId> getSetOfAllLeaves() const = 0;
326 
332  virtual std::vector<NodeId> getAllInnerNodes() const = 0;
333 
339  virtual std::vector<NodeId> getAllNodes() const = 0;
340 
348  virtual std::pair<NodeId, NodeId> getNodes(EdgeId edge) const = 0;
349 
357  virtual NodeId getTop(EdgeId edge) const = 0;
358 
366  virtual NodeId getBottom(EdgeId edge) const = 0;
367 
368  // /@}
369 
373  // /@{
374 
380  virtual bool isTree() const = 0;
381 
387  virtual bool isDA() const = 0;
388 
389 
394  virtual void orientate() = 0;
395 
401  virtual bool isDirected() const = 0;
402 
403 
409  virtual bool containsReciprocalRelations() const = 0;
410 
411 
412  // /@}
413 
414 
418  // /@{
419 
426  {
427 public:
428  virtual ~EdgeIterator() {}
429 
430  virtual void next() = 0;
431  virtual bool end() const = 0;
432  virtual void start() = 0;
433 
434  virtual EdgeId operator*() = 0;
435  };
436 
437 
438  /*
439  * @brief builds iterator on all Nodes of the graph
440  *
441  */
442 
443  virtual std::unique_ptr<EdgeIterator> allEdgesIterator() = 0;
444 
445  /*
446  * @brief builds iterator on the outgoing neighbor nodes of a Node
447  *
448  */
449 
450  virtual std::unique_ptr<EdgeIterator> outgoingEdgesIterator(NodeId node) = 0;
451 
452  /*
453  * @brief builds iterator on the incoming neighbor nodes of a Node
454  *
455  */
456 
457  virtual std::unique_ptr<EdgeIterator> incomingEdgesIterator(NodeId node) = 0;
458 
465  virtual std::vector<EdgeId> getEdges(const NodeId node) const = 0;
466 
474  virtual std::vector<EdgeId> getOutgoingEdges(const NodeId node) const = 0;
475 
483  virtual std::vector<EdgeId> getIncomingEdges(NodeId node) const = 0;
484 
490  virtual std::vector<EdgeId> getAllEdges() const = 0;
491 
499  virtual EdgeId getEdge(NodeId nodeA, NodeId nodeB) const = 0;
500 
508  virtual EdgeId getAnyEdge(NodeId nodeA, NodeId nodeB) const = 0;
509 
510  // /@}
511 
512 protected:
517  // /@{
518 
523  virtual void notifyDeletedEdges(const std::vector<EdgeId>& edgesToDelete) const = 0;
524 
529  virtual void notifyDeletedNodes(const std::vector<NodeId>& nodesToDelete) const = 0;
530 
531 
532  // /@}
533 
534 public:
541  virtual void outputToDot(std::ostream& out, const std::string& name) const = 0;
542 
543  template<class N, class E, class GraphImpl>
545 };
546 }
547 #endif // BPP_GRAPH_GRAPH_H
virtual std::vector< EdgeId > getOutgoingEdges(const NodeId node) const =0
virtual std::unique_ptr< EdgeIterator > outgoingEdgesIterator(NodeId node)=0
virtual NodeId operator*()=0
virtual std::unique_ptr< NodeIterator > incomingNeighborNodesIterator(NodeId node)=0
virtual std::vector< NodeId > getAllNodes() const =0
virtual std::unique_ptr< NodeIterator > allNodesIterator()=0
virtual size_t getNumberOfEdges() const =0
virtual void notifyDeletedEdges(const std::vector< EdgeId > &edgesToDelete) const =0
virtual ~NodeIterator()
Definition: Graph.h:189
virtual bool isDA() const =0
define categories of iterators
Definition: Graph.h:203
virtual std::pair< NodeId, NodeId > getNodes(EdgeId edge) const =0
virtual void orientate()=0
virtual std::vector< EdgeId > getEdges(const NodeId node) const =0
virtual NodeId getTop(EdgeId edge) const =0
virtual std::unique_ptr< EdgeIterator > allEdgesIterator()=0
unsigned int NodeId
Definition: Graph.h:30
virtual void next()=0
virtual size_t getDegree(NodeId node) const =0
virtual void setRoot(NodeId newRoot)=0
virtual std::vector< NodeId > getNeighbors(const NodeId node) const =0
virtual std::vector< NodeId > getOutgoingNeighbors(const NodeId node) const =0
virtual size_t getNumberOfOutgoingNeighbors(NodeId node) const =0
virtual std::vector< NodeId > getIncomingNeighbors(NodeId node) const =0
virtual bool containsReciprocalRelations() const =0
virtual size_t getNumberOfNodes() const =0
virtual NodeId getBottom(EdgeId edge) const =0
virtual std::unique_ptr< EdgeIterator > incomingEdgesIterator(NodeId node)=0
virtual bool end() const =0
virtual void unregisterObserver(GraphObserver *observer)=0
virtual std::unique_ptr< NodeIterator > outgoingNeighborNodesIterator(NodeId node)=0
virtual NodeId createNode()=0
virtual void makeDirected()=0
virtual EdgeId getEdge(NodeId nodeA, NodeId nodeB) const =0
virtual bool isDirected() const =0
virtual void makeUndirected()=0
virtual NodeId createNodeFromNode(NodeId origin)=0
virtual std::vector< EdgeId > getIncomingEdges(NodeId node) const =0
virtual bool isLeaf(NodeId node) const =0
virtual std::vector< EdgeId > getAllEdges() const =0
virtual std::vector< NodeId > getAllLeaves() const =0
virtual ~EdgeIterator()
Definition: Graph.h:428
virtual void notifyDeletedNodes(const std::vector< NodeId > &nodesToDelete) const =0
virtual EdgeId link(NodeId nodeA, NodeId nodeB)=0
virtual void outputToDot(std::ostream &out, const std::string &name) const =0
virtual void deleteNode(NodeId node)=0
virtual void registerObserver(GraphObserver *observer)=0
virtual bool isTree() const =0
virtual ~Graph()
Definition: Graph.h:34
virtual std::vector< NodeId > getAllInnerNodes() const =0
virtual std::vector< EdgeId > unlink(NodeId nodeA, NodeId nodeB)=0
virtual NodeId createNodeFromEdge(NodeId origin)=0
Defines a Graph Observer. It is a template which follows (subscribed to) a Graph. The graph and the g...
Definition: GraphObserver.h:31
virtual NodeId getRoot() const =0
virtual std::vector< NodeId > getLeavesFromNode(NodeId node, unsigned int maxDepth) const =0
virtual NodeId createNodeOnEdge(NodeId edge)=0
virtual size_t getNumberOfNeighbors(NodeId node) const =0
virtual EdgeId getAnyEdge(NodeId nodeA, NodeId nodeB) const =0
virtual void start()=0
unsigned int EdgeId
Definition: Graph.h:31
virtual size_t getNumberOfIncomingNeighbors(const NodeId node) const =0
virtual std::set< NodeId > getSetOfAllLeaves() const =0