49 inline void _marryParents_(
const NodeSet& parents, UndiGraph& g) {
50 for (
auto it1 = parents.begin(); it1 != parents.end(); ++it1) {
52 for (++it2; it2 != parents.end(); ++it2)
53 g.addEdge(*it1, *it2);
57 template <
typename DEST_GRAPH,
typename SRC_GRAPH >
58 inline void _copyNodeWithName_(DEST_GRAPH& dest,
const SRC_GRAPH& src,
NodeId id) {
59 dest.addNodeWithId(
id);
60 if (src.hasName(
id)) dest.setName(
id, src.nameFromId(
id));
65 template < GUM_DiGraphable G >
68 for (
const auto node: g.nodes()) {
69 _copyNodeWithName_(moral, g, node);
73 for (
const auto node: g.nodes()) {
74 for (
const auto p: g.parents(node))
76 for (
const auto n: g.neighbours(node))
81 for (
const auto node: g.nodes()) {
82 if (already.
contains(node))
continue;
86 NodeSet frontier = g.neighbours(node);
87 while (!frontier.
empty()) {
91 par += g.parents(nei);
92 frontier += g.neighbours(nei) - already;
95 _marryParents_(par, moral);
98 for (
const auto node: g.nodes()) {
99 const auto& par = g.parents(node);
100 for (
const auto p: par)
102 _marryParents_(par, moral);
108 template < GUM_DiGraphable G >
113 for (
const auto n: query) {
114 _copyNodeWithName_(ancestral, g, n);
117 while (!frontier.
empty()) {
119 frontier.
erase(current);
121 for (
const auto p: g.parents(current)) {
123 _copyNodeWithName_(ancestral, g, p);
126 ancestral.
addArc(p, current);
128 for (
const auto n: g.neighbours(current)) {
130 _copyNodeWithName_(ancestral, g, n);
142 while (!frontier.
empty()) {
144 frontier.
erase(current);
145 _copyNodeWithName_(res, g, current);
146 for (
const auto p: g.parents(current))
150 for (
const auto node: res.
nodes()) {
151 const auto& par = g.parents(node);
152 for (
const auto p: par)
154 _marryParents_(par, res);
void addArc(const NodeId tail, const NodeId head) override
insert a new arc into the directed graph
Base class for mixed graphs.
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart
bool existsNode(const NodeId id) const
returns true iff the NodeGraphPart contains the given nodeId
bool contains(const Key &k) const
Indicates whether a given elements belong to the set.
Key popFirst()
Removes and returns an arbitrary element from the set.
bool empty() const noexcept
Indicates whether the set is the empty set.
iterator begin() const
The usual unsafe begin iterator to parse the set.
void insert(const Key &k)
Inserts a new element into the set.
void erase(const Key &k)
Erases an element from the set.
Base class for undirected graphs.
void addEdge(NodeId first, NodeId second) override
insert a new edge into the undirected graph
Concept for mixed graphs (both arcs and edges).
Size NodeId
Type for node ids.
Set< NodeId > NodeSet
Some typdefs and define for shortcuts ...
Moralization algorithms for aGrUM directed and mixed graphs.
UndiGraph moralizedAncestralGraph(const G &g, const NodeSet &query)
Returns the moralized ancestral graph of query in g.
UndiGraph moralGraph(const G &g)
Returns the moral graph of g.