Stáhnout: 3.4_13.pl  SWISH   Zobrazit: jednoduše   3.4_13.py

% nacteni:
/* ['3.4_13.pl']. */

solution(Node,Solution) :- depth_first_search([], Node,Solution).

depth_first_search(Path,Node,[Node|Path]) :- goal(Node).
depth_first_search(Path,Node,Sol) :- move(Node,Node1),
    \+ member(Node1,Path),depth_first_search([Node|Path],Node1,Sol).

% vzorova data
goal(e).
move(a,b). move(a,e). move(a,f).
move(b,c). move(f,c). move(c,d). move(d,e).

% demonstracni vypis

start:- 
    write('depth_first_search'),nl,
    write('Prvni reseni solution(a,Solution):'),nl,
    solution(a,Solution),write(Solution),nl.
?-start.


#!/usr/bin/env python
# encoding=utf-8 (pep 0263)

from linked_lists import Cons, Nil, member

def solution(node):
    for path in depth_first_search(Nil, node):
        yield path

def depth_first_search(akumulator_path, node):
    akumulator_path1 = Cons(node, akumulator_path)
    if is_goal(node):
        yield akumulator_path1
    for node1 in move_anyY(node):
        if member(node1,akumulator_path1):
            continue
        for path in depth_first_search(akumulator_path1, node1):
            yield path

def is_goal(x):
    # zavisi na resenem problemu
    return x == "E"

graph = dict(A=["B", "E", "F"],
             B=["C"], F=["C"],
             C=["D"],
             D=["E"])

def move_anyY(x):
    # zavisi na resenem problemu
    if x in graph:
        for y in graph[x]:
            yield y

# demonstracni vypis
if __name__ == "__main__":
    print("Depth-First Search")
    print("Volani next(solution('A')) : %s" % next(solution('A')))

 Stáhnout: 3.4_13.pl  SWISH   Zobrazit: jednoduše   3.4_13.py