bpp-core3  3.0.0
Graph.h
Go to the documentation of this file.
1 //
2 // File: Graph.h
3 // Authors:
4 // Thomas Bigot
5 // Last modified: vendredi 4 novembre 2016, à 10h 21
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide utilitary
12  classes. This file belongs to the Bio++ Project.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_GRAPH_GRAPH_H
42 #define BPP_GRAPH_GRAPH_H
43 
44 #include <algorithm>
45 #include <iostream>
46 #include <map>
47 #include <memory>
48 #include <set>
49 #include <string>
50 #include <utility>
51 #include <vector>
52 
53 
54 // forward declaration to avoid circular dependancies.
55 // since we do not need its size in this header file (only using pointers to it)
56 namespace bpp
57 {
58 class GraphObserver;
59 }
60 
61 namespace bpp
62 {
63 class Graph
64 {
65 public:
66  typedef unsigned int NodeId;
67  typedef unsigned int EdgeId;
68 
69 
70  virtual ~Graph(){}
71 
72 protected:
78  virtual void setRoot(NodeId newRoot) = 0;
79 
80 public:
85  virtual NodeId getRoot() const = 0;
86 
98  virtual void makeDirected() = 0;
99 
110  virtual void makeUndirected() = 0;
111 
112  // /@}
113 
114 
118  // /@{
119 
125  virtual NodeId createNode() = 0;
126 
133  virtual NodeId createNodeFromNode(NodeId origin) = 0;
134 
141  virtual NodeId createNodeOnEdge(NodeId edge) = 0;
142 
143 
150  virtual NodeId createNodeFromEdge(NodeId origin) = 0;
151 
152 protected:
160  virtual EdgeId link(NodeId nodeA, NodeId nodeB) = 0;
161 
170  virtual void link(Graph::NodeId nodeA, Graph::NodeId nodeB, Graph::EdgeId edgeID) = 0;
171 
179  virtual std::vector<EdgeId> unlink(NodeId nodeA, NodeId nodeB) = 0;
180 
181 public:
187  virtual void deleteNode(NodeId node) = 0;
188 
189  // /@}
190 
194  // /@{
195 
201  virtual void registerObserver(GraphObserver* observer) = 0;
202 
208  virtual void unregisterObserver(GraphObserver* observer) = 0;
209 
210 // /@}
211 
215  // /@{
216 
223  {
224 public:
225  virtual ~NodeIterator() {}
226 
227  virtual void next() = 0;
228  virtual bool end() const = 0;
229  virtual void start() = 0;
230 
231  virtual NodeId operator*() = 0;
232  };
233 
239  struct ALLGRAPHITER {};
242 
243  /*
244  * @brief builds iterator on all Nodes of the graph
245  *
246  */
247 
248  virtual std::unique_ptr<NodeIterator> allNodesIterator() = 0;
249 
250  virtual std::unique_ptr<NodeIterator> allNodesIterator() const = 0;
251 
252  /*
253  * @brief builds iterator on the outgoing neighbor nodes of a Node
254  *
255  */
256 
257  virtual std::unique_ptr<NodeIterator> outgoingNeighborNodesIterator(NodeId node) = 0;
258 
259  /*
260  * @brief builds iterator on the incoming neighbor nodes of a Node
261  *
262  */
263 
264  virtual std::unique_ptr<NodeIterator> incomingNeighborNodesIterator(NodeId node) = 0;
265 
266 
271  virtual size_t getNumberOfNodes() const = 0;
272 
277  virtual size_t getNumberOfEdges() const = 0;
278 
285  virtual size_t getDegree(NodeId node) const = 0;
286 
291  virtual bool isLeaf(NodeId node) const = 0;
292 
299  virtual size_t getNumberOfNeighbors(NodeId node) const = 0;
300 
307  virtual size_t getNumberOfOutgoingNeighbors(NodeId node) const = 0;
308 
315  virtual size_t getNumberOfIncomingNeighbors(const NodeId node) const = 0;
316 
323  virtual std::vector<NodeId> getNeighbors(const NodeId node) const = 0;
324 
332  virtual std::vector<NodeId> getOutgoingNeighbors(const NodeId node) const = 0;
333 
341  virtual std::vector<NodeId> getIncomingNeighbors(NodeId node) const = 0;
342 
351  virtual std::vector<NodeId> getLeavesFromNode(NodeId node, unsigned int maxDepth) const = 0;
352 
359  virtual std::vector<NodeId> getAllLeaves() const = 0;
360 
361  virtual std::set<NodeId> getSetOfAllLeaves() const = 0;
362 
368  virtual std::vector<NodeId> getAllInnerNodes() const = 0;
369 
375  virtual std::vector<NodeId> getAllNodes() const = 0;
376 
384  virtual std::pair<NodeId, NodeId> getNodes(EdgeId edge) const = 0;
385 
393  virtual NodeId getTop(EdgeId edge) const = 0;
394 
402  virtual NodeId getBottom(EdgeId edge) const = 0;
403 
404  // /@}
405 
409  // /@{
410 
416  virtual bool isTree() const = 0;
417 
423  virtual bool isDA() const = 0;
424 
425 
430  virtual void orientate() = 0;
431 
437  virtual bool isDirected() const = 0;
438 
439 
445  virtual bool containsReciprocalRelations() const = 0;
446 
447 
448  // /@}
449 
450 
454  // /@{
455 
462  {
463 public:
464  virtual ~EdgeIterator() {}
465 
466  virtual void next() = 0;
467  virtual bool end() const = 0;
468  virtual void start() = 0;
469 
470  virtual EdgeId operator*() = 0;
471  };
472 
473 
474  /*
475  * @brief builds iterator on all Nodes of the graph
476  *
477  */
478 
479  virtual std::unique_ptr<EdgeIterator> allEdgesIterator() = 0;
480 
481  /*
482  * @brief builds iterator on the outgoing neighbor nodes of a Node
483  *
484  */
485 
486  virtual std::unique_ptr<EdgeIterator> outgoingEdgesIterator(NodeId node) = 0;
487 
488  /*
489  * @brief builds iterator on the incoming neighbor nodes of a Node
490  *
491  */
492 
493  virtual std::unique_ptr<EdgeIterator> incomingEdgesIterator(NodeId node) = 0;
494 
501  virtual std::vector<EdgeId> getEdges(const NodeId node) const = 0;
502 
510  virtual std::vector<EdgeId> getOutgoingEdges(const NodeId node) const = 0;
511 
519  virtual std::vector<EdgeId> getIncomingEdges(NodeId node) const = 0;
520 
526  virtual std::vector<EdgeId> getAllEdges() const = 0;
527 
535  virtual EdgeId getEdge(NodeId nodeA, NodeId nodeB) const = 0;
536 
544  virtual EdgeId getAnyEdge(NodeId nodeA, NodeId nodeB) const = 0;
545 
546  // /@}
547 
548 protected:
553  // /@{
554 
559  virtual void notifyDeletedEdges(const std::vector<EdgeId>& edgesToDelete) const = 0;
560 
565  virtual void notifyDeletedNodes(const std::vector<NodeId>& nodesToDelete) const = 0;
566 
567 
568  // /@}
569 
570 public:
577  virtual void outputToDot(std::ostream& out, const std::string& name) const = 0;
578 
579  template<class N, class E, class GraphImpl>
581 };
582 }
583 #endif // BPP_GRAPH_GRAPH_H
Defines a Graph Observer. It is a template which follows (subscribed to) a Graph. The graph and the g...
Definition: GraphObserver.h:69
virtual void next()=0
virtual ~EdgeIterator()
Definition: Graph.h:464
virtual EdgeId operator*()=0
virtual void start()=0
virtual bool end() const =0
virtual void next()=0
virtual void start()=0
virtual ~NodeIterator()
Definition: Graph.h:225
virtual bool end() const =0
virtual NodeId operator*()=0
unsigned int NodeId
Definition: Graph.h:66
virtual EdgeId getAnyEdge(NodeId nodeA, NodeId nodeB) const =0
virtual void outputToDot(std::ostream &out, const std::string &name) const =0
virtual size_t getNumberOfIncomingNeighbors(const NodeId node) const =0
virtual std::unique_ptr< EdgeIterator > allEdgesIterator()=0
unsigned int EdgeId
Definition: Graph.h:67
virtual std::vector< EdgeId > getEdges(const NodeId node) const =0
virtual size_t getNumberOfNeighbors(NodeId node) const =0
virtual std::unique_ptr< NodeIterator > outgoingNeighborNodesIterator(NodeId node)=0
virtual std::unique_ptr< EdgeIterator > incomingEdgesIterator(NodeId node)=0
virtual std::vector< NodeId > getNeighbors(const NodeId node) const =0
virtual EdgeId getEdge(NodeId nodeA, NodeId nodeB) const =0
virtual std::vector< NodeId > getAllInnerNodes() const =0
virtual void setRoot(NodeId newRoot)=0
virtual void deleteNode(NodeId node)=0
virtual std::unique_ptr< NodeIterator > incomingNeighborNodesIterator(NodeId node)=0
virtual std::vector< EdgeId > getAllEdges() const =0
virtual ~Graph()
Definition: Graph.h:70
virtual std::unique_ptr< NodeIterator > allNodesIterator() const =0
virtual bool containsReciprocalRelations() const =0
virtual std::vector< NodeId > getOutgoingNeighbors(const NodeId node) const =0
virtual void unregisterObserver(GraphObserver *observer)=0
virtual void orientate()=0
virtual size_t getNumberOfEdges() const =0
virtual void makeUndirected()=0
virtual NodeId getTop(EdgeId edge) const =0
virtual std::vector< NodeId > getAllNodes() const =0
virtual std::unique_ptr< NodeIterator > allNodesIterator()=0
virtual void notifyDeletedEdges(const std::vector< EdgeId > &edgesToDelete) const =0
virtual std::vector< EdgeId > getOutgoingEdges(const NodeId node) const =0
virtual bool isLeaf(NodeId node) const =0
virtual bool isDA() const =0
virtual EdgeId link(NodeId nodeA, NodeId nodeB)=0
virtual void notifyDeletedNodes(const std::vector< NodeId > &nodesToDelete) const =0
virtual std::vector< NodeId > getIncomingNeighbors(NodeId node) const =0
virtual std::vector< NodeId > getLeavesFromNode(NodeId node, unsigned int maxDepth) const =0
virtual void makeDirected()=0
virtual size_t getDegree(NodeId node) const =0
virtual std::set< NodeId > getSetOfAllLeaves() const =0
virtual NodeId createNodeFromEdge(NodeId origin)=0
virtual NodeId getBottom(EdgeId edge) const =0
virtual NodeId createNodeFromNode(NodeId origin)=0
virtual std::vector< EdgeId > getIncomingEdges(NodeId node) const =0
virtual std::unique_ptr< EdgeIterator > outgoingEdgesIterator(NodeId node)=0
virtual size_t getNumberOfNodes() const =0
virtual std::vector< EdgeId > unlink(NodeId nodeA, NodeId nodeB)=0
virtual bool isTree() const =0
virtual NodeId createNode()=0
virtual NodeId getRoot() const =0
virtual std::pair< NodeId, NodeId > getNodes(EdgeId edge) const =0
virtual bool isDirected() const =0
virtual void link(Graph::NodeId nodeA, Graph::NodeId nodeB, Graph::EdgeId edgeID)=0
virtual std::vector< NodeId > getAllLeaves() const =0
virtual NodeId createNodeOnEdge(NodeId edge)=0
virtual void registerObserver(GraphObserver *observer)=0
virtual size_t getNumberOfOutgoingNeighbors(NodeId node) const =0
define categories of iterators
Definition: Graph.h:239