FindShortestTour[{v1,v2,…}]
尝试找到 vi 的一种排序,使得访问所有 vi 一次的总距离最短.
FindShortestTour[graph]
尝试找到 graph 中顶点的的一种排序,使得访问每个顶点一次的总长度最短.
FindShortestTour[{v1,v2,…},j,k]
找到一个 vi 的排序,使得从 vj 到 vk 的总距离最小.
FindShortestTour[graph,s,t]
找到一个顶点的排序,使得从 s 到 t 的路径的总长度最小.
FindShortestTour[{vw,…},…]
使用规则 vw 指定图 g.
FindShortestTour[dataprop,…]
给出了 data 的属性 prop.
FindShortestTour
FindShortestTour[{v1,v2,…}]
尝试找到 vi 的一种排序,使得访问所有 vi 一次的总距离最短.
FindShortestTour[graph]
尝试找到 graph 中顶点的的一种排序,使得访问每个顶点一次的总长度最短.
FindShortestTour[{v1,v2,…},j,k]
找到一个 vi 的排序,使得从 vj 到 vk 的总距离最小.
FindShortestTour[graph,s,t]
找到一个顶点的排序,使得从 s 到 t 的路径的总长度最小.
FindShortestTour[{vw,…},…]
使用规则 vw 指定图 g.
FindShortestTour[dataprop,…]
给出了 data 的属性 prop.
更多信息和选项
- FindShortestTour 又称作旅行推销员问题(TSP).
- FindShortestTour 返回形如 {dmin,{vi1,vi2,…}} 的列表,其中 dmin 是求得的路径的长度,而 {vi1,vi2,…} 是排序.
- 在 FindShortestTour[dataprop,…] 中,prop 的可能形式包括:
-
"Elements" 最短路径的元素 "Indices" 最短路径的索引 "Length" 最短路径的距离 {prop1,prop2,…} 多种形式列表 All 元素、索引和距离的关联 - 可以给出下列选项:
-
DistanceFunction Automatic 应用于对象对的函数 Method Automatic 使用的方法 - DistanceFunction 基于 vi 的自动设置包括:
-
EuclideanDistance 数值列表的个数 EditDistance 字符串 GeoDistance 地理位置 - 对于 graph,距离被取为 GraphDistance,这是未加权图的最短路径长度,或者加权图的权值总和.
范例
打开所有单元 关闭所有单元基本范例 (3)
pts = {{1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 1}, {2, 3}, {2, 5}, {3, 1}, {3, 2}, {3, 4}, {3, 5}, {4, 1}, {4, 3}, {4, 5}, {5, 1}, {5, 2}, {5, 3}, {5, 4}};FindShortestTour[%]pts[[Last[%]]]Graphics[Line[%]]FindShortestTour[pts, 1, 5]Graphics[Line[pts[[Last[%]]]]]g = PolyhedronData["Dodecahedron", "Skeleton"];FindShortestTour[g]HighlightGraph[g, PathGraph[%[[2]]]]FindShortestTour[CountryData["Europe"] -> "Elements", Entity["Country", "Albania"], Entity["Country", "Spain"]]GeoGraphics[{Thick, Red, GeoPath[%]}]范围 (7)
FindShortestTour 适用于点列表:
FindShortestTour[{{2, 2}, {5, 2}, {5, 3}, {4, 5}, {4, 4}, {3, 2}}]FindShortestTour[{"cat", "dog", "do", "cap", "dock"}]FindShortestTour[{GeoPosition[{41, 20}], GeoPosition[{5, 20}], GeoPosition[{49, 32}], GeoPosition[{53, 28}], GeoPosition[{47, 29}]}]FindShortestTour[[image]]FindShortestTour[[image]]FindShortestTour[{1 -> 2, 1 -> 4, 2 -> 3, 2 -> 5, 3 -> 6, 4 -> 5, 4 -> 7, 5 -> 6, 5 -> 8, 6 -> 9, 7 -> 8, 7 -> 10, 8 -> 9, 8 -> 11, 9 -> 12, 10 -> 11, 11 -> 12}]FindShortestTour 作用于大规模图:
g = RandomGraph[{1000, 5000}];Timing[FindShortestTour[g]]//Short选项 (3)
DistanceFunction (3)
在默认情况下,EditDistance 用于字符串:
FindShortestTour[{"This", "finds", "the", "shortest", "tour", "through", "a", "list", "of", "strings"}]{"This", "finds", "the", "shortest", "tour", "through", "a", "list", "of", "strings"}[[%[[2]]]]这里找到经过100个点的最短路径,其中对过“河”会有相应的“惩罚”:
pts = RandomReal[1, {100, 2}];{len, tour} = FindShortestTour[pts, DistanceFunction -> ((If[(#1[[1]] ≤ .5 && #2[[1]] ≥ .5) || (#2[[1]] ≤ .5 && #1[[1]] ≥ .5), 100000, EuclideanDistance[#1, #2]])&)]Graphics[{GraphicsComplex[pts, {Point[Range[100]], Line[tour]}], Red, Line[{{0.5, 0}, {0.5, 1}}]}, Frame -> True, FrameTicks -> Automatic]d = SparseArray[{{1, 2} -> 1, {2, 1} -> 1, {6, 1} -> 1, {6, 2} -> 1, {5, 1} -> 1, {1, 5} -> 1, {2, 6} -> 1, {2, 3} -> 10, {3, 2} -> 10, {3, 5} -> 1, {5, 3} -> 1, {3, 4} -> 1, {4, 3} -> 1, {4, 5} -> 15, {4, 1} -> 1, {5, 4} -> 15, {5, 2} -> 1, {1, 4} -> 1, {2, 5} -> 1, {1, 6} -> 1}, {6, 6}, Infinity];{len, tour} = FindShortestTour[{1, 2, 3, 4, 5, 6}, DistanceFunction -> (d[[#1, #2]]&)]HighlightGraph[WeightedAdjacencyGraph[d, EdgeLabels -> "EdgeWeight"], PathGraph[tour]]应用 (3)
p = RandomReal[10, {50, 2}];FindShortestTour[p]Graphics[{Line[p[[Last[%]]]], PointSize[Medium], Red, Point[p]}]p = RandomReal[20, {100, 3}];FindShortestTour[p];Graphics3D[{Line[p[[Last[%]]]], Sphere[p, 0.5]}]europe = CountryData["Europe"]pos = GeoPosition[CountryData[#, "CenterCoordinates"]]& /@ europe;FindShortestTour[europe];GeoGraphics[{Thick, Red, GeoPath[pos[[Last[%]]]]}]monalisa = [image];monalisa = ColorQuantize[ColorConvert[monalisa, "Grayscale"], 2];pos = PixelValuePositions[ImageAdjust[monalisa], 0];res = FindShortestTour[pos];Graphics[Line[pos[[res[[2]]]]]]属性和关系 (2)
FindShortestTour 对于非哈密顿图给出空列表:
g = GraphData[{"CayleyTree", {3, 4}}]HamiltonianGraphQ[g]FindShortestTour[g]g = [image];FindShortestTour[g]FindHamiltonianPath[g]可能存在的问题 (2)
对大数据集, FindShortestTour 找出至少其长度与最小值相近的路径:
data = RandomInteger[100, {1000, 2}];FindShortestTour[data]//ShortN[%[[1]]]用 PerformanceGoal->"Quality" 找出最优结果:
FindShortestTour[data, PerformanceGoal :> "Quality"]//ShortN[%[[1]]]在 FindShortestTour 中,将规则视为无向边:
rules = {1 -> 2, 1 -> 4, 2 -> 3, 2 -> 5, 3 -> 6, 4 -> 5, 4 -> 7, 5 -> 6, 5 -> 8, 6 -> 9, 7 -> 8, 7 -> 10, 8 -> 9, 8 -> 11, 9 -> 12, 10 -> 11, 11 -> 12};FindShortestTour[rules]FindShortestTour[Graph[rules, DirectedEdges -> False]]巧妙范例 (1)
places = CountryData["Countries"];centers = Map[GeoPosition[CountryData[#, "CenterCoordinates"]]&, places];{dist, route} = FindShortestTour[centers];GeoGraphics[GeoPath[centers[[route]]], GeoRange -> "World"]r = 6371830;countryCenters = Map[First[GeoPositionXYZ[#]]&, centers[[route]]];arc[x_, y_] :=
Module[{a}, a = VectorAngle[x, y]; Table[Evaluate[RotationTransform[θ, {x, y}][x]], {θ, 0, a, a / Ceiling[10a]}]]tourLine = Apply[arc, Partition[countryCenters, 2, 1], {1}];countries = MapThread[Tooltip[Point[#1], #2[[2]]]&, {countryCenters, places[[route]]}];Graphics3D[{Sphere[{0, 0, 0}, 0.99 r], {Red, Thick, Line[tourLine]}, {Yellow, PointSize[Medium], countries}}, Boxed -> False, SphericalRegion -> True]文本
Wolfram Research (2007),FindShortestTour,Wolfram 语言函数,https://reference.wolfram.com/language/ref/FindShortestTour.html (更新于 2024 年).
CMS
Wolfram 语言. 2007. "FindShortestTour." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2024. https://reference.wolfram.com/language/ref/FindShortestTour.html.
APA
Wolfram 语言. (2007). FindShortestTour. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/FindShortestTour.html 年
BibTeX
@misc{reference.wolfram_2026_findshortesttour, author="Wolfram Research", title="{FindShortestTour}", year="2024", howpublished="\url{https://reference.wolfram.com/language/ref/FindShortestTour.html}", note=[Accessed: 03-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_findshortesttour, organization={Wolfram Research}, title={FindShortestTour}, year={2024}, url={https://reference.wolfram.com/language/ref/FindShortestTour.html}, note=[Accessed: 03-November-2026]}