简体   繁体   English

寻找最短距离,同时访问每个节点恰好一次

[英]Find shortest distance, while visiting every node exactly once

For practicing reasons, I am working on Advent of Code 2015, and I am stuck at day 9 .出于练习的原因,我正在编写 Advent of Code 2015,我被困在第 9 天 The goal is to find the shortest distance, while visiting every location within the graph exactly once.目标是找到最短距离,同时只访问图中的每个位置一次。 Every point is directly connected to each other and the end point must be different from the starting point.每个点都直接相互连接,终点必须与起点不同。 I have formulated a solution, but the final value is not correct, and I am not seeing the underlying problem.我已经制定了一个解决方案,但最终值不正确,我没有看到根本问题。

First, I create a graph object with the locations and the distances.首先,我创建了一个包含位置和距离的图形对象。 Then I collect every permutation of the locations into a list, and then I find and summarize the distances for each permutation.然后我将位置的每个排列收集到一个列表中,然后我找到并总结每个排列的距离。 Finally, I print out the minimum distance value, which is the solution to the exercise.最后,我打印出最小距离值,这就是练习的解决方案。

The code:编码:

from collections import defaultdict
from itertools import permutations

with open("input.txt") as file:
    input_ = file.read().split("\n")[:-1]

class Graph():
    def __init__(self):
        self.edges = defaultdict(list)
        self.weights = {}
    
    def add_edge(self, from_node, to_node, weight):
        self.edges[from_node].append(to_node)
        self.edges[to_node].append(from_node)
        self.weights[(from_node, to_node)] = weight
        self.weights[(to_node, from_node)] = weight

graph = Graph()

edges = [(i.split()[0], i.split()[2], int(i.split()[-1])) for i in input_]
for edge in edges:
    graph.add_edge(*edge)
    
loc_set = set([i[0] for i in edges])
routes = list(permutations(loc_set, len(loc_set)))

dists = []
for i in routes:
    print(i)
    dist_temp = []
    for k in range(len(i))[1:]:
        dist_temp.append(graph.weights[(i[k-1], i[k])])
    dists.append(sum(dist_temp))
    print(dist_temp)
    print(sum(dist_temp))
    
print(min(dists))

After getting an invalid value, I have manually checked some permutations and their corresponding distances, hence the prints in the code.获得无效值后,我手动检查了一些排列及其相应的距离,因此在代码中打印。

The input (copying and pasting it to notepad and saving it as input.txt should be working fine for my code):输入(将其复制并粘贴到记事本并将其另存为 input.txt 应该适用于我的代码):

Faerun to Tristram = 65
Faerun to Tambi = 129
Faerun to Norrath = 144
Faerun to Snowdin = 71
Faerun to Straylight = 137
Faerun to AlphaCentauri = 3
Faerun to Arbre = 149
Tristram to Tambi = 63
Tristram to Norrath = 4
Tristram to Snowdin = 105
Tristram to Straylight = 125
Tristram to AlphaCentauri = 55
Tristram to Arbre = 14
Tambi to Norrath = 68
Tambi to Snowdin = 52
Tambi to Straylight = 65
Tambi to AlphaCentauri = 22
Tambi to Arbre = 143
Norrath to Snowdin = 8
Norrath to Straylight = 23
Norrath to AlphaCentauri = 136
Norrath to Arbre = 115
Snowdin to Straylight = 101
Snowdin to AlphaCentauri = 84
Snowdin to Arbre = 96
Straylight to AlphaCentauri = 107
Straylight to Arbre = 14
AlphaCentauri to Arbre = 46

I am pretty sure, that there are much more refined solutions to the problem, and I am open to recommendations, since I am just a hobbyist.我很确定,这个问题有更精致的解决方案,我愿意接受建议,因为我只是一个业余爱好者。 However, I would be very happy, if we'd be able to work out the shortcomings of my approach and make it work properly.但是,如果我们能够解决我的方法的缺点并使其正常工作,我会非常高兴。

I have found the error thanks to Setonix's comment!感谢 Setonix 的评论,我发现了错误! Turns out, the approach is working (it is most probably nowhere near a nice implementation of TSP, as mentioned by Mark Ransom, but it is functional nonetheless!), I was only careless, when defining the set of locations.事实证明,这种方法是有效的(正如 Mark Ransom 所提到的,它很可能离 TSP 的一个很好的实现相去甚远,但它仍然是功能性的!),在定义位置集时,我只是粗心大意。

I have assumed that every location is present at least once in the beginning of the instruction strings.我假设每个位置在指令字符串的开头至少出现一次。 However, one location ("Arbre") was only present at the end of the instructions.但是,一个位置(“Arbre”)仅出现在说明的末尾。 Therefore, the graph was incomplete, hence the wrong outputs.因此,图表不完整,因此输出错误。

As a quick fix, I have modified the code in the following way:作为快速修复,我以以下方式修改了代码:

loc_set = set([i[0] for i in edges])

to

loc_set = list(set([i[0] for i in edges]))
loc_set.append("Arbre")

Also, turns out, that incidentally the approach was good for this exercise, since the second part asks for the longest distance, which can be found via a single additional line of code at the end:此外,事实证明,顺便说一句,该方法对本练习很有用,因为第二部分要求最长距离,可以通过最后的一行附加代码找到:

print(max(dists))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 最短路径GENERATION,在定向和加权图中具有正好k个边(编辑:仅访问每个节点一次) - Shortest path GENERATION with exactly k edges in a directed and weighted graph (edit: visit each node only once) 优化算法以查找与所有建筑物的最短距离 - Optimizing algorithm to find shortest distance to all buildings 如何找到两个正则表达式之间的最短距离 - how to find the shortest distance between two regex 我需要找到源节点和目标节点之间的最短路径(距离)。 鉴于必须包含某些节点 - I need to find the shortest path (distance) between a source node and a target node. Given that certain nodes MUST be included 在定向加权图中查找最短路径,该路径访问每个节点,对重新访问节点和边缘没有限制 - Find shortest path in directed, weighted graph that visits every node with no restrictions on revisiting nodes and edges 从源节点枚举 DAG 中的所有路径,仅访问一次 - Enumerating all paths with in a DAG from a source node, visiting only once 如何完全调用每个父项的构造函数一次? - How to invoke the constructor of every parent exactly once? 在图上执行深度优先运行时访问节点两次 - Visiting node twice while doing a depth-first run on a graph Python 最短距离,而 escaping 使用回溯的二元迷宫 - Python shortest distance while escaping a binary maze using backtracking 如何从路线(Python)找到一个位置的最短距离, - How to find Shortest distance of a location from a route (Python),
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM