aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
mixedGraph.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /** @file
23  * @brief Base classes for mixed directed/undirected graphs
24  *
25  * @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26  */
27 #ifndef GUM_MIXED_GRAPH_H
28 #define GUM_MIXED_GRAPH_H
29 
30 #include <iostream>
31 #include <utility>
32 
33 #include <agrum/agrum.h>
34 
35 #include <agrum/tools/graphs/diGraph.h>
36 #include <agrum/tools/graphs/undiGraph.h>
37 
38 namespace gum {
39 
40  /* ===========================================================================
41  */
42  /* === BASE CLASS FOR MANIPULATING GRAPHS WITH BOTH EDGES AND ARCS ===
43  */
44  /* ===========================================================================
45  */
46  /** @class MixedGraph
47  * @brief Base class for mixed graphs
48  *
49  * \ingroup graph_group
50  *
51  *
52  * @par Usage example:
53  * @code
54  * // creating empty graphs
55  * MixedGraph g1,g2;
56  *
57  * // adding nodes, arcs and edges to g1
58  * NodeId i1=g1.addNode();
59  * NodeId i2=g1.addNode();
60  * NodeId i3=g1.addNode();
61  * g1.addArc( i1,i2 );
62  * g1.addArc( i1,i3 );
63  * g1.addArc( i2,i3 );
64  * g1.addEdge( i1,i2 );
65  * g1.addEdge( i1,i3 );
66  * g1.addEdge( i2,i3 );
67  *
68  * //throw an InvalidNode
69  * // g1.addArc( i1+i2+i3,i1 );
70  * // g1.addEdge( i1+i2+i3,i1 );
71  *
72  * // copying graphs
73  * MixedGraph g3 = g1;
74  * g2 = g1;
75  * MixedGraph g4=g1;
76  *
77  * // check if a graph has no node
78  * if ( g1.empty() ) cerr << "graph g1 is empty" << endl;
79  *
80  * // remove all the nodes (as well as their adjacent arcs and edges)
81  * g1.clear();
82  *
83  * // remove some arc
84  * g4.eraseArc( Arc ( i1,i3 ) );
85  *
86  * // remove some edge
87  * g4.eraseEdge( Edge ( i1,i3 ) );
88  *
89  * // remove node
90  * g2.eraseNode( i2 );
91  *
92  * // parse a graph
93  * for ( const auto nod : g3.nodes() )
94  * cerr << nod << endl;
95  *
96  * for ( const auto arc : g3.arcs() )
97  * cerr << arc << endl;
98  *
99  * for ( const auto edg : g3.edges()) )
100  * cerr << edg << endl;
101  *
102  * const NodeSet& a=g3.parents( 3 );
103  *
104  * for ( const auto iter : a)
105  * cerr << " - "<<iter;
106  *
107  * cerr<<endl;
108  *
109  * const NodeSet& a=g3.neighbours( 3 );
110  *
111  * for ( NodeSetIterator iter = a.begin( ); iter != a.end(); ++iter )
112  * cerr << " - "<<*iter;
113  *
114  * cerr<<endl;
115  *
116  * // remove all the arcs that are parent of a given node
117  * g3.eraseParents( 2 );
118  *
119  * // remove all the edges that are adjacent to a given node
120  * g3.eraseNeighbours( 2 );
121  * @endcode
122  */
123  /* ===========================================================================
124  */
125 
126  class MixedGraph: public virtual UndiGraph, public virtual DiGraph {
127  public:
128  // ############################################################################
129  /// @name Constructors / Destructors
130  // ############################################################################
131  /// @{
132 
133  /// default constructor
134  /** @param nodes_size the size of the hash table used to store all the nodes
135  * @param nodes_resize_policy the resizing policy of this hash table
136  * @param arcs_size the size of the hash table used to store all the arcs
137  * @param arcs_resize_policy the resizing policy of this hash table
138  * @param edges_size the size of the hash table used to store all the edges
139  * @param edges_resize_policy the resizing policy of this hash table */
140  explicit MixedGraph(Size nodes_size = HashTableConst::default_size,
141  bool nodes_resize_policy = true,
142  Size arcs_size = HashTableConst::default_size,
143  bool arcs_resize_policy = true,
144  Size edges_size = HashTableConst::default_size,
145  bool edges_resize_policy = true);
146 
147  explicit MixedGraph(const UndiGraph& g);
148 
149  explicit MixedGraph(const DiGraph& g);
150 
151  /// copy constructor
152  /** @param g the MixedGraph to copy */
153  MixedGraph(const MixedGraph& g);
154 
155  /// destructor
156  virtual ~MixedGraph();
157 
158  /// @}
159 
160  // ############################################################################
161  /// @name Operators
162  // ############################################################################
163  /// @{
164 
165  /// copy operator
166  /** @param g the MixedGraph to copy */
167  MixedGraph& operator=(const MixedGraph& g);
168 
169  /// tests whether two MixedGraphs are identical (same nodes, arcs and edges)
170  /** @param g the MixedGraph with which "this" is compared */
171  // not virtual : it is a feature !!! :)
172  bool operator==(const MixedGraph& g) const;
173 
174  /// tests whether two MixedGraphs are different
175  /** @param g the MixedGraph with which "this" is compared */
176  // not virtual : it is a feature !!! :)
177  bool operator!=(const MixedGraph& g) const;
178 
179  /// @}
180 
181  // ############################################################################
182  /// @name Accessors/Modifiers
183  // ############################################################################
184  /// @{
185 
186  /// remove a node as well as its adjacent arcs and edges from the graph
187  /** @param id the id of the node to be removed
188  * @warning if the node does not exist, nothing is done. In particular, no
189  * exception is raised.*/
190  virtual void eraseNode(const NodeId id);
191 
192  /// removes all the nodes, arcs and edges from the graph
193  virtual void clear();
194 
195  /** @brief returns a mixed edge/directed arc path from node1 to node2 in the
196  * arc/edge set
197  *
198  * This function returns, if any, a path from node1 to node2, using edges
199  * and/or arcs (wrt the direction of th arcs)
200  * @param node1 the id from which the path begins
201  * @param node2 the id to which the path ends
202  * if no path can be found between the two nodes, the returned vector is empty*/
203  std::vector< NodeId > mixedOrientedPath(NodeId node1, NodeId node2) const;
204 
205  /// returns a mixed/directed path from node1 to node2 in the arc/edge set
206  /** This function returns, if any, a path from node1 to node2, using edges
207  * and/or arcs (not necessarily following the direction of th arcs)
208  * @param node1 the id from which the path begins
209  * @param node2 the id to which the path ends
210  * if no path can be found between the two nodes, the returned vector is empty. */
211  std::vector< NodeId > mixedUnorientedPath(NodeId node1, NodeId node2) const;
212 
213  /// to friendly display mixed graph in DOT format
214  virtual std::string toDot() const;
215 
216  /// to friendly display the content of the MixedGraph
217  virtual std::string toString() const;
218 
219  /// returns the set of node adjacent to a given node
220  /** Note that the set of node returned may be empty.
221  * @param id the node to which the edges are adjacent */
222  NodeSet adjacents(const NodeId id) const;
223  /// @}
224  };
225 
226  /// for friendly displaying the content of directed graphs
227  std::ostream& operator<<(std::ostream&, const MixedGraph&);
228 
229 } /* namespace gum */
230 
231 #ifndef GUM_NO_INLINE
232 # include <agrum/tools/graphs/mixedGraph_inl.h>
233 #endif // GUM_NOINLINE
234 
235 #endif /* GUM_MIXEDGRAPH_H */