在骑士之旅中我在DFS中缺少什么?

时间:2015-02-06 10:20:14

标签: python algorithm recursion

我正在尝试使用DFS解决knight tour problem。我生成了我的图表(在这个例子中我有5x5矩阵):

{
  0: set([11, 7]),
  1: set([8, 10, 12]),
  2: set([9, 11, 5, 13]),
  3: set([12, 14, 6]),
  4: set([13, 7]),
  5: set([16, 2, 12]), 6: set([17, 3, 13, 15]), 7: set([0, 4, 10, 14, 16, 18]), 8: set([19, 1, 11, 17]), 9: set([2, 12, 18]), 10: set([1, 17, 21, 7]), 11: set([0, 2, 8, 18, 20, 22]), 12: set([1, 3, 5, 9, 15, 19, 21, 23]), 13: set([2, 4, 6, 16, 22, 24]), 14: set([23, 17, 3, 7]), 15: set([12, 22, 6]), 16: set([23, 7, 5, 13]), 17: set([6, 8, 10, 14, 20, 24]), 18: set([9, 11, 21, 7]), 19: set([8, 12, 22]), 20: set([17, 11]), 21: set([10, 12, 18]), 
  22: set([19, 11, 13, 15]),
  23: set([16, 12, 14]),
  24: set([17, 13])
}

然后我试图调用DFS来找到长度为25的路径(到达每个方格)。为此,我跟踪当前路径,将其与所需长度进行比较,如果没有达到,则递归地跨越所有邻居的DFS。如果没有未经检查的邻居(我们到达死胡同但仍有广场应该到达),我将从路径中删除最后一个元素。

def knightTour(current, limit, path):
    if len(path) == limit:
        return path

    path.append(current)

    neighbors = graph[current]
    if len(neighbors):
        for i in neighbors:
            if i not in set(path):
                return knightTour(i, limit, path)
    else:
        path.pop()
        return False

knightTour(0, 24, [])

我遗漏了一些显而易见的东西,因为在我的情况下,它无法找到完整的路径并被卡住[0, 11, 2, 9, 12, 1, 8, 19, 22, 13, 4, 7, 10, 17, 6, 3, 14, 23, 16]。知道我的错误在哪里?

2 个答案:

答案 0 :(得分:4)

您不会进行反向跟踪,因此您的算法最终会停留在无法正常工作的路径上(除非它运气好并且第一次得到结果)。这是一个稍微简单的实现:

def knights_tour(graph, path=None):
    if path is None:
        path = [min(graph)]
    if len(path) == len(graph):
        return path
    visited = set(path)
    for neighbour in graph[path[-1]]:
        if neighbour not in visited:
            path.append(neighbour)
            if knights_tour(graph, path):
                return path
            path.pop()

请注意,如果递归调用已返回true-y(即已找到完整路径),则此选项仅返回path,否则将删除该邻居并继续检查来自其他邻居的可能路径。

答案 1 :(得分:3)

Jon的优秀答案的补充,这是另一个更接近原始代码的版本,因此您可以看到问题确实存在:

def knightTour(current, limit, path):
    path.append(current)    # add current before returning, or the last 
    if len(path) == limit:  # node will be missing in the returned path
        return path
                            # (no need to check length)
    for i in graph[current]:
        if i not in path:   # (no point in creating a set in each iteration)
            tour = knightTour(i, limit, path)
            if tour:        # only return the path if it is not None, i.e.
                return tour # if the recusion was succesful (backtracking)
    else:
        path.pop()          # (use implicit return None)

被称为knightTour(0, 25, []),结果为[0, 11, 2, 9, 12, 1, 8, 19, 22, 13, 4, 7, 10, 21, 18, 17, 6, 3, 14, 23, 16, 5, 15, 20, 24]