Stáhnout: 3.6_17.py   Zobrazit: duálně   3.6_17.pl

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

from linked_lists import LinkedList, Cons, Nil, member, append

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

def solution(node):
    for path in breadth_first_search(LinkedList([LinkedList(node)])):
        yield path

def breadth_first_search(paths):
    if paths == Nil:
        return
    path = paths.head
    node = path.head
    if is_goal(node):
        yield path
    new_paths = Nil
    for node1 in move_anyY(node):
        if not member(node1, path):
            new_paths = Cons(Cons(node1, path), new_paths)
    if new_paths != Nil:
        paths1 = append(paths.tail, new_paths)
    else:
        paths1 = paths.tail
    for path in breadth_first_search(paths1):
        yield path

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("Breadth-First Search")
    print("Volani next(solution('A')): %s" % next(solution('A')))
    print("Vsechna reseni solution('A'):")
    for s in solution('A'):
        print s

 Stáhnout: 3.6_17.py   Zobrazit: duálně   3.6_17.pl