aGrUM  0.14.2
DAG.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005 by Christophe GONZALES and Pierre-Henri WUILLEMIN *
3  * {prenom.nom}_at_lip6.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
26 #include <agrum/graphs/DAG.h>
27 
28 #ifdef GUM_NO_INLINE
29 # include <agrum/graphs/DAG_inl.h>
30 #endif // GUM_NOINLINE
31 
32 namespace gum {
33 
34  // diamond structure require to explicitly initialize NodeGraphPart
35  DAG::DAG(Size nodes_size,
36  bool nodes_resize_policy,
37  Size arcs_size,
38  bool arcs_resize_policy) :
39  NodeGraphPart(nodes_size, nodes_resize_policy),
40  DiGraph(nodes_size, nodes_resize_policy, arcs_size, arcs_resize_policy) {
41  GUM_CONSTRUCTOR(DAG);
42  }
43 
44  // diamond structure require to explicitly initialize NodeGraphPart
45  DAG::DAG(const DAG& g) : NodeGraphPart(g), DiGraph(g) { GUM_CONS_CPY(DAG); }
46 
47  DAG::~DAG() { GUM_DESTRUCTOR(DAG); }
48 
54  bool DAG::__hasDirectedPath(const NodeId from, const NodeId to) {
55  if (!exists(from)) return false;
56 
57  if (from == to) return true;
58 
59  // not recursive version => use a FIFO for simulating the recursion
60  List< NodeId > nodeFIFO;
61  nodeFIFO.pushBack(from);
62 
63  NodeSet marked;
64  marked.insert(from);
65 
66  NodeId new_one;
67 
68  while (!nodeFIFO.empty()) {
69  new_one = nodeFIFO.front();
70  // std::cout<<new_one<<std::endl;
71  nodeFIFO.popFront();
72 
73  for (const auto chi : children(new_one)) {
74  if (chi == to) return true;
75 
76  if (!marked.contains(chi)) {
77  nodeFIFO.pushBack(chi);
78  marked.insert(chi);
79  }
80  }
81  }
82 
83  return false;
84  }
85 
87  UndiGraph moralgraph;
88  moralgraph.populateNodes(*this);
89  // transform the arcs into edges
90  for (const auto arc : arcs())
91  moralgraph.addEdge(arc.first(), arc.second());
92 
93  // marry the parents
94  for (const auto node : nodes()) {
95  const auto& par = parents(node);
96 
97  for (auto it1 = par.begin(); it1 != par.end(); ++it1) {
98  auto it2 = it1;
99 
100  for (++it2; it2 != par.end(); ++it2) {
101  // will automatically check if this edge already exists
102  moralgraph.addEdge(*it1, *it2);
103  }
104  }
105  }
106  return moralgraph;
107  }
108 
109 } /* namespace gum */
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Definition: set_tpl.h:578
bool empty() const noexcept
Returns a boolean indicating whether the chained list is empty.
Definition: list_tpl.h:1967
bool __hasDirectedPath(const NodeId from, const NodeId to)
checks whether there exists a directed path from from to to
Definition: DAG.cpp:54
virtual void addEdge(const NodeId first, const NodeId second)
insert a new edge into the undirected graph
Definition: undiGraph_inl.h:32
UndiGraph moralGraph() const
Definition: DAG.cpp:86
Inline implementation of Base classes for directed acylic graphs.
bool exists(const NodeId id) const
alias for existsNode
Generic doubly linked lists.
Definition: list.h:369
gum is the global namespace for all aGrUM entities
Definition: agrum.h:25
void popFront()
Removes the first element of a List, if any.
Definition: list_tpl.h:1961
virtual ~DAG()
destructor
Definition: DAG.cpp:47
DAG(Size nodes_size=HashTableConst::default_size, bool nodes_resize_policy=true, Size arcs_size=HashTableConst::default_size, bool arcs_resize_policy=true)
default constructor
Definition: DAG.cpp:35
const NodeSet & parents(const NodeId id) const
returns the set of nodes with arc ingoing to a given node
Val & pushBack(const Val &val)
Inserts a new element (a copy) at the end of the chained list.
Definition: list_tpl.h:1589
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
Base class for all oriented graphs.
Definition: diGraph.h:108
Class for node sets in graph.
Val & front() const
Returns a reference to first element of a list, if any.
Definition: list_tpl.h:1828
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart
Base class for undirected graphs.
Definition: undiGraph.h:106
void populateNodes(const NodeGraphPart &s)
populateNodes clears *this and fills it with the same nodes as "s"
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
Base class for dag.
Definition: DAG.h:99
Size NodeId
Type for node ids.
Definition: graphElements.h:97
void insert(const Key &k)
Inserts a new element into the set.
Definition: set_tpl.h:610
Base classes for directed acyclic graphs.