Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is a very simple way to turn a depth-first search program into a breadth-first one, provided the depth-first search is directly mapped to Prolog's search. This technique is called <a href="http://en.wikipedia.org/wiki/Iterative_deepening_depth-first_search" rel="noreferrer">iterative deepening</a>.</p> <p>Simply add an additional argument to ensure that the search will only go N steps deep. So a dfs-version:</p> <pre><code>dfs(State) :- final(State). dfs(State1) :- state_transition(State1, State2), dfs(State2). </code></pre> <p>Is transformed into a bfs by adding an argument for the depth. E.g. by using <a href="/questions/tagged/successor-arithmetics" class="post-tag" title="show questions tagged &#39;successor-arithmetics&#39;" rel="tag">successor-arithmetics</a>:</p> <pre><code>bfs(State, _) :- final(State). bfs(State1, s(X)) :- state_transition(State1, State2), bfs(State2, X). </code></pre> <p>A goal <code>bfs(State,s(s(s(0))))</code> will now find all derivations requiring 3 or less steps. You still can perform dfs! Simply use <code>bfs(State,X)</code>.</p> <p>To find all derivations use <code>natural_number(X), bfs(State,X)</code>.</p> <p>Often it is useful to use a list instead of the s(X)-number. This list might contain all intermediary states or the particular transitions performed.</p> <p>You might hesitate to use this technique, because it seems to recompute a lot of intermediary states ("repeatedly expanded states"). After all, first it searches all paths with one step, then, at most two steps, then, at most three steps... However, if your problem is a search problem, the branching factor here hidden within <code>state_transition/2</code> will mitigate that overhead. To see this, consider a branching factor of 2: We only will have an overhead of a factor of two! Often, there are easy ways to regain that factor of two: E.g., by speeding up <code>state_transition/2</code> or <code>final/1</code>.</p> <p>But the biggest advantage is that it does not consume a lot of space - in contrast to naive dfs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload