3

I have a question. I have a continuous undirected graph. So and I need a code in Prolog which give me a complementary graph.

for example graph:

edge(1,2).
edge(2,3).
edge(3,4).
edge(4,5).
edge(5,1).

rot(X,Y):- edge(X,Y).
rot(X,Y):- edge(Y,X).

Please, help :) thanks.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136

1 Answers1

0

This should work for issuing ground queries and also to enumerate all the possible edges.

complement(X, Y):-
  ((var(X);var(Y)) -> setof(V, Z^rot(V,Z), Vertices);true), % If either vertex is not ground, compute the set of vertices
  (ground(X) -> (once(rot(X, _)), VerticesX=[X]) ; VerticesX=Vertices), % Determine list of candidate X
  (ground(Y) -> (once(rot(Y, _)), VerticesY=[Y]) ; VerticesY=Vertices), % and candidate Y
  member(X, VerticesX),
  member(Y, VerticesY),
  X \= Y,        % Generate and test all
  \+(rot(X,Y)).  % possible candidates
gusbro
  • 22,357
  • 35
  • 46