GraphPath[g, start, end]
グラフ g の頂点 start と end の間の最短経路を見付ける.
GraphPath
GraphPath[g, start, end]
グラフ g の頂点 start と end の間の最短経路を見付ける.
詳細とオプション
- GraphPathの機能はWolfram言語の組込み関数FindShortestPathで利用できるようになった.
- GraphPathを使うためには,まずグラフユーティリティパッケージをロードしなくてはならない.それにはNeeds["GraphUtilities`"]を実行する必要がある.
- 以下のオプションを使うことができる:
-
Method Automatic 最短経路を見付けるために使用するメソッド Weighted True 距離の計算において辺の重みを使うよう指定する
例題
すべて開く すべて閉じる例 (2)
Needs["GraphUtilities`"]g = SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}];GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]GraphPath[g, 1, 3]GraphPath[g, 1, 3, Weighted -> False]GraphPathの代わりにFindShortestPathが使われるようになった:
g = WeightedAdjacencyGraph[SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}, Infinity]]FindShortestPath[g, 1, 3]FindShortestPath[g, 1, 3, Method -> "UnitWeight"]オプション (1)
Method (1)
Needs["GraphUtilities`"]g0 = ToCombinatoricaGraph[{1 -> 2, 2 -> 3, 3 -> 1, 1 -> 3}];
g = SetEdgeWeights[g0, {{1, 2}, {2, 3}, {3, 1}, {1, 3}}, {-1, -2, 3, 1}];
adj = AdjacencyMatrix[g];GraphPlot[g0, MultiedgeStyle -> True, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[adj[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]辺の重みが負であるため,ダイクストラ(Dijkstra)法は適用できない:
GraphPath[g, 1, 3, Method -> "Dijkstra"]GraphPath[g, 1, 3, Method -> "BellmanFord"]GraphDistanceMatrix[g, Method -> "Johnson"]g = {{0, 5, 4, 0}, {0, 0, 3, -3}, {0, 0.0000001, 0, -1}, {-2, 0, 2, 0}};GraphPlot[{{1 -> 2, 5}, {1 -> 3, 4}, {2 -> 3, 3}, {2 -> 4, -3}, {3 -> 2, 0.0000001}, {3 -> 4, -1}, {4 -> 1, -2}, {4 -> 3, 2}}, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, .05], Text[#3, LineScaledCoordinate[#1, .3], Background -> White]}&)]GraphPath[g, 1, 4, Method -> "Dijkstra"]GraphPath[g, 1, 4, Method -> "BellmanFord"]辺の負閉路を持つグラフに対するデフォルトのアルゴリズムは,ベルマン・フォード法である:
GraphPath[g, 1, 4]特性と関係 (1)
Needs["GraphUtilities`"]g = SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -3.}, {5, 5}];GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> ({Arrow[#, 0.1], Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]GraphPath[g, 1, 3]GraphDistance[g, 1, 3, Weighted -> True]GraphDistance[g, 1, 3]考えられる問題 (1)
Needs["GraphUtilities`"]g = SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}];GraphPlot[g, VertexLabeling -> True, EdgeRenderingFunction -> ({Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1]], Arrow[#, 0.1]}&)]GraphPath[g, 1, 3, Method -> "Dijkstra"]"BellmanFord"法を使い,頂点1から3までの最短経路を見付ける:
GraphPath[g, 1, 3, Method -> "BellmanFord"]インタラクティブな例題 (1)
Needs["GraphUtilities`"]DynamicModule[{g, coord, path, edges}, g = {1 -> 2, 1 -> 3, 1 -> 17, 2 -> 5, 2 -> 15, 3 -> 4, 3 -> 19, 4 -> 5, 4 -> 20, 5 -> 13, 6 -> 7, 6 -> 8, 6 -> 18, 7 -> 10, 7 -> 11, 8 -> 9, 8 -> 16, 9 -> 10, 9 -> 14, 10 -> 12, 11 -> 13, 11 -> 20, 12 -> 13, 12 -> 15, 14 -> 15, 14 -> 17, 16 -> 17, 16 -> 19, 18 -> 19, 18 -> 20};
coord = GraphCoordinates[g];
path = GraphPath[Flatten[{g, Map[Reverse, g]}], 1, 7];
edges = Transpose[{Drop[path, -1], Drop[path, 1]}];
Manipulate[pe = Take[edges, length];
GraphPlot[g, VertexLabeling -> True, VertexCoordinateRules -> coord, EdgeRenderingFunction -> ({If[MemberQ[pe, #2] || MemberQ[pe, Reverse[#2]], Sequence@@{Thickness[0.01], Red}, Black], Line[#1]}&), ImageSize -> 300], {length, 1, Length[edges], 1}]]テクニカルノート
関連するガイド
-
▪
- グラフユーティリティパッケージ ▪
- グラフとネットワーク ▪
- グラフの可視化 ▪
- グラフ上の計算 ▪
- グラフの構築と表現 ▪
- グラフと行列 ▪
- グラフの特性と測定 ▪
- グラフの操作と変更 ▪
- ランダムグラフ ▪
- ソーシャルネットワーク分析 ▪
- グラフの特性 ▪
- 数学データ形式 ▪
- 離散数学
テキスト
Wolfram Research (2007), GraphPath, Wolfram言語関数, https://reference.wolfram.com/language/GraphUtilities/ref/GraphPath.html.
CMS
Wolfram Language. 2007. "GraphPath." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/GraphPath.html.
APA
Wolfram Language. (2007). GraphPath. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/GraphPath.html
BibTeX
@misc{reference.wolfram_2026_graphpath, author="Wolfram Research", title="{GraphPath}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/GraphPath.html}", note=[Accessed: 17-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphpath, organization={Wolfram Research}, title={GraphPath}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/GraphPath.html}, note=[Accessed: 17-August-2026]}