我想计算带有置换的“ distance_table = []”中两个值之间的差,在这种情况下如何正确使用置换?

我想计算一个距离表中的两个值之间的差,该距离表是从一个文件读取的,一个csv文件中有许多城市,它们之间有距离.在.csv文件中,第一行是城市,
    像这样:

Barcelona;Belgrade;Berlin

接下来的行是城市之间的距离,如下所示:

0;1528.13;1497.61

1528.13;0;999.25

1497.61;999.25;0

例如,巴塞罗那到巴塞罗那的距离是0(第一个数字),巴塞罗那和贝尔格莱德的距离是1528.13(第二个),贝尔格莱德和柏林的距离是999.25.等等

我正在尝试创建一种算法来搜索像这样的文件中所有城市的最短路径.但是我将需要使用Python以及可能来自itertools的置换.

我不知道如何正确使用排列,因此我无法将不同可能性的距离加在一起.我怎样才能做到这一点?

所以我要导入置换,csv,读取文件,然后从此处开始…

from itertools import permutations
import csv

# Read data file
distance_table = []
with open('european_cities.csv') as file:
  reader = csv.reader(file,delimiter=';')
  # First row is the city names
  city_names = reader.next()
  # The rest of the rows are the distance table
  for row in reader:
    distance_table.append([float(cell) for cell in row])

因此,现在我可以例如从distance_table中看到城市A和城市B之间的距离,如下所示:

distance_table[city_A][city_B]

当我只希望每个城市出现一次时,如何遍历排列中的所有组合?

我想要例如:cityA-cityB cityB-cityC cityC-cityA
而不是:cityA-cityB cityA-cityC cityB-cityC cityC-cityA etcetra. . .

我想在这里使用不同的算法,首先是一个愚蠢的算法,以蛮横的眼光来看一下它与更智能的算法(如最短路径算法)之间的时间差异.

但是我不知道该如何穿越城市.怎么样?

解决方法:

您说您希望没有任何城市的所有排列出现两次,但是您的示例在末尾再次列出了起始城市(cityA):

I want for example: cityA-cityB + cityB-cityC + cityC-cityA

因此,假设第一个城市再次出现实际上是您的意思,我认为以下内容显示了如何生成所需城市的排列-如果该假设是错误的,只需删除复制第一个城市的那一行.

为了获得不同的总距离(三个始终相同),我添加了第四个城市并更改了输出格式,以便更紧凑以更好地容纳更多城市.

Barcelona;Belgrade;Berlin;Brussels
0;1528.13;1497.61;1346.0
1528.13;0;999.25;1723.0
1497.61;999.25;0;764.0
1346.0;1723.0;764.0;0

这是代码:

from __future__ import print_function
import csv
import functools
try:
    from itertools import izip, imap
except ImportError:  # Python 3
    izip = zip
    imap = map
from itertools import permutations, repeat

# Create a dictance dictionary from csv data file with entries like this:
#     (city_a, city_b): float(distance-between-city_a-and-city_b)
# for all pairs of city names in the file.
data_filename = 'european_cities.csv'
dist_dict = {}
with open(data_filename, 'r') as file:
    reader = csv.reader(file, delimiter=';')
    cities = next(reader)  # header row
    num_cities = len(cities)
    for city in cities:  # should be a row of distances for each city
        from_city_iter = repeat(city, num_cities)
        dist_dict.update((pair for pair in izip(izip(from_city_iter, cities),
                                                imap(float, next(reader)))
                            if pair[1])) # skip 0 distances (city_a == city_b)

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2,s3), ..."
    a, b = iter(iterable), iter(iterable)
    next(b)  # advance second iterator one iteration
    return izip(a, b)

for tour in permutations(cities, len(cities)):
    tour += (tour[0],)  # make round trip by appending starting city
    route = '->'.join(tour)
    dist = sum(dist_dict[city_a, city_b] for city_a, city_b in pairwise(tour))
    print('{:^49}: {:,}'.format(route, dist))

输出:

Barcelona->Belgrade->Berlin->Brussels->Barcelona : 4,637.38
Barcelona->Belgrade->Brussels->Berlin->Barcelona : 5,512.74
Barcelona->Berlin->Belgrade->Brussels->Barcelona : 5,565.86
Barcelona->Berlin->Brussels->Belgrade->Barcelona : 5,512.74
Barcelona->Brussels->Belgrade->Berlin->Barcelona : 5,565.86
Barcelona->Brussels->Berlin->Belgrade->Barcelona : 4,637.38
 Belgrade->Barcelona->Berlin->Brussels->Belgrade : 5,512.74
 Belgrade->Barcelona->Brussels->Berlin->Belgrade : 4,637.38
 Belgrade->Berlin->Barcelona->Brussels->Belgrade : 5,565.86
 Belgrade->Berlin->Brussels->Barcelona->Belgrade : 4,637.38
 Belgrade->Brussels->Barcelona->Berlin->Belgrade : 5,565.86
 Belgrade->Brussels->Berlin->Barcelona->Belgrade : 5,512.74
  Berlin->Barcelona->Belgrade->Brussels->Berlin  : 5,512.74
  Berlin->Barcelona->Brussels->Belgrade->Berlin  : 5,565.86
  Berlin->Belgrade->Barcelona->Brussels->Berlin  : 4,637.38
  Berlin->Belgrade->Brussels->Barcelona->Berlin  : 5,565.86
  Berlin->Brussels->Barcelona->Belgrade->Berlin  : 4,637.38
  Berlin->Brussels->Belgrade->Barcelona->Berlin  : 5,512.74
 Brussels->Barcelona->Belgrade->Berlin->Brussels : 4,637.38
 Brussels->Barcelona->Berlin->Belgrade->Brussels : 5,565.86
 Brussels->Belgrade->Barcelona->Berlin->Brussels : 5,512.74
 Brussels->Belgrade->Berlin->Barcelona->Brussels : 5,565.86
 Brussels->Berlin->Barcelona->Belgrade->Brussels : 5,512.74
 Brussels->Berlin->Belgrade->Barcelona->Brussels : 4,637.38
上一篇:Java中的递归置换产生错误的结果


下一篇:python-检查排列是否存在/组合是否唯一