[ create a new paste ] login | about

Link: http://codepad.org/RGKUJCCu    [ raw code | output | fork ]

Python, pasted on Aug 5:
from collections import deque

data = [
    [1, 2],
    [0, 3, 4],
    [0],
    [1, 5, 6],
    [1],
    [3],
    [3],
]
start = 0
goal = 6
def bfs(data,start, goal):
    q = deque([[start]])
    while q:
        path = q.popleft()
        node = path[-1]
        if node == goal:
            return path
        for v in data[node]:
            newPath = list(path)
            newPath.append(v)
            q.append(newPath)
r = bfs(data, start, goal)
print(r)


Output:
1
[0, 1, 3, 6]


Create a new paste based on this one


Comments: