是否有log(n)算法将行列表更改为点列表?



我有一个以(pointA, pointB)格式表示的行列表,但尚未排序,如:(a, B),(C, D), (B, F), (F,C)到目前为止,我想把它变成一个点列表:a, B, F, C, D, a顺便说一下,可以检索行中的所有点。谢谢

对于插入和查找具有O(1)平摊平均时间复杂度的哈希映射,您可以以简单的方式在O(n)内完成。

这正是我所需要的!假设我有七:(A, B) (A、C) (D、B), (D, E), (F, G), (F, E), (G, H) (C、H),我把它转换成一个"B - D - E -"F - H -"G - C使用哈希可以快速解决问题:

def point_to_hash(point):
return str(point[0]) +','+ str(point[1])
left_hash = point_to_hash(convex_hull_lines[0][0])
points_dict = {left_hash: convex_hull_lines[0][1]}
right_hashes = [point_to_hash(convex_hull_lines[0][1])]
for line in convex_hull_lines[1:]:
if point_to_hash(line[0]) in points_dict.keys() or point_to_hash(line[1]) in right_hashes:
points_dict[point_to_hash(line[1])] = line[0]
right_hashes.append(point_to_hash(line[0]))
else:
points_dict[point_to_hash(line[0])] = line[1]
right_hashes.append(point_to_hash(line[1]))
convex_hull_points = [convex_hull_lines[0][0], convex_hull_lines[0][1]]
point_hash = point_to_hash(convex_hull_points[1])
step = 1
step_count = len(points_dict)
while step < step_count:
next_point = points_dict[point_hash]
convex_hull_points.append(next_point)
point_hash = point_to_hash(next_point)
step += 1

相关内容

最新更新