aGrUM
0.20.2
a C++ library for (probabilistic) graphical models
mixedGraph.cpp
Go to the documentation of this file.
1
/**
2
*
3
* Copyright 2005-2020 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 Source implementation of Base classes for undirected graphs
24
*
25
* @author Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
26
*
27
*/
28
#
include
<
agrum
/
tools
/
graphs
/
mixedGraph
.
h
>
29
30
#
ifdef
GUM_NO_INLINE
31
#
include
<
agrum
/
tools
/
graphs
/
mixedGraph_inl
.
h
>
32
#
endif
// GUM_NOINLINE
33
34
namespace
gum
{
35
36
MixedGraph
::
MixedGraph
(
Size
nodes_size
,
37
bool
nodes_resize_policy
,
38
Size
arcs_size
,
39
bool
arcs_resize_policy
,
40
Size
edges_size
,
41
bool
edges_resize_policy
) :
42
// Note that we need to initialize the NodeGraphPart by ourselves
43
// because
44
// it is a virtual inherited class (see C++ FAQ Lite #25.12 for details)
45
NodeGraphPart
(
nodes_size
,
nodes_resize_policy
),
46
UndiGraph
(
edges_size
,
edges_resize_policy
),
47
DiGraph
(
arcs_size
,
arcs_resize_policy
) {
48
// for debugging purposes
49
GUM_CONSTRUCTOR
(
MixedGraph
);
50
}
51
52
MixedGraph
::
MixedGraph
(
const
UndiGraph
&
g
) :
53
NodeGraphPart
(
g
),
UndiGraph
(
g
),
DiGraph
() {
54
GUM_CONSTRUCTOR
(
MixedGraph
);
55
}
56
57
MixedGraph
::
MixedGraph
(
const
DiGraph
&
g
) :
58
NodeGraphPart
(
g
),
UndiGraph
(),
DiGraph
(
g
) {
59
GUM_CONSTRUCTOR
(
MixedGraph
);
60
}
61
62
MixedGraph
::
MixedGraph
(
const
MixedGraph
&
g
) :
63
NodeGraphPart
(
g
),
UndiGraph
(
g
),
DiGraph
(
g
) {
64
// for debugging purposes
65
GUM_CONS_CPY
(
MixedGraph
);
66
}
67
68
MixedGraph
::~
MixedGraph
() {
69
// for debugging purposes
70
GUM_DESTRUCTOR
(
MixedGraph
);
71
}
72
73
std
::
string
MixedGraph
::
toString
()
const
{
74
std
::
string
s
=
NodeGraphPart
::
toString
();
75
s
+=
" , "
;
76
s
+=
ArcGraphPart
::
toString
();
77
s
+=
" , "
;
78
s
+=
EdgeGraphPart
::
toString
();
79
return
s
;
80
}
81
82
const
std
::
vector
<
NodeId
>
83
MixedGraph
::
mixedOrientedPath
(
const
NodeId
n1
,
const
NodeId
n2
)
const
{
84
// not recursive version => use a FIFO for simulating the recursion
85
List
<
NodeId
>
nodeFIFO
;
86
nodeFIFO
.
pushBack
(
n2
);
87
88
// mark[node] = successor if visited, else mark[node] does not exist
89
NodeProperty
<
NodeId
>
mark
;
90
mark
.
insert
(
n2
,
n2
);
91
92
NodeId
current
;
93
94
while
(!
nodeFIFO
.
empty
()) {
95
current
=
nodeFIFO
.
front
();
96
nodeFIFO
.
popFront
();
97
98
// check the neighbours
99
for
(
const
auto
new_one
:
neighbours
(
current
)) {
100
if
(
mark
.
exists
(
new_one
))
// if the node has already been visited
101
continue
;
// do not check it again
102
103
mark
.
insert
(
new_one
,
current
);
104
105
if
(
new_one
==
n1
) {
106
std
::
vector
<
NodeId
>
v
;
107
108
for
(
current
=
n1
;
current
!=
n2
;
current
=
mark
[
current
])
109
v
.
push_back
(
current
);
110
111
v
.
push_back
(
n2
);
112
113
return
v
;
114
}
115
116
nodeFIFO
.
pushBack
(
new_one
);
117
}
118
119
// check the parents
120
for
(
const
auto
new_one
:
parents
(
current
)) {
121
if
(
mark
.
exists
(
new_one
))
// if this node is already marked, do not
122
continue
;
// check it again
123
124
mark
.
insert
(
new_one
,
current
);
125
126
if
(
new_one
==
n1
) {
127
std
::
vector
<
NodeId
>
v
;
128
129
for
(
current
=
n1
;
current
!=
n2
;
current
=
mark
[
current
])
130
v
.
push_back
(
current
);
131
132
v
.
push_back
(
n2
);
133
134
return
v
;
135
}
136
137
nodeFIFO
.
pushBack
(
new_one
);
138
}
139
}
140
141
GUM_ERROR
(
NotFound
,
"no path found"
);
142
}
143
144
const
std
::
vector
<
NodeId
>
145
MixedGraph
::
mixedUnorientedPath
(
const
NodeId
n1
,
const
NodeId
n2
)
const
{
146
// not recursive version => use a FIFO for simulating the recursion
147
List
<
NodeId
>
nodeFIFO
;
148
nodeFIFO
.
pushBack
(
n2
);
149
150
// mark[node] = successor if visited, else mark[node] does not exist
151
NodeProperty
<
NodeId
>
mark
;
152
mark
.
insert
(
n2
,
n2
);
153
154
NodeId
current
;
155
156
while
(!
nodeFIFO
.
empty
()) {
157
current
=
nodeFIFO
.
front
();
158
nodeFIFO
.
popFront
();
159
160
// check the neighbours
161
for
(
const
auto
new_one
:
neighbours
(
current
)) {
162
if
(
mark
.
exists
(
new_one
))
// if the node has already been visited
163
continue
;
// do not check it again
164
165
mark
.
insert
(
new_one
,
current
);
166
167
if
(
new_one
==
n1
) {
168
std
::
vector
<
NodeId
>
v
;
169
170
for
(
current
=
n1
;
current
!=
n2
;
current
=
mark
[
current
])
171
v
.
push_back
(
current
);
172
173
v
.
push_back
(
n2
);
174
175
return
v
;
176
}
177
178
nodeFIFO
.
pushBack
(
new_one
);
179
}
180
181
// check the parents
182
for
(
const
auto
new_one
:
parents
(
current
)) {
183
if
(
mark
.
exists
(
new_one
))
// the node has already been visited
184
continue
;
185
186
mark
.
insert
(
new_one
,
current
);
187
188
if
(
new_one
==
n1
) {
189
std
::
vector
<
NodeId
>
v
;
190
191
for
(
current
=
n1
;
current
!=
n2
;
current
=
mark
[
current
])
192
v
.
push_back
(
current
);
193
194
v
.
push_back
(
n2
);
195
196
return
v
;
197
}
198
199
nodeFIFO
.
pushBack
(
new_one
);
200
}
201
202
// check the children
203
for
(
const
auto
new_one
:
children
(
current
)) {
204
if
(
mark
.
exists
(
new_one
))
// the node has already been visited
205
continue
;
206
207
mark
.
insert
(
new_one
,
current
);
208
209
if
(
new_one
==
n1
) {
210
std
::
vector
<
NodeId
>
v
;
211
212
for
(
current
=
n1
;
current
!=
n2
;
current
=
mark
[
current
])
213
v
.
push_back
(
current
);
214
215
v
.
push_back
(
n2
);
216
return
v
;
217
}
218
219
nodeFIFO
.
pushBack
(
new_one
);
220
}
221
}
222
223
GUM_ERROR
(
NotFound
,
"no path found"
);
224
}
225
226
std
::
string
MixedGraph
::
toDot
()
const
{
227
std
::
stringstream
output
;
228
std
::
stringstream
nodeStream
;
229
std
::
stringstream
edgeStream
;
230
List
<
NodeId
>
treatedNodes
;
231
output
<<
"digraph \""
232
<<
"no_name\" {"
<<
std
::
endl
;
233
nodeStream
<<
"node [shape = ellipse];"
<<
std
::
endl
;
234
std
::
string
tab
=
" "
;
235
236
for
(
const
auto
node
:
nodes
()) {
237
nodeStream
<<
tab
<<
node
<<
";"
;
238
239
for
(
const
auto
nei
:
neighbours
(
node
))
240
if
(!
treatedNodes
.
exists
(
nei
))
241
edgeStream
<<
tab
<<
node
<<
" -> "
<<
nei
<<
" [dir=none];"
242
<<
std
::
endl
;
243
244
for
(
const
auto
chi
:
children
(
node
))
245
edgeStream
<<
tab
<<
node
<<
" -> "
<<
chi
<<
";"
<<
std
::
endl
;
246
247
treatedNodes
.
insert
(
node
);
248
}
249
250
output
<<
nodeStream
.
str
() <<
std
::
endl
251
<<
edgeStream
.
str
() <<
std
::
endl
252
<<
"}"
<<
std
::
endl
;
253
254
return
output
.
str
();
255
}
256
257
/// for friendly displaying the content of directed graphs
258
std
::
ostream
&
operator
<<(
std
::
ostream
&
stream
,
const
MixedGraph
&
g
) {
259
stream << g.toString();
260
return
stream;
261
}
262
263
}
/* namespace gum */
gum::Set::emplace
INLINE void emplace(Args &&... args)
Definition:
set_tpl.h:669
gum::operator<<
std::ostream & operator<<(std::ostream &aStream, const Instantiation &i)
Print information of the instantiation in the stream.
Definition:
instantiation.cpp:265