3reachable(V) :- cycle(U, V), reachable(U).
4reachable(a) :- cycle(V, a).
5
6% Every node must be reachable.
7:- node(U), not reachable(U).
8
10other(U, V) :-
11 node(U), node(V), node(W),
12 edge(U, W), V \= W, cycle(U, W).
13cycle(U, V) :-
14 edge(U, V), not other(U, V).
15
17:- cycle(U, W), cycle(V, W), U \= V. 18
19
20travel_path(Start, Length, Cycle) :- path(Start, Start, Start, Length, [], Cycle).
21
22path(_, X, Y, D, Prev, [X,[D],Y|Prev]) :-
23 cycle_dist(X, Y, D).
24path(Start, X, Y, D, Prev, Cycle) :-
25 D #= D1 + D2,
26 cycle_dist(Z, Y, D1), Z \= Start,
27 path(Start, X, Z, D2, [([D1],Y)|Prev], Cycle).
28
29edge(X,Y) :- distance(X,Y,D).
30cycle_dist(U,V,D) :-
31 cycle(U,V), distance(U,V,D).
32
33node(a).
34node(b).
35node(c).
36node(d).
37
38distance(b, c, 31/10).
39distance(c, d, L) :-
40 L #> 8, L #< 21/2.
41distance(d, a, 1).
42distance(a, b, 1).
43distance(c, a, 1).
44distance(a, d, 1).
45distance(d, b, 1).
46
47
48?- travel_path(b, D, Cycle). 49
50#