TravelingSalesperson.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:GoodEnoughAlgs 作者: elsander 项目源码 文件源码
def DistanceMatrix(coords):
    '''Take a set of coordinates and calculate a matrix of
    Euclidean distances between the points.'''

    # we can assume that xs and ys are the same length
    stops = coords.shape[1]
    distMat = scipy.zeros((stops, stops))

    # this will be symmetric, so we only need to calculate
    # the upper triangular
    for i in range(stops):
        for j in range(i + 1, stops):
            xdist = coords[0, i] - coords[0, j]
            ydist = coords[1, i] - coords[1, j]
            distMat[i, j] = math.sqrt(xdist**2 + ydist**2)
    # add the transpose to make it symmetric
    distMat = distMat + distMat.transpose()
    return distMat
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号