【问题标题】:Depth First Search Algorithm Prolog深度优先搜索算法序言
【发布时间】:2014-11-21 16:17:15
【问题描述】:

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

我正在尝试学习 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.

我有点明白发生了什么,但我终其一生都无法找到或发明一些我可以使用的事实。

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

提前谢谢你

【问题讨论】:

  • Here 是此问题的通用解决方案。你直接说closure0(mov,Start,Finis)
  • 请像其他谓词一样缩进go
  • 我可能以错误的方式提出问题,因为我不理解错误的答案
  • @SeanGray:提供整个程序 - 包括mov/2 的一些事实。没有它,您的代码仍然是假设的。
  • 这就是我要问的问题:D,我想知道我可以使用哪些事实。

标签: algorithm prolog depth-first-search transitive-closure


【解决方案1】:

以下是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

使用此图帮助您理解树

【讨论】:

    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-13
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多