I have encountered this cut which should return true if there exists an edge A-B or B-A for some node B of graph Graph.
node(A,Graph) :- adjacent(A,_,Graph),!.
The problem is I do not understand why removing this cut would have any effects on the returned solutions.
As I understand it the only use for a cut at the end of a Prolog statement is when there is another statement with the same name [another node(...)] which we don't want to be called if the first one succeedes. An example would be a function that takes X and Y and returns the larger one as the third parameter.
max1(X, Y, X) :- X > Y, !.
max1(_X, Y, Y).
However, there is no other statement called node(...) so I can't see how the cut could affect the solutions.
This is my code. It should find a spanning tree. It is explained in detail here. The compiler is SWI-Prolog 7.6.4 on linux.
:- op(900, fy, not).
stree1( Graph, Tree) :-
subset( Graph, Tree), tree( Tree), covers( Tree, Graph).
tree( Tree) :-
connected( Tree), not hasacycle( Tree).
connected( Graph) :-
not ( node( A, Graph), node( B, Graph), not path( A, B, Graph, _) ).
hasacycle( Graph) :-
adjacent( Node1, Node2, Graph),
path( Node1, Node2, Graph, [Node1, X, Y | _] ).
covers( Tree, Graph) :-
not ( node( Node, Graph), not node( Node, Tree) ).
subset( [], [] ).
subset( [X | Set], Subset) :- subset( Set, Subset).
subset( [X | Set], [X | Subset]) :- subset( Set, Subset).
adjacent( Node1, Node2, Graph) :-
member( Node1-Node2, Graph)
;
member( Node2-Node1, Graph).
node( Node, Graph) :- adjacent( Node, _, Graph).
path( A, Z, Graph, Path) :-
path1( A, [Z], Graph, Path).
path1( A, [A | Path1], _, [A | Path1] ).
path1( A, [Y | Path1], Graph, Path) :-
adjacent( X, Y, Graph),
not member( X, Path1),
path1( A, [X, Y | Path1], Graph, Path).
Solutions returned without cut (correct)
?- stree1([a-b, b-c, b-d, c-d], Tree).
Tree = [a-b, b-d, c-d] ;
Tree = [a-b, b-c, c-d] ;
Tree = [a-b, b-c, b-d] ;
false.
Solutions returned with cut (incorrect)
?- stree1([a-b, b-c, b-d, c-d], Tree).
Tree = [a-b] ;
Tree = [a-b, c-d] ;
Tree = [a-b, b-d] ;
Tree = [a-b, b-d, c-d] ;
Tree = [a-b, b-c] ;
Tree = [a-b, b-c, c-d] ;
Tree = [a-b, b-c, b-d] ;
false.