[英]Recursive possible all paths finder
For example I have points like this:例如我有这样的观点:
[[4, 5], [4, 6], [5, 6], [5, 4], [6, 5], [7, 5]]
I want to find all POSSIBLE paths.我想找到所有可能的路径。 It can start from every single point if it fits the criteria.
如果符合标准,它可以从每一个点开始。
I mean with possible is: In result array there can not be a point that doesn't touch its previous or next point.我的意思是可能是:在结果数组中,不能有一个点不触及其上一个或下一个点。
[4, 5] [7, 5] [7, 6]
is not possible because i indexes(4 and 7) are not touching. [4, 5] [7, 5] [7, 6]
是不可能的,因为 i 索引(4 和 7)没有接触。 [4, 6] [5, 4]
is not possible because j indexes(6 and 4) are not touching. [4, 6] [5, 4]
是不可能的,因为 j 个索引(6 和 4)没有接触。
At the end I try to have something like this:最后我尝试有这样的东西:
[[[4, 5], [4, 6], [5, 6]],
[[4, 6], [5, 6], [6, 6]],
[[5, 6], [4, 6], [4, 5], [5, 4], [6, 5], [7, 5]]
I tried to achieve this with recursion like this.我试图通过这样的递归来实现这一点。 This is a working python code but nothing close to what i want for now :)
这是一个有效的python代码,但与我现在想要的没有什么关系:)
points_array = [[4, 5], [4, 6], [5, 6],
[5, 4], [6, 5], [7, 5]]
possible_points = []
def find_all_possible_paths(left_points,
start_point,
current_path):
if len(left_points) == 0:
if len(current_path) > 3:
possible_points.append(current_path)
return
is_left_any_touching_point = False
for point in left_points:
#Take prev_start_point and current_point indexes
start_point_i = start_point[0]
start_point_j = start_point[1]
current_point_i = point[0]
current_point_j = point[1]
# IF POINTS ARE TOUCHING add it to list and continue recursion
if abs(current_point_i - start_point_i) \
<= 1 and abs(current_point_j - start_point_j) <= 1:
is_left_any_touching_point = True
current_path.append(point)
left_points.remove(point)
find_all_possible_paths(left_points, point, current_path)
if not is_left_any_touching_point:
if len(current_path) > 3:
possible_points.append(current_path)
return
#For every start point, try to find every possible paths (We delete start point)
for i in range(0, len(points_array) - 1):
temp_array = points_array.copy()
del temp_array[i]
find_all_possible_paths(temp_array, points_array[i], [])
print(possible_points)
If I understand your criteria, the following should work.如果我了解您的标准,则以下内容应该有效。
That said, I may misunderstand your criteria since I'm finding a lot of paths that your code does not.也就是说,我可能会误解您的标准,因为我发现了很多您的代码没有的路径。 But the paths look like they follow the stated rules.
但是这些路径看起来像是遵循规定的规则。
def path_iter(points):
point_tree = {}
for point in points:
if point[0] not in point_tree:
point_tree[point[0]] = set()
point_tree[point[0]].add(point[1])
def recur (path):
yielded = False
i, j = path[-1]
for i1 in [i-1, i, i+1]:
if i1 in point_tree:
for j1 in [j-1, j, j+1]:
if j1 in point_tree[i1] and [i1, j1] not in path:
path.append([i1, j1])
yield from recur(path)
path.pop()
yielded = True
if not yielded and 3 < len(path):
yield list(path)
for point in points:
yield from recur([point])
for path in path_iter(points_array):
print(path)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.