1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2000-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(digraph_utils_SUITE).
%-define(debug, true).
-ifdef(debug).
-define(line, put(line, ?LINE), ).
-else.
-include_lib("test_server/include/test_server.hrl").
-endif.
-export([all/0, suite/0,groups/0,init_per_group/2,end_per_group/2]).
-export([simple/1, loop/1, isolated/1, topsort/1, subgraph/1,
condensation/1, tree/1]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
suite() -> [{suite_callbacks,[ts_install_scb]}].
all() ->
[simple, loop, isolated, topsort, subgraph,
condensation, tree].
groups() ->
[].
init_per_group(_GroupName, Config) ->
Config.
end_per_group(_GroupName, Config) ->
Config.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
simple(doc) -> [];
simple(suite) -> [];
simple(Config) when is_list(Config) ->
?line G = digraph:new(),
?line add_vertices(G, [a]),
?line add_edges(G, [{b,c},{b,d},{e,f},{f,g},{g,e},{h,h},{i,i},{i,j}]),
?line 10 = length(digraph_utils:postorder(G)),
?line 10 = length(digraph_utils:preorder(G)),
?line ok = evall(digraph_utils:components(G),
[[a],[b,c,d],[e,f,g],[h],[i,j]]),
?line ok = evall(digraph_utils:strong_components(G),
[[a],[b],[c],[d],[e,f,g],[h],[i],[j]]),
?line ok = evall(digraph_utils:cyclic_strong_components(G),
[[e,f,g],[h],[i]]),
?line true = path(G, e, e),
?line false = path(G, e, j),
?line false = path(G, a, a),
?line false = digraph_utils:topsort(G),
?line false = digraph_utils:is_acyclic(G),
?line ok = eval(digraph_utils:loop_vertices(G), [h,i]),
?line ok = eval(digraph_utils:reaching([e], G), [e,f,g]),
?line ok = eval(digraph_utils:reaching_neighbours([e], G), [e,f,g]),
?line ok = eval(digraph_utils:reachable([e], G), [e,f,g]),
?line ok = eval(digraph_utils:reachable_neighbours([e], G), [e,f,g]),
?line ok = eval(digraph_utils:reaching([b], G), [b]),
?line ok = eval(digraph_utils:reaching_neighbours([b], G), []),
?line ok = eval(digraph_utils:reachable([b], G), [b,c,d]),
?line ok = eval(digraph_utils:reachable_neighbours([b], G), [c,d]),
?line ok = eval(digraph_utils:reaching([h], G), [h]),
?line ok = eval(digraph_utils:reaching_neighbours([h], G), [h]),
?line ok = eval(digraph_utils:reachable([h], G), [h]),
?line ok = eval(digraph_utils:reachable_neighbours([h], G), [h]),
?line ok = eval(digraph_utils:reachable([e,f], G), [e,f,g]),
?line ok = eval(digraph_utils:reachable_neighbours([e,f], G), [e,f,g]),
?line ok = eval(digraph_utils:reachable([h,h,h], G), [h]),
?line true = digraph:delete(G),
ok.
loop(doc) -> [];
loop(suite) -> [];
loop(Config) when is_list(Config) ->
?line G = digraph:new(),
?line add_vertices(G, [a,b]),
?line add_edges(G, [{a,a},{b,b}]),
?line ok = evall(digraph_utils:components(G), [[a],[b]]),
?line ok = evall(digraph_utils:strong_components(G), [[a],[b]]),
?line ok = evall(digraph_utils:cyclic_strong_components(G), [[a],[b]]),
?line [_,_] = digraph_utils:topsort(G),
?line false = digraph_utils:is_acyclic(G),
?line ok = eval(digraph_utils:loop_vertices(G), [a,b]),
?line [_,_] = digraph_utils:preorder(G),
?line [_,_] = digraph_utils:postorder(G),
?line ok = eval(digraph_utils:reaching([b], G), [b]),
?line ok = eval(digraph_utils:reaching_neighbours([b], G), [b]),
?line ok = eval(digraph_utils:reachable([b], G), [b]),
?line ok = eval(digraph_utils:reachable_neighbours([b], G), [b]),
?line true = path(G, a, a),
?line true = digraph:delete(G),
ok.
isolated(doc) -> [];
isolated(suite) -> [];
isolated(Config) when is_list(Config) ->
?line G = digraph:new(),
?line add_vertices(G, [a,b]),
?line ok = evall(digraph_utils:components(G), [[a],[b]]),
?line ok = evall(digraph_utils:strong_components(G), [[a],[b]]),
?line ok = evall(digraph_utils:cyclic_strong_components(G), []),
?line [_,_] = digraph_utils:topsort(G),
?line true = digraph_utils:is_acyclic(G),
?line ok = eval(digraph_utils:loop_vertices(G), []),
?line [_,_] = digraph_utils:preorder(G),
?line [_,_] = digraph_utils:postorder(G),
?line ok = eval(digraph_utils:reaching([b], G), [b]),
?line ok = eval(digraph_utils:reaching_neighbours([b], G), []),
?line ok = eval(digraph_utils:reachable([b], G), [b]),
?line ok = eval(digraph_utils:reachable_neighbours([b], G), []),
?line false = path(G, a, a),
?line true = digraph:delete(G),
ok.
topsort(doc) -> [];
topsort(suite) -> [];
topsort(Config) when is_list(Config) ->
?line G = digraph:new(),
?line add_edges(G, [{a,b},{b,c},{c,d},{d,e},{e,f}]),
?line ok = eval(digraph_utils:topsort(G), [a,b,c,d,e,f]),
?line true = digraph:delete(G),
ok.
subgraph(doc) -> [];
subgraph(suite) -> [];
subgraph(Config) when is_list(Config) ->
?line G = digraph:new([acyclic]),
?line add_edges(G, [{b,c},{b,d},{e,f},{f,fg,fgl,g},{f,fg2,fgl2,g},{g,e},
{h,h},{i,i},{i,j}]),
?line add_vertices(G, [{b,bl},{f,fl}]),
?line SG = digraph_utils:subgraph(G, [u1,b,c,u2,f,g,i,u3]),
?line [b,c,f,g,i] = lists:sort(digraph:vertices(SG)),
?line {b,bl} = digraph:vertex(SG, b),
?line {c,[]} = digraph:vertex(SG, c),
?line {fg,f,g,fgl} = digraph:edge(SG, fg),
?line {fg2,f,g,fgl2} = digraph:edge(SG, fg2),
?line {_, {_, acyclic}} = lists:keysearch(cyclicity, 1, digraph:info(SG)),
?line true = digraph:delete(SG),
?line SG1 = digraph_utils:subgraph(G, [f, g, h],
[{type, []}, {keep_labels, false}]),
?line [f,g,h] = lists:sort(digraph:vertices(SG1)),
?line {f,[]} = digraph:vertex(SG1, f),
?line {fg,f,g,[]} = digraph:edge(SG1, fg),
?line {_, {_, cyclic}} = lists:keysearch(cyclicity, 1, digraph:info(SG1)),
?line true = digraph:delete(SG1),
?line SG2 = digraph_utils:subgraph(G, [f, g, h],
[{type, [acyclic]},
{keep_labels, true}]),
?line [f,g,h] = lists:sort(digraph:vertices(SG2)),
?line {f,fl} = digraph:vertex(SG2, f),
?line {fg,f,g,fgl} = digraph:edge(SG2, fg),
?line {_, {_, acyclic}} = lists:keysearch(cyclicity, 1, digraph:info(SG2)),
?line true = digraph:delete(SG2),
?line {'EXIT',{badarg,_}} =
(catch digraph_utils:subgraph(G, [f], [{invalid, opt}])),
?line {'EXIT',{badarg,_}} =
(catch digraph_utils:subgraph(G, [f], [{keep_labels, not_Bool}])),
?line {'EXIT',{badarg,_}} =
(catch digraph_utils:subgraph(G, [f], [{type, not_type}])),
?line {'EXIT',{badarg,_}} =
(catch digraph_utils:subgraph(G, [f], [{type, [not_type]}])),
?line {'EXIT',{badarg,_}} =
(catch digraph_utils:subgraph(G, [f], not_a_list)),
?line true = digraph:delete(G),
ok.
condensation(doc) -> [];
condensation(suite) -> [];
condensation(Config) when is_list(Config) ->
?line G = digraph:new([]),
?line add_edges(G, [{b,c},{b,d},{e,f},{f,fg,fgl,g},{f,fg2,fgl2,g},{g,e},
{h,h},{j,i},{i,j}]),
?line add_vertices(G, [q]),
?line CG = digraph_utils:condensation(G),
?line Vs = sort_2(digraph:vertices(CG)),
?line [[b],[c],[d],[e,f,g],[h],[i,j],[q]] = Vs,
?line Fun = fun(E) ->
{_E, V1, V2, _L} = digraph:edge(CG, E),
{lists:sort(V1), lists:sort(V2)}
end,
?line Es = lists:map(Fun, digraph:edges(CG)),
?line [{[b],[c]},{[b],[d]},{[e,f,g],[e,f,g]},{[h],[h]},{[i,j],[i,j]}] =
lists:sort(Es),
?line true = digraph:delete(CG),
?line true = digraph:delete(G),
ok.
tree(doc) -> ["OTP-7081"];
tree(suite) -> [];
tree(Config) when is_list(Config) ->
?line false = is_tree([], []),
?line true = is_tree([a], []),
?line false = is_tree([a,b], []),
?line true = is_tree([{a,b}]),
?line false = is_tree([{a,b},{b,a}]),
?line true = is_tree([{a,b},{a,c},{b,d},{b,e}]),
?line false = is_tree([{a,b},{a,c},{b,d},{b,e}, {d,e}]),
?line false = is_tree([{a,b},{a,c},{b,d},{b,e}, {b,e}]),
?line true = is_tree([{a,c},{c,b}]),
?line true = is_tree([{b,a},{c,a}]),
%% Parallel edges. Acyclic and with one componets
%% (according to the digraph module).
?line false = is_tree([{a,b},{a,b}]),
?line no = arborescence_root([], []),
?line {yes, a} = arborescence_root([a], []),
?line no = arborescence_root([a,b], []),
?line {yes, a} = arborescence_root([{a,b}]),
?line no = arborescence_root([{a,b},{b,a}]),
?line {yes, a} = arborescence_root([{a,b},{a,c},{b,d},{b,e}]),
?line no = arborescence_root([{a,b},{a,c},{b,d},{b,e}, {d,e}]),
?line no = arborescence_root([{a,b},{a,c},{b,d},{b,e}, {b,e}]),
?line {yes, a} = arborescence_root([{a,c},{c,b}]),
?line no = arborescence_root([{b,a},{c,a}]),
?line false = is_arborescence([], []),
?line true = is_arborescence([a], []),
?line false = is_arborescence([a,b], []),
?line true = is_arborescence([{a,b}]),
?line false = is_arborescence([{a,b},{b,a}]),
?line true = is_arborescence([{a,b},{a,c},{b,d},{b,e}]),
?line false = is_arborescence([{a,b},{a,c},{b,d},{b,e}, {d,e}]),
?line false = is_arborescence([{a,b},{a,c},{b,d},{b,e}, {b,e}]),
?line true = is_arborescence([{a,c},{c,b}]),
?line false = is_arborescence([{b,a},{c,a}]),
%% Parallel edges.
?line false = is_arborescence([{a,b},{a,b}]),
ok.
is_tree(Es) ->
is_tree([], Es).
is_tree(Vs, Es) ->
gu(Vs, Es, is_tree).
is_arborescence(Es) ->
is_arborescence([], Es).
is_arborescence(Vs, Es) ->
gu(Vs, Es, is_arborescence).
arborescence_root(Es) ->
arborescence_root([], Es).
arborescence_root(Vs, Es) ->
gu(Vs, Es, arborescence_root).
gu(Vs, Es, F) ->
G = digraph:new(),
add_vertices(G, Vs),
add_edges(G, Es),
Reply = digraph_utils:F(G),
true = digraph:delete(G),
Reply.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
sort_2(L) ->
lists:sort(lists:map(fun(V) -> lists:sort(V) end, L)).
path(G, V1, V2) ->
digraph:get_path(G, V1, V2) /= false.
add_vertices(G, Vs) ->
lists:foreach(fun({V, Label}) -> digraph:add_vertex(G, V, Label);
(V) -> digraph:add_vertex(G, V)
end, Vs).
add_edges(G, L) ->
Fun = fun({From, To}) ->
digraph:add_vertex(G, From),
digraph:add_vertex(G, To),
digraph:add_edge(G, From, To);
({From, Edge, Label, To}) ->
digraph:add_vertex(G, From),
digraph:add_vertex(G, To),
digraph:add_edge(G, Edge, From, To, Label)
end,
lists:foreach(Fun, L).
eval(L, E) ->
Expected = lists:sort(E),
Got = lists:sort(L),
if
Expected == Got ->
ok;
true ->
not_ok
end.
evall(L, E) ->
F = fun(L1) -> lists:sort(L1) end,
Fun = fun(LL) -> F(lists:map(F, LL)) end,
Expected = Fun(E),
Got = Fun(L),
if
Expected == Got ->
ok;
true ->
not_ok
end.
|