8

我希望你能帮我解决这个问题。

我正在尝试了解 Prolog 中的深度优先搜索算法,我遇到了以下代码

go(Start, Goal) :-
   empty_stack(Empty_been_list),
   stack(Start, Empty_been_list, Been_list),
   path(Start, Goal, Been_list).

% path implements a depth first search in PROLOG

% Current state = goal, print out been list
path(Goal, Goal, Been_list) :-
    reverse_print_stack(Been_list).

path(State, Goal, Been_list) :-
    mov(State, Next),
    % not(unsafe(Next)),
    not(member_stack(Next, Been_list)),
    stack(Next, Been_list, New_been_list),
    path(Next, Goal, New_been_list), !.

reverse_print_stack(S) :-
    empty_stack(S).
reverse_print_stack(S) :-
    stack(E, Rest, S),
    reverse_print_stack(Rest),
    write(E), nl.

我有点明白发生了什么,但我一辈子都找不到或发明一些我可以用它的事实。

请帮忙。即使这是一组非常简单的事实,我只需要从某个地方开始

先感谢您

4

2 回答 2

3

以下是 prolog 代码中使用的 DFS 示例

% solve( Node, Solution):
%    Solution is an acyclic path (in reverse order) between Node and a goal

solve( Node, Solution)  :-
  depthfirst( [], Node, Solution).

% depthfirst( Path, Node, Solution):
%   extending the path [Node | Path] to a goal gives Solution

depthfirst( Path, Node, [Node | Path] )  :-
   goal( Node).

depthfirst( Path, Node, Sol)  :-
  s( Node, Node1),
  \+ member( Node1, Path),                % Prevent a cycle
  depthfirst( [Node | Path], Node1, Sol).

depthfirst2( Node, [Node], _)  :-
   goal( Node).

depthfirst2( Node, [Node | Sol], Maxdepth)  :-
   Maxdepth > 0,
   s( Node, Node1),
   Max1 is Maxdepth - 1,
   depthfirst2( Node1, Sol, Max1).


goal(f).
goal(j).
s(a,b).
s(a,c).
s(b,d).
s(b,e).
s(c,f).
s(c,g).
s(d,h).
s(e,i).
s(e,j).

为了测试此代码,请转到 Swish SWI prolog 并将其粘贴到终端中。

然后查询代码并在右侧输入:solve(a, Sol)

解决方案将是: Sol = [j, e, b, a]

您可以通过键入以下内容来调试此代码:trace, (solve(a, Sol))。

以下是prolog代码中使用的BFS示例

前往 swish 并使用与以前相同的步骤进行查询

解决方案将是: Sol = [f, c, a]

% solve( Start, Solution):
%    Solution is a path (in reverse order) from Start to a goal

solve( Start, Solution)  :-
  breadthfirst( [ [Start] ], Solution).

% breadthfirst( [ Path1, Path2, ...], Solution):
%   Solution is an extension to a goal of one of paths

breadthfirst( [ [Node | Path] | _], [Node | Path])  :-
  goal( Node).

breadthfirst( [Path | Paths], Solution)  :-
  extend( Path, NewPaths),
  append( Paths, NewPaths, Paths1),
  breadthfirst( Paths1, Solution).

extend( [Node | Path], NewPaths)  :-
  bagof( [NewNode, Node | Path],
         ( s( Node, NewNode), \+ member( NewNode, [Node | Path] ) ),
         NewPaths),
  !.

extend( Path, [] ).              % bagof failed: Node has no successor
s(a,b).
s(a,c).
s(b,d).
s(b,e).
s(c,f).
s(c,g).
s(d,h).
s(e,i).
s(e,j).
goal(j).
goal(f).

希望这有助于理解 DFS 和 BFS

使用此图可帮助您了解树

在此处输入图像描述

于 2018-02-25T19:46:30.017 回答
0

您只需要制作描述图表中有效移动的事实。

因此,例如,如果您有节点 A、B、C 和 D,则图上的每条边都会有一个 mov() 事实。如果 A 对 B 和 C 有边,而 B 对 D 有边,你的事实将是

mov(A, B).
mov(A, C).
mov(B, D).

基本上,为从一个节点到另一个节点的每条路径绘制一个图形并写一个类似上面的事实。

于 2014-11-28T17:39:25.623 回答