def roundable(a,b):
"""Returns true if a can be rounded to b at any precision"""
a = Decimal(str(a))
b = Decimal(str(b))
return a.quantize(b) == b
def close(point_1, point_2):
for a,b in zip(point_1, point_2):
if not (roundable(a,b) or roundable(b,a)):
return False
return True
Dijkstra's algorithm proceeds in a
series of rounds, where each round
establishes the shortest path from s
to some new vertex. Specifically, x
is the vertex that minimizes dist(s,
vi) + w(vi, x) over all unfinished 1
<= i <= n...
给你的向量
执行舍入的最简单方法可能是使用十进制模块:http://docs.python.org/library/decimal.html。
然后,要获得近似值,可以使用量化方法:
这种方法的优点是内置了避免舍入误差的能力。希望这是有帮助的。
编辑:
在看到你的评论之后,看起来你在试着看两个观点是否接近。这些函数可以按您的要求执行:
我不知道这是否比epsilon方法好,但实现起来相当简单。
“…使用浮点数(如上面的浮点数)没有帮助…”-为什么不呢?我不记得整数是Dijkstra的要求。你不关心边缘的长度吗?这更可能是一个浮点数,即使端点是用整数值表示的。
我引用史蒂夫·斯基纳的“算法设计手册”:
距离-不提整数。
像这样?
相关问题 更多 >
编程相关推荐