假设您定义了以下递归函数,返回一个成对列表的列表,其中每个成对列表都是从
from到
tolength 的路径
i:
find_paths_from_to_with_length(from, to, i): if i == 1: if to in neighbors(from) or from == to: return [[(from, to)]] return [] all_paths = [] for node in neighbors(from) + [from]: neighbor_all_paths = find_paths_from_to_with_length(node, to, i - 1) for path in neigbor_all_paths: all_paths.append([(from, node)] + neighbor_path return all_paths
然后,只需使用源,目标和所需的长度来调用它。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)