FindShortestPath[g,s,t]
グラフ g 中のソース頂点 s からターゲット頂点 t までの最短経路を求める.
FindShortestPath[g,s,All]
異なる t に繰り返し適用できるShortestPathFunction[…]を生成する.
FindShortestPath[g,All,t]
異なる s に繰り返し適用できるShortestPathFunction[…]を生成する.
異なる s と t に繰り返し適用できるShortestPathFunction[…]を生成する.
FindShortestPath[{vw,…},…]
規則 vw を使ってグラフ g を指定する.
FindShortestPath
FindShortestPath[g,s,t]
グラフ g 中のソース頂点 s からターゲット頂点 t までの最短経路を求める.
FindShortestPath[g,s,All]
異なる t に繰り返し適用できるShortestPathFunction[…]を生成する.
FindShortestPath[g,All,t]
異なる s に繰り返し適用できるShortestPathFunction[…]を生成する.
異なる s と t に繰り返し適用できるShortestPathFunction[…]を生成する.
FindShortestPath[{vw,…},…]
規則 vw を使ってグラフ g を指定する.
詳細とオプション
- FindShortestPath[g,s,t]は s から t までの経路を与える.
- FindShortestPath[g,s]はFindShortestPath[g,s,All]に等しい.
- FindShortestPath[g]はFindShortestPath[g,All,All]に等しい.
- 重みなしのグラフの場合,辺の長さは1であるとみなされる.
- 重み付きのグラフの場合,辺の長さはその重みであるとみなされる.
- Methodを与えることもできる.次は使用可能なMethodの設定値である.
-
"BellmanFord" 正負の重みをサポート "Dijkstra" 正の重みをサポート - FindShortestPathは,無向グラフ,有向グラフ,重み付きグラフ,多重グラフ,混合グラフに用いることができる.
例題
すべて開く すべて閉じる例 (1)
スコープ (9)
指定 (7)
FindShortestPathは無向グラフに使うことができる:
GridGraph[{3, 4}, VertexSize -> {2 -> Medium, 7 -> Medium}]HighlightGraph[%, PathGraph@FindShortestPath[%, 2, 7]]g = [image];HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4], DirectedEdges -> True]]g = [image];HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4]]]g = [image];HighlightGraph[g, Subgraph[g, FindShortestPath[g, 1, 4]]]WeightedAdjacencyGraph[(| | | | |
| --- | --- | ---- | ---- |
| ∞ | 1.6 | 2 | 1.4 |
| 1.6 | ∞ | 1.9 | ∞ |
| 2 | 1.9 | ∞ | 0.62 |
| 1.4 | ∞ | 0.62 | ∞ |), VertexSize -> {2 -> Small, 4 -> Small}]HighlightGraph[%, PathGraph@FindShortestPath[%, 2, 4]]g = {12, 23, 31, 34, 45, 35};HighlightGraph[g, PathGraph[FindShortestPath[g, 1, 4], DirectedEdges -> True]]g = GridGraph[{10, 10, 10, 10}];Timing[FindShortestPath[g, 1, 100]//Length]VertexCount[g]コレクション (2)
g = GridGraph[{2, 3}, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small], VertexSize -> {1 -> Small}]spf = FindShortestPath[%, 1, All]ShortestPathFunctionをグラフのすべての頂点に適用する:
Table[spf[v], {v, VertexList[g]}]HighlightGraph[g, PathGraph[#, DirectedEdges -> True]]& /@ %g = GridGraph[{2, 3}, DirectedEdges -> True, EdgeStyle -> Arrowheads[Small], VertexSize -> {6 -> Small}]spf = FindShortestPath[%, All, 6]ShortestPathFunctionをグラフのすべての頂点に適用する:
Table[spf[v], {v, VertexList[g]}]HighlightGraph[g, PathGraph[#, DirectedEdges -> True]]& /@ %オプション (3)
Method (3)
{PetersenGraph[4, 1, VertexSize -> {1 -> Medium, 7 -> Medium}], PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexSize -> {1 -> Medium, 7 -> Medium}]}Table[HighlightGraph[g, PathGraph@FindShortestPath[g, 1, 7]], {g, %}]"UnitWeight"メソッドはすべての辺に重み1を使う:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexSize -> {1 -> Small, 5 -> Small}]HighlightGraph[%, PathGraph@FindShortestPath[%, 1, 5, Method -> "UnitWeight"]]"Dijkstra"は辺の重みが正のグラフのみに使うことができる:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexSize -> {1 -> Small, 5 -> Small}]HighlightGraph[%, PathGraph@FindShortestPath[%, 1, 5, Method -> "Dijkstra"]]アプリケーション (3)
CompleteKaryTree[6, 2]HighlightGraph[%, PathGraph@FindShortestPath[%, 12, 57]]coords = PolyhedronData["SoccerBall", "Vertices"];
{Graphics3D[Join[{Glow[Gray], PolyhedronData["SoccerBall", "GraphicsComplex"], Red}, Sphere[#, 0.3]& /@ coords[[{19, 30}]]]], g = PolyhedronData["SoccerBall", "Skeleton"]}path = PathGraph@FindShortestPath[g, 19, 30];
{HighlightGraph[g, path], Graphics3D[Join[{Glow[Gray], PolyhedronData["SoccerBall", "GraphicsComplex"], Red}, Sphere[#, 0.3]& /@ coords[[{19, 30}]], {Thick, Red, Line[coords[[VertexList[path]]]]}]]}g = GridGraph[{20, 30}]m = Graph[VertexList[g], Module[{stack = {RandomChoice[VertexList[g]]}},
Do[AnnotationValue[{g, v}, "Visited"] = False, {v, VertexList[g]}];
AnnotationValue[{g, First@stack}, "Visited"] = True;
While[Not[stack == {}],
With[{v = First@stack},
With[{adj = Cases[EdgeList[g, v_] /. {vu_ :> u, u_v :> u}, x_ /; ¬AnnotationValue[{g, x}, "Visited"]]},
If[adj == {},
stack = Rest@stack,
With[{w = RandomChoice[adj]},
PrependTo[stack, w];
AnnotationValue[{g, w}, "Visited"] = True;
Sow[vw]]]]]]]//Reap//Last//First, VertexCoordinates -> GraphEmbedding[g]];HighlightGraph[m, PathGraph@FindShortestPath[m, First@VertexList[m], Last@VertexList[m]]]関連するガイド
-
▪
- 経路,閉路,フロー ▪
- グラフとネットワーク ▪
- 離散数学 ▪
- グラフ上の計算
テキスト
Wolfram Research (2010), FindShortestPath, Wolfram言語関数, https://reference.wolfram.com/language/ref/FindShortestPath.html (2015年に更新).
CMS
Wolfram Language. 2010. "FindShortestPath." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/FindShortestPath.html.
APA
Wolfram Language. (2010). FindShortestPath. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/FindShortestPath.html
BibTeX
@misc{reference.wolfram_2026_findshortestpath, author="Wolfram Research", title="{FindShortestPath}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/FindShortestPath.html}", note=[Accessed: 12-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_findshortestpath, organization={Wolfram Research}, title={FindShortestPath}, year={2015}, url={https://reference.wolfram.com/language/ref/FindShortestPath.html}, note=[Accessed: 12-November-2026]}