穿越网络图,覆盖所有节点。

huangapple go评论81阅读模式
英文:

Traverse through a networkx graph covering all nodes

问题

I want to traverse through all the nodes in an undirected networkx graph given a start node. The order of nodes visited is not a concern as long as it finds an optimal path with few nodes repeated.

Example:

G = nx.Graph(4)
G.add_edges_from([(1,2),(2,3),(2,4)])

The returned optimal answer:

[1,2,3,2,4] # for start node 1
[3,2,1,2,4] # or [3,2,4,2,1] for start node 3

Thanks in advance

英文:

I want to traverse through all the nodes in an undirected networkx graph given a start node. The order of nodes visited is not a concern as long as it finds a optimal path with few nodes repeated.

Example:

G = nx.Graph(4)
G.add_edges_from([(1,2),(2,3),(2,4)])

The returned optimal answer:

[1,2,3,2,4] # for start node 1
[3,2,1,2,4] # or [3,2,4,2,1] for start node 3

Thanks in advance

答案1

得分: 0

最佳答案,没有重新访问的顶点,也没有多次遍历的边,被称为旅行推销员问题。这是一个非常困难的问题!

由于您允许重新访问顶点和多次遍历边,因此有各种近似方法可用于在合理的处理时间内获得这样的答案。

总体思路是找到最小生成树,并通过树进行深度优先搜索,必要时允许回溯 - 达到树的叶子节点时进行回溯。

没有更多关于您需要解决的特定问题的细节,我无法提供更具体的信息。您可能需要编辑您的问题以提供一些细节。

但是,您应该考虑的一件事是:您的图是否是“度量”的?也就是说,对于每个可能的三个顶点(A、B、C),总是选择两个节点之间的边比经过第三个节点更便宜。如果是这样,那么您可以使用度量近似解决旅行推销员问题。请注意,大多数模拟实际问题的图形都是度量的,而且这种近似对于大多数用途都足够好。在您的情况下,边似乎没有关联的权重,因此假设所有边的权重相同。这意味着您的图确实是度量的,您应该探索旅行推销员问题的近似解决方案。您可以从以下链接开始:http://www.cs.tufts.edu/~cowen/advanced/2002/adv-lect3.pdf

英文:

The optimal answer, with no vertices revisited and no edges traversed more than once, is called the travelling salesman problem. It is a very hard problem!

Since you are allowing revisited vertices and multiple edge traversals, there are various approximations that can be used to get such answers in a reasonable amount of processing time.

The general idea is to find the minimum spanning tree and do a depth first search through the tree, allowing backtracking when necessary - a leaf of the tree is reached.

Without more details about the particular problem you need to solve, I cannot be more specific. You should probably edit your question to provide some details.

However, one thing you should look into is: Is your graph "metric"? That is, for every possible triplet of vertices ( A,B,C ) is is ALWAYS cheaper to take the edge between two nodes rather than go via the third node. If it is, then you can use the metric approximate solution to the TSP. Note that most graphs that model real world problems will be metric, and the approximation is good enough for most uses. In your case, the edges seem to have no associated weights, so the assumption is that all edges have the same weight. That means that your graph is indeed metric and you should explore the approximate solutions to TSP. You could start with: http://www.cs.tufts.edu/~cowen/advanced/2002/adv-lect3.pdf

huangapple
  • 本文由 发表于 2023年8月5日 02:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定