eyeprolog 1.3.78 → 1.3.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,633 @@
1
+ /* Author: R.A.O'Keefe, Vitor Santos Costa, Jan Wielemaker
2
+ E-mail: J.Wielemaker@vu.nl
3
+ WWW: http://www.swi-prolog.org
4
+ Copyright (c) 1984-2021, VU University Amsterdam
5
+ CWI, Amsterdam
6
+ SWI-Prolog Solutions .b.v
7
+ All rights reserved.
8
+
9
+ Redistribution and use in source and binary forms, with or without
10
+ modification, are permitted provided that the following conditions
11
+ are met:
12
+
13
+ 1. Redistributions of source code must retain the above copyright
14
+ notice, this list of conditions and the following disclaimer.
15
+
16
+ 2. Redistributions in binary form must reproduce the above copyright
17
+ notice, this list of conditions and the following disclaimer in
18
+ the documentation and/or other materials provided with the
19
+ distribution.
20
+
21
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
+ POSSIBILITY OF SUCH DAMAGE.
33
+ */
34
+
35
+ :- module(ugraphs,
36
+ [ add_edges/3, % +Graph, +Edges, -NewGraph
37
+ add_vertices/3, % +Graph, +Vertices, -NewGraph
38
+ complement/2, % +Graph, -NewGraph
39
+ compose/3, % +LeftGraph, +RightGraph, -NewGraph
40
+ del_edges/3, % +Graph, +Edges, -NewGraph
41
+ del_vertices/3, % +Graph, +Vertices, -NewGraph
42
+ edges/2, % +Graph, -Edges
43
+ neighbors/3, % +Vertex, +Graph, -Vertices
44
+ neighbours/3, % +Vertex, +Graph, -Vertices
45
+ reachable/3, % +Vertex, +Graph, -Vertices
46
+ top_sort/2, % +Graph, -Sort
47
+ top_sort/3, % +Graph, -Sort0, -Sort
48
+ transitive_closure/2, % +Graph, -Closure
49
+ transpose_ugraph/2, % +Graph, -NewGraph
50
+ vertices/2, % +Graph, -Vertices
51
+ vertices_edges_to_ugraph/3, % +Vertices, +Edges, -Graph
52
+ ugraph_union/3, % +Graph1, +Graph2, -Graph
53
+ connect_ugraph/3 % +Graph1, -Start, -Graph
54
+ ]).
55
+
56
+ /** Graph manipulation library
57
+
58
+ The S-representation of a graph is a list of (vertex-neighbours) pairs,
59
+ where the pairs are in standard order (as produced by keysort) and the
60
+ neighbours of each vertex are also in standard order (as produced by
61
+ sort). This form is convenient for many calculations.
62
+
63
+ A new UGraph from raw data can be created using
64
+ `vertices_edges_to_ugraph/3`.
65
+
66
+ Adapted to support some of the functionality of the SICStus ugraphs
67
+ library by Vitor Santos Costa.
68
+
69
+ Ported from YAP 5.0.1 to SWI-Prolog by Jan Wielemaker.
70
+
71
+ Ported from SWI-Prolog to Scryer by [Adrián Arroyo Calle](https://adrianistan.eu)
72
+
73
+ License: BSD-2 or Artistic 2.0
74
+ */
75
+
76
+ :- use_module(library(lists)).
77
+ :- use_module(library(pairs)).
78
+ :- use_module(library(ordsets)).
79
+
80
+ %% vertices(+Graph, -Vertices)
81
+ %
82
+ % Unify Vertices with all vertices appearing in Graph. Example:
83
+ %
84
+ % ```
85
+ % ?- vertices([1-[3,5],2-[4],3-[],4-[5],5-[]], L).
86
+ % L = [1, 2, 3, 4, 5]
87
+ % ```
88
+
89
+ vertices([], []) :- !.
90
+ vertices([Vertex-_|Graph], [Vertex|Vertices]) :-
91
+ vertices(Graph, Vertices).
92
+
93
+
94
+ %% vertices_edges_to_ugraph(+Vertices, +Edges, -UGraph) is det.
95
+ %
96
+ % Create a UGraph from Vertices and edges. Given a graph with a
97
+ % set of Vertices and a set of Edges, Graph must unify with the
98
+ % corresponding S-representation. Note that the vertices without
99
+ % edges will appear in Vertices but not in Edges. Moreover, it is
100
+ % sufficient for a vertice to appear in Edges.
101
+ %
102
+ % ```
103
+ % ?- vertices_edges_to_ugraph([],[1-3,2-4,4-5,1-5], L).
104
+ % L = [1-[3,5], 2-[4], 3-[], 4-[5], 5-[]]
105
+ % ```
106
+ %
107
+ % In this case all vertices are defined implicitly. The next
108
+ % example shows three unconnected vertices:
109
+ %
110
+ % ```
111
+ % ?- vertices_edges_to_ugraph([6,7,8],[1-3,2-4,4-5,1-5], L).
112
+ % L = [1-[3,5], 2-[4], 3-[], 4-[5], 5-[], 6-[], 7-[], 8-[]]
113
+ % ```
114
+
115
+ vertices_edges_to_ugraph(Vertices, Edges, Graph) :-
116
+ sort(Edges, EdgeSet),
117
+ p_to_s_vertices(EdgeSet, IVertexBag),
118
+ append(Vertices, IVertexBag, VertexBag),
119
+ sort(VertexBag, VertexSet),
120
+ p_to_s_group(VertexSet, EdgeSet, Graph).
121
+
122
+
123
+ %% add_vertices(+Graph, +Vertices, -NewGraph)
124
+ %
125
+ % Unify NewGraph with a new graph obtained by adding the list of
126
+ % Vertices to Graph. Example:
127
+ %
128
+ % ```
129
+ % ?- add_vertices([1-[3,5],2-[]], [0,1,2,9], NG).
130
+ % NG = [0-[], 1-[3,5], 2-[], 9-[]]
131
+ % ```
132
+
133
+ % replace with real msort/2 when available
134
+ msort_(List, Sorted) :-
135
+ pairs_keys(Pairs, List),
136
+ keysort(Pairs, SortedPairs),
137
+ pairs_keys(SortedPairs, Sorted).
138
+
139
+ add_vertices(Graph, Vertices, NewGraph) :-
140
+ % msort/2 not available in Scryer Prolog yet: msort(Vertices, V1),
141
+ msort_(Vertices, V1),
142
+ add_vertices_to_s_graph(V1, Graph, NewGraph).
143
+
144
+ add_vertices_to_s_graph(L, [], NL) :-
145
+ !,
146
+ add_empty_vertices(L, NL).
147
+ add_vertices_to_s_graph([], L, L) :- !.
148
+ add_vertices_to_s_graph([V1|VL], [V-Edges|G], NGL) :-
149
+ compare(Res, V1, V),
150
+ add_vertices_to_s_graph(Res, V1, VL, V, Edges, G, NGL).
151
+
152
+ add_vertices_to_s_graph(=, _, VL, V, Edges, G, [V-Edges|NGL]) :-
153
+ add_vertices_to_s_graph(VL, G, NGL).
154
+ add_vertices_to_s_graph(<, V1, VL, V, Edges, G, [V1-[]|NGL]) :-
155
+ add_vertices_to_s_graph(VL, [V-Edges|G], NGL).
156
+ add_vertices_to_s_graph(>, V1, VL, V, Edges, G, [V-Edges|NGL]) :-
157
+ add_vertices_to_s_graph([V1|VL], G, NGL).
158
+
159
+ add_empty_vertices([], []).
160
+ add_empty_vertices([V|G], [V-[]|NG]) :-
161
+ add_empty_vertices(G, NG).
162
+
163
+ %% del_vertices(+Graph, +Vertices, -NewGraph) is det.
164
+ %
165
+ % Unify NewGraph with a new graph obtained by deleting the list of
166
+ % Vertices and all the edges that start from or go to a vertex in
167
+ % Vertices to the Graph. Example:
168
+ %
169
+ % ```
170
+ % ?- del_vertices([1-[3,5],2-[4],3-[],4-[5],5-[],6-[],7-[2,6],8-[]],
171
+ % [2,1],
172
+ % NL).
173
+ % NL = [3-[],4-[5],5-[],6-[],7-[6],8-[]]
174
+ % ```
175
+
176
+ del_vertices(Graph, Vertices, NewGraph) :-
177
+ sort(Vertices, V1), % JW: was msort
178
+ ( V1 = []
179
+ -> Graph = NewGraph
180
+ ; del_vertices(Graph, V1, V1, NewGraph)
181
+ ).
182
+
183
+ del_vertices(G, [], V1, NG) :-
184
+ !,
185
+ del_remaining_edges_for_vertices(G, V1, NG).
186
+ del_vertices([], _, _, []).
187
+ del_vertices([V-Edges|G], [V0|Vs], V1, NG) :-
188
+ compare(Res, V, V0),
189
+ split_on_del_vertices(Res, V,Edges, [V0|Vs], NVs, V1, NG, NGr),
190
+ del_vertices(G, NVs, V1, NGr).
191
+
192
+ del_remaining_edges_for_vertices([], _, []).
193
+ del_remaining_edges_for_vertices([V0-Edges|G], V1, [V0-NEdges|NG]) :-
194
+ ord_subtract(Edges, V1, NEdges),
195
+ del_remaining_edges_for_vertices(G, V1, NG).
196
+
197
+ split_on_del_vertices(<, V, Edges, Vs, Vs, V1, [V-NEdges|NG], NG) :-
198
+ ord_subtract(Edges, V1, NEdges).
199
+ split_on_del_vertices(>, V, Edges, [_|Vs], Vs, V1, [V-NEdges|NG], NG) :-
200
+ ord_subtract(Edges, V1, NEdges).
201
+ split_on_del_vertices(=, _, _, [_|Vs], Vs, _, NG, NG).
202
+
203
+ %% add_edges(+Graph, +Edges, -NewGraph)
204
+ %
205
+ % Unify NewGraph with a new graph obtained by adding the list of Edges
206
+ % to Graph. Example:
207
+ %
208
+ % ```
209
+ % ?- add_edges([1-[3,5],2-[4],3-[],4-[5],
210
+ % 5-[],6-[],7-[],8-[]],
211
+ % [1-6,2-3,3-2,5-7,3-2,4-5],
212
+ % NL).
213
+ % NL = [1-[3,5,6], 2-[3,4], 3-[2], 4-[5],
214
+ % 5-[7], 6-[], 7-[], 8-[]]
215
+ % ```
216
+
217
+ add_edges(Graph, Edges, NewGraph) :-
218
+ p_to_s_graph(Edges, G1),
219
+ ugraph_union(Graph, G1, NewGraph).
220
+
221
+ %% ugraph_union(+Graph1, +Graph2, -NewGraph)
222
+ %
223
+ % NewGraph is the union of Graph1 and Graph2. Example:
224
+ %
225
+ % ```
226
+ % ?- ugraph_union([1-[2],2-[3]],[2-[4],3-[1,2,4]],L).
227
+ % L = [1-[2], 2-[3,4], 3-[1,2,4]]
228
+ % ```
229
+
230
+ ugraph_union(Set1, [], Set1) :- !.
231
+ ugraph_union([], Set2, Set2) :- !.
232
+ ugraph_union([Head1-E1|Tail1], [Head2-E2|Tail2], Union) :-
233
+ compare(Order, Head1, Head2),
234
+ ugraph_union(Order, Head1-E1, Tail1, Head2-E2, Tail2, Union).
235
+
236
+ ugraph_union(=, Head-E1, Tail1, _-E2, Tail2, [Head-Es|Union]) :-
237
+ ord_union(E1, E2, Es),
238
+ ugraph_union(Tail1, Tail2, Union).
239
+ ugraph_union(<, Head1, Tail1, Head2, Tail2, [Head1|Union]) :-
240
+ ugraph_union(Tail1, [Head2|Tail2], Union).
241
+ ugraph_union(>, Head1, Tail1, Head2, Tail2, [Head2|Union]) :-
242
+ ugraph_union([Head1|Tail1], Tail2, Union).
243
+
244
+ %% del_edges(+Graph, +Edges, -NewGraph)
245
+ %
246
+ % Unify NewGraph with a new graph obtained by removing the list of
247
+ % Edges from Graph. Notice that no vertices are deleted. Example:
248
+ %
249
+ % ```
250
+ % ?- del_edges([1-[3,5],2-[4],3-[],4-[5],5-[],6-[],7-[],8-[]],
251
+ % [1-6,2-3,3-2,5-7,3-2,4-5,1-3],
252
+ % NL).
253
+ % NL = [1-[5],2-[4],3-[],4-[],5-[],6-[],7-[],8-[]]
254
+ % ```
255
+
256
+ del_edges(Graph, Edges, NewGraph) :-
257
+ p_to_s_graph(Edges, G1),
258
+ graph_subtract(Graph, G1, NewGraph).
259
+
260
+ %% graph_subtract(+Set1, +Set2, ?Difference)
261
+ %
262
+ % Is based on `ord_subtract/3`
263
+
264
+ graph_subtract(Set1, [], Set1) :- !.
265
+ graph_subtract([], _, []).
266
+ graph_subtract([Head1-E1|Tail1], [Head2-E2|Tail2], Difference) :-
267
+ compare(Order, Head1, Head2),
268
+ graph_subtract(Order, Head1-E1, Tail1, Head2-E2, Tail2, Difference).
269
+
270
+ graph_subtract(=, H-E1, Tail1, _-E2, Tail2, [H-E|Difference]) :-
271
+ ord_subtract(E1,E2,E),
272
+ graph_subtract(Tail1, Tail2, Difference).
273
+ graph_subtract(<, Head1, Tail1, Head2, Tail2, [Head1|Difference]) :-
274
+ graph_subtract(Tail1, [Head2|Tail2], Difference).
275
+ graph_subtract(>, Head1, Tail1, _, Tail2, Difference) :-
276
+ graph_subtract([Head1|Tail1], Tail2, Difference).
277
+
278
+ %% edges(+Graph, -Edges)
279
+ %
280
+ % Unify Edges with all edges appearing in Graph. Example:
281
+ %
282
+ % ```
283
+ % ?- edges([1-[3,5],2-[4],3-[],4-[5],5-[]], L).
284
+ % L = [1-3, 1-5, 2-4, 4-5]
285
+ % ```
286
+
287
+ edges(Graph, Edges) :-
288
+ s_to_p_graph(Graph, Edges).
289
+
290
+ p_to_s_graph(P_Graph, S_Graph) :-
291
+ sort(P_Graph, EdgeSet),
292
+ p_to_s_vertices(EdgeSet, VertexBag),
293
+ sort(VertexBag, VertexSet),
294
+ p_to_s_group(VertexSet, EdgeSet, S_Graph).
295
+
296
+
297
+ p_to_s_vertices([], []).
298
+ p_to_s_vertices([A-Z|Edges], [A,Z|Vertices]) :-
299
+ p_to_s_vertices(Edges, Vertices).
300
+
301
+
302
+ p_to_s_group([], _, []).
303
+ p_to_s_group([Vertex|Vertices], EdgeSet, [Vertex-Neibs|G]) :-
304
+ p_to_s_group(EdgeSet, Vertex, Neibs, RestEdges),
305
+ p_to_s_group(Vertices, RestEdges, G).
306
+
307
+
308
+ p_to_s_group([V1-X|Edges], V2, [X|Neibs], RestEdges) :- V1 == V2,
309
+ !,
310
+ p_to_s_group(Edges, V2, Neibs, RestEdges).
311
+ p_to_s_group(Edges, _, [], Edges).
312
+
313
+
314
+
315
+ s_to_p_graph([], []) :- !.
316
+ s_to_p_graph([Vertex-Neibs|G], P_Graph) :-
317
+ s_to_p_graph(Neibs, Vertex, P_Graph, Rest_P_Graph),
318
+ s_to_p_graph(G, Rest_P_Graph).
319
+
320
+
321
+ s_to_p_graph([], _, P_Graph, P_Graph) :- !.
322
+ s_to_p_graph([Neib|Neibs], Vertex, [Vertex-Neib|P], Rest_P) :-
323
+ s_to_p_graph(Neibs, Vertex, P, Rest_P).
324
+
325
+ %% transitive_closure(+Graph, -Closure)
326
+ %
327
+ % Generate the graph Closure as the transitive closure of Graph.
328
+ % Example:
329
+ %
330
+ % ```
331
+ % ?- transitive_closure([1-[2,3],2-[4,5],4-[6]],L).
332
+ % L = [1-[2,3,4,5,6], 2-[4,5,6], 4-[6]]
333
+ % ```
334
+
335
+ transitive_closure(Graph, Closure) :-
336
+ warshall(Graph, Graph, Closure).
337
+
338
+ warshall([], Closure, Closure) :- !.
339
+ warshall([V-_|G], E, Closure) :-
340
+ memberchk(V-Y, E), % Y := E(v)
341
+ warshall(E, V, Y, NewE),
342
+ warshall(G, NewE, Closure).
343
+
344
+
345
+ warshall([X-Neibs|G], V, Y, [X-NewNeibs|NewG]) :-
346
+ memberchk(V, Neibs),
347
+ !,
348
+ ord_union(Neibs, Y, NewNeibs),
349
+ warshall(G, V, Y, NewG).
350
+ warshall([X-Neibs|G], V, Y, [X-Neibs|NewG]) :-
351
+ !,
352
+ warshall(G, V, Y, NewG).
353
+ warshall([], _, _, []).
354
+
355
+ %% transpose_ugraph(Graph, NewGraph) is det.
356
+ %
357
+ % Unify NewGraph with a new graph obtained from Graph by replacing
358
+ % all edges of the form V1-V2 by edges of the form V2-V1. The cost
359
+ % is O(|V|\*log(|V|)). Notice that an undirected graph is its own
360
+ % transpose. Example:
361
+ %
362
+ % ```
363
+ % ?- transpose([1-[3,5],2-[4],3-[],4-[5],
364
+ % 5-[],6-[],7-[],8-[]], NL).
365
+ % NL = [1-[],2-[],3-[1],4-[2],5-[1,4],6-[],7-[],8-[]]
366
+ % ```
367
+
368
+ transpose_ugraph(Graph, NewGraph) :-
369
+ edges(Graph, Edges),
370
+ vertices(Graph, Vertices),
371
+ flip_edges(Edges, TransposedEdges),
372
+ vertices_edges_to_ugraph(Vertices, TransposedEdges, NewGraph).
373
+
374
+ flip_edges([], []).
375
+ flip_edges([Key-Val|Pairs], [Val-Key|Flipped]) :-
376
+ flip_edges(Pairs, Flipped).
377
+
378
+ %% compose(+LeftGraph, +RightGraph, -NewGraph)
379
+ %
380
+ % Compose NewGraph by connecting the _drains_ of LeftGraph to the
381
+ % _sources_ of RightGraph. Example:
382
+ %
383
+ % ```
384
+ % ?- compose([1-[2],2-[3]],[2-[4],3-[1,2,4]],L).
385
+ % L = [1-[4], 2-[1,2,4], 3-[]]
386
+ % ```
387
+
388
+ compose(G1, G2, Composition) :-
389
+ vertices(G1, V1),
390
+ vertices(G2, V2),
391
+ ord_union(V1, V2, V),
392
+ compose(V, G1, G2, Composition).
393
+
394
+ compose([], _, _, []) :- !.
395
+ compose([Vertex|Vertices], [Vertex-Neibs|G1], G2,
396
+ [Vertex-Comp|Composition]) :-
397
+ !,
398
+ compose1(Neibs, G2, [], Comp),
399
+ compose(Vertices, G1, G2, Composition).
400
+ compose([Vertex|Vertices], G1, G2, [Vertex-[]|Composition]) :-
401
+ compose(Vertices, G1, G2, Composition).
402
+
403
+
404
+ compose1([V1|Vs1], [V2-N2|G2], SoFar, Comp) :-
405
+ compare(Rel, V1, V2),
406
+ !,
407
+ compose1(Rel, V1, Vs1, V2, N2, G2, SoFar, Comp).
408
+ compose1(_, _, Comp, Comp).
409
+
410
+
411
+ compose1(<, _, Vs1, V2, N2, G2, SoFar, Comp) :-
412
+ !,
413
+ compose1(Vs1, [V2-N2|G2], SoFar, Comp).
414
+ compose1(>, V1, Vs1, _, _, G2, SoFar, Comp) :-
415
+ !,
416
+ compose1([V1|Vs1], G2, SoFar, Comp).
417
+ compose1(=, V1, Vs1, V1, N2, G2, SoFar, Comp) :-
418
+ ord_union(N2, SoFar, Next),
419
+ compose1(Vs1, G2, Next, Comp).
420
+
421
+ %% top_sort(+Graph, -Sorted) is semidet.
422
+ %
423
+ % Sorted is a topological sorted list of nodes in Graph. A
424
+ % toplogical sort is possible if the graph is connected and
425
+ % acyclic. In the example we show how topological sorting works
426
+ % for a linear graph:
427
+ %
428
+ % ```
429
+ % ?- top_sort([1-[2], 2-[3], 3-[]], L).
430
+ % L = [1, 2, 3]
431
+ % ```
432
+
433
+ top_sort(Graph, Sorted) :-
434
+ vertices_and_zeros(Graph, Vertices, Counts0),
435
+ count_edges(Graph, Vertices, Counts0, Counts1),
436
+ select_zeros(Counts1, Vertices, Zeros),
437
+ top_sort(Zeros, Sorted, Graph, Vertices, Counts1).
438
+
439
+ %% top_sort(+Graph, -Sorted, ?Tail) is semidet.
440
+ %
441
+ % The predicate `top_sort/3` is a difference list version of
442
+ % `top_sort/2`.
443
+
444
+ top_sort(Graph, Sorted0, Sorted) :-
445
+ vertices_and_zeros(Graph, Vertices, Counts0),
446
+ count_edges(Graph, Vertices, Counts0, Counts1),
447
+ select_zeros(Counts1, Vertices, Zeros),
448
+ top_sort(Zeros, Sorted, Sorted0, Graph, Vertices, Counts1).
449
+
450
+
451
+ vertices_and_zeros([], [], []) :- !.
452
+ vertices_and_zeros([Vertex-_|Graph], [Vertex|Vertices], [0|Zeros]) :-
453
+ vertices_and_zeros(Graph, Vertices, Zeros).
454
+
455
+
456
+ count_edges([], _, Counts, Counts) :- !.
457
+ count_edges([_-Neibs|Graph], Vertices, Counts0, Counts2) :-
458
+ incr_list(Neibs, Vertices, Counts0, Counts1),
459
+ count_edges(Graph, Vertices, Counts1, Counts2).
460
+
461
+
462
+ incr_list([], _, Counts, Counts) :- !.
463
+ incr_list([V1|Neibs], [V2|Vertices], [M|Counts0], [N|Counts1]) :-
464
+ V1 == V2,
465
+ !,
466
+ N is M+1,
467
+ incr_list(Neibs, Vertices, Counts0, Counts1).
468
+ incr_list(Neibs, [_|Vertices], [N|Counts0], [N|Counts1]) :-
469
+ incr_list(Neibs, Vertices, Counts0, Counts1).
470
+
471
+
472
+ select_zeros([], [], []) :- !.
473
+ select_zeros([0|Counts], [Vertex|Vertices], [Vertex|Zeros]) :-
474
+ !,
475
+ select_zeros(Counts, Vertices, Zeros).
476
+ select_zeros([_|Counts], [_|Vertices], Zeros) :-
477
+ select_zeros(Counts, Vertices, Zeros).
478
+
479
+
480
+
481
+ top_sort([], [], Graph, _, Counts) :-
482
+ !,
483
+ vertices_and_zeros(Graph, _, Counts).
484
+ top_sort([Zero|Zeros], [Zero|Sorted], Graph, Vertices, Counts1) :-
485
+ graph_memberchk(Zero-Neibs, Graph),
486
+ decr_list(Neibs, Vertices, Counts1, Counts2, Zeros, NewZeros),
487
+ top_sort(NewZeros, Sorted, Graph, Vertices, Counts2).
488
+
489
+ top_sort([], Sorted0, Sorted0, Graph, _, Counts) :-
490
+ !,
491
+ vertices_and_zeros(Graph, _, Counts).
492
+ top_sort([Zero|Zeros], [Zero|Sorted], Sorted0, Graph, Vertices, Counts1) :-
493
+ graph_memberchk(Zero-Neibs, Graph),
494
+ decr_list(Neibs, Vertices, Counts1, Counts2, Zeros, NewZeros),
495
+ top_sort(NewZeros, Sorted, Sorted0, Graph, Vertices, Counts2).
496
+
497
+ graph_memberchk(Element1-Edges, [Element2-Edges2|_]) :-
498
+ Element1 == Element2,
499
+ !,
500
+ Edges = Edges2.
501
+ graph_memberchk(Element, [_|Rest]) :-
502
+ graph_memberchk(Element, Rest).
503
+
504
+
505
+ decr_list([], _, Counts, Counts, Zeros, Zeros) :- !.
506
+ decr_list([V1|Neibs], [V2|Vertices], [1|Counts1], [0|Counts2], Zi, Zo) :-
507
+ V1 == V2,
508
+ !,
509
+ decr_list(Neibs, Vertices, Counts1, Counts2, [V2|Zi], Zo).
510
+ decr_list([V1|Neibs], [V2|Vertices], [N|Counts1], [M|Counts2], Zi, Zo) :-
511
+ V1 == V2,
512
+ !,
513
+ M is N-1,
514
+ decr_list(Neibs, Vertices, Counts1, Counts2, Zi, Zo).
515
+ decr_list(Neibs, [_|Vertices], [N|Counts1], [N|Counts2], Zi, Zo) :-
516
+ decr_list(Neibs, Vertices, Counts1, Counts2, Zi, Zo).
517
+
518
+
519
+
520
+ %% neighbours(+Vertex, +Graph, -Neigbours) is det.
521
+ %
522
+ % Neigbours is a sorted list of the neighbours of Vertex in Graph.
523
+ % Example:
524
+ %
525
+ % ```
526
+ % ?- neighbours(4,[1-[3,5],2-[4],3-[],
527
+ % 4-[1,2,7,5],5-[],6-[],7-[],8-[]], NL).
528
+ % NL = [1,2,7,5]
529
+ % ```
530
+
531
+ %% neighbors(+Vertex, +Graph, -Neigbours) is det.
532
+ %
533
+ % Same as `neighbours/3`.
534
+
535
+ neighbors(Vertex, Graph, Neig) :-
536
+ neighbours(Vertex, Graph, Neig).
537
+
538
+ neighbours(V,[V0-Neig|_],Neig) :-
539
+ V == V0,
540
+ !.
541
+ neighbours(V,[_|G],Neig) :-
542
+ neighbours(V,G,Neig).
543
+
544
+
545
+ %% connect_ugraph(+UGraphIn, -Start, -UGraphOut) is det.
546
+ %
547
+ % Adds Start as an additional vertex that is connected to all vertices
548
+ % in UGraphIn. This can be used to create an topological sort for a
549
+ % not connected graph. Start is before any vertex in UGraphIn in the
550
+ % standard order of terms. No vertex in UGraphIn can be a variable.
551
+ %
552
+ % Can be used to order a not-connected graph as follows:
553
+ %
554
+ % ```
555
+ % top_sort_unconnected(Graph, Vertices) :-
556
+ % ( top_sort(Graph, Vertices)
557
+ % -> true
558
+ % ; connect_ugraph(Graph, Start, Connected),
559
+ % top_sort(Connected, Ordered0),
560
+ % Ordered0 = [Start|Vertices]
561
+ % ).
562
+ % ```
563
+
564
+ connect_ugraph([], 0, []) :- !.
565
+ connect_ugraph(Graph, Start, [Start-Vertices|Graph]) :-
566
+ vertices(Graph, Vertices),
567
+ Vertices = [First|_],
568
+ before(First, Start).
569
+
570
+ %% before(+Term, -Before) is det.
571
+ %
572
+ % Unify Before to a term that comes before Term in the standard
573
+ % order of terms.
574
+ %
575
+ % Throws `instantiation_error` if Term is unbound.
576
+
577
+ before(X, _) :-
578
+ var(X),
579
+ !,
580
+ instantiation_error(X).
581
+ before(Number, Start) :-
582
+ number(Number),
583
+ !,
584
+ Start is Number - 1.
585
+ before(_, 0).
586
+
587
+
588
+ %% complement(+UGraphIn, -UGraphOut)
589
+ %
590
+ % UGraphOut is a ugraph with an edge between all vertices that are
591
+ % _not_ connected in UGraphIn and all edges from UGraphIn removed.
592
+ % Example:
593
+ %
594
+ % ```
595
+ % ?- complement([1-[3,5],2-[4],3-[],
596
+ % 4-[1,2,7,5],5-[],6-[],7-[],8-[]], NL).
597
+ % NL = [1-[2,4,6,7,8],2-[1,3,5,6,7,8],3-[1,2,4,5,6,7,8],
598
+ % 4-[3,5,6,8],5-[1,2,3,4,6,7,8],6-[1,2,3,4,5,7,8],
599
+ % 7-[1,2,3,4,5,6,8],8-[1,2,3,4,5,6,7]]
600
+ % ```
601
+
602
+
603
+ % TODO: Simple two-step algorithm. You could be smarter, I suppose.
604
+
605
+ complement(G, NG) :-
606
+ vertices(G,Vs),
607
+ complement(G,Vs,NG).
608
+
609
+ complement([], _, []).
610
+ complement([V-Ns|G], Vs, [V-INs|NG]) :-
611
+ ord_add_element(Ns,V,Ns1),
612
+ ord_subtract(Vs,Ns1,INs),
613
+ complement(G, Vs, NG).
614
+
615
+ %% reachable(+Vertex, +UGraph, -Vertices)
616
+ %
617
+ % True when Vertices is an ordered set of vertices reachable in
618
+ % UGraph, including Vertex. Example:
619
+ %
620
+ % ```
621
+ % ?- reachable(1,[1-[3,5],2-[4],3-[],4-[5],5-[]],V).
622
+ % V = [1, 3, 5]
623
+ % ```
624
+
625
+ reachable(N, G, Rs) :-
626
+ reachable([N], G, [N], Rs).
627
+
628
+ reachable([], _, Rs, Rs).
629
+ reachable([N|Ns], G, Rs0, RsF) :-
630
+ neighbours(N, G, Nei),
631
+ ord_union(Rs0, Nei, Rs1, D),
632
+ append(Ns, D, Nsi),
633
+ reachable(Nsi, G, Rs1, RsF).
package/src/lib/uuid.pl CHANGED
@@ -1,8 +1,66 @@
1
- /** Reproducible UUID version 4 generation. */
1
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2
+ The uuidv4/1, uuidv4_string/1, uuid_string/2 interface and conversion are
3
+ adapted from Scryer Prolog's public-domain library(uuid), written by
4
+ Adrián Arroyo. EyeProlog retains its explicit-state uuid/3 extension.
5
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
2
6
 
3
- :- module(uuid, [uuid/3]).
7
+ /** UUID version 4 generation and conversion. */
4
8
 
5
- :- use_module(library(random), [random/3]).
9
+ :- module(uuid, [uuid/3, uuidv4/1, uuidv4_string/1, uuid_string/2]).
10
+
11
+ :- use_module(library(random), [random/3, random_integer/3]).
12
+
13
+ uuidv4(Uuid) :-
14
+ uuid__random_bytes(16, Bytes),
15
+ Bytes = [B1, B2, B3, B4, B5, B6, B7, B8,
16
+ B9, B10, B11, B12, B13, B14, B15, B16],
17
+ NewTimeHi is 64 + B7 mod 16,
18
+ NewClockSeqHi is 128 + B9 mod 64,
19
+ Uuid = [B1, B2, B3, B4, B5, B6, NewTimeHi, B8,
20
+ NewClockSeqHi, B10, B11, B12, B13, B14, B15, B16].
21
+
22
+ uuidv4_string(String) :-
23
+ uuidv4(Uuid),
24
+ uuid_string(Uuid, String).
25
+
26
+ uuid_string(Uuid, String) :-
27
+ Uuid = [B1, B2, B3, B4, B5, B6, B7, B8,
28
+ B9, B10, B11, B12, B13, B14, B15, B16],
29
+ uuid__append(S1, ['-'|Tail1], String),
30
+ uuid__length(S1, 8),
31
+ uuid__append(S2, ['-'|Tail2], Tail1),
32
+ uuid__length(S2, 4),
33
+ uuid__append(S3, ['-'|Tail3], Tail2),
34
+ uuid__length(S3, 4),
35
+ uuid__append(S4, ['-'|S5], Tail3),
36
+ uuid__length(S4, 4),
37
+ uuid__length(S5, 12),
38
+ uuid__hex_bytes(S1, [B1, B2, B3, B4]),
39
+ uuid__hex_bytes(S2, [B5, B6]),
40
+ uuid__hex_bytes(S3, [B7, B8]),
41
+ uuid__hex_bytes(S4, [B9, B10]),
42
+ uuid__hex_bytes(S5, [B11, B12, B13, B14, B15, B16]).
43
+
44
+ uuid__random_bytes(0, []) :- !.
45
+ uuid__random_bytes(N, [Byte|Bytes]) :-
46
+ random_integer(0, 256, Byte),
47
+ N1 is N - 1,
48
+ uuid__random_bytes(N1, Bytes).
49
+
50
+ uuid__hex_bytes([], []).
51
+ uuid__hex_bytes([High,Low|Hex], [Byte|Bytes]) :-
52
+ uuid__hex_value(High, HighValue),
53
+ uuid__hex_value(Low, LowValue),
54
+ Byte is HighValue * 16 + LowValue,
55
+ uuid__hex_bytes(Hex, Bytes).
56
+
57
+ uuid__hex_value(Char, Value) :-
58
+ nonvar(Value), !,
59
+ Value >= 0,
60
+ Value < 16,
61
+ uuid__hex_digit(Value, Char).
62
+ uuid__hex_value(Char, Value) :-
63
+ uuid__hex_digit(Value, Char).
6
64
 
7
65
  % Seed-threaded UUID version 4 generation. The version and variant nibbles are
8
66
  % fixed by RFC 9562; all other nibbles come from random/3. Reusing Seed0
@@ -52,3 +110,8 @@ uuid__hex_digit(15, f).
52
110
  uuid__append([], Ys, Ys).
53
111
  uuid__append([X|Xs], Ys, [X|Zs]) :- uuid__append(Xs, Ys, Zs).
54
112
 
113
+ uuid__length([], 0).
114
+ uuid__length([_|Xs], N) :-
115
+ N > 0,
116
+ N1 is N - 1,
117
+ uuid__length(Xs, N1).