I'm implementing an in-depth search on graphs in Prolog, I already have the following:
%arestas:
edge(c4,b4).
edge(b4,b3).
edge(b3,a3).
edge(b3,c3).
%determina que o grafo é nao direcionado
edge(V1, V2) :- edge(V2, V1).
%busca em profundidade
dfsB(Node,Solution) :- dfsB3(Node, [], Solution).
dfsB3(Node, History, [Node|History]) :- goal(Node).
dfsB3(Node,History,Solution) :- sucessor(Node, Node1), not(member(Node1,History)), dfsB3(Node1,[Node|History],Solution).
sucessor(X,Y) :- edge(X,Y). %esta certo?
In dfs
, History
is a list of nodes that have already been visited.
I'm in doubt if sucessor(X,Y)
is implemented correctly.
And when running a query dfs(c4,a3)
(which was to run because it has path) SWI-Prolog runs and does not end. So, I believe I need to narrow the depth of the search ... how can I do that?