GraphDistanceMatrix[g]
(i, j)番目の要素が,g における頂点 i と j の間の最短経路の長さであるような行列を返す.
GraphDistanceMatrix[g,Parent]
(1,i,j)
番目の要素が i から j の最短経路の長さであり,(2,i,j)
番目の要素が i から j までの最短経路における j の先行点であるような三次元行列を返す.
GraphDistanceMatrix
GraphDistanceMatrix[g]
(i, j)番目の要素が,g における頂点 i と j の間の最短経路の長さであるような行列を返す.
GraphDistanceMatrix[g,Parent]
(1,i,j)
番目の要素が i から j の最短経路の長さであり,(2,i,j)
番目の要素が i から j までの最短経路における j の先行点であるような三次元行列を返す.
詳細とオプション
- GraphDistanceMatrixの機能はWolfram言語の組込み関数GraphDistanceMatrixで利用できるようになった.
- GraphDistanceMatrixを使うためには,まずグラフユーティリティパッケージをロードしなくてはならない.それには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]}&)]GraphDistanceMatrix[g]//MatrixFormこの関数の代わりにWolframシステムのGraphDistanceMatrixが使われるようになった:
g = WeightedAdjacencyGraph[SparseArray[{{1, 2} -> 1., {2, 3} -> 1., {1, 5} -> 1., {4, 3} -> 1., {5, 4} -> -2.}, {5, 5}, Infinity]]GraphDistanceMatrix[g]//MatrixFormスコープ (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 -> ({Arrow[#, 0.1], Text[g[[First[#2], Last[#2]]], LineScaledCoordinate[#1], Background -> White]}&)]GraphDistanceMatrix[g]//MatrixFormprd = GraphDistanceMatrix[g, Parent][[2]];
prd//MatrixFormDrop[FixedPointList[prd[[1, #]]&, 3], -1]//ReverseGraphPath[g, 1, 3]オプション (2)
Method (2)
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)法は適用できない:
GraphDistanceMatrix[g, Method -> "Dijkstra"]ワーシャル・フロイド(Floyd–Warshall)とジョンソン(Johnson)のどちらのアルゴリズムも有効である:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]GraphDistanceMatrix[g, Method -> "Johnson"]Needs["GraphUtilities`"]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]}&)]GraphDistanceMatrix[g, Method -> "Dijkstra"]ワーシャル・フロイド法は重みの負閉路を検出しないため,誤った答を出す:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]GraphDistanceMatrix[g, Method -> "Johnson"]辺の負閉路を持つグラフに対するデフォルトのアルゴリズムはジョンソン法である:
GraphDistanceMatrix[g]テクニカルノート
関連するガイド
-
▪
- グラフユーティリティパッケージ ▪
- グラフとネットワーク ▪
- グラフの可視化 ▪
- グラフ上の計算 ▪
- グラフの構築と表現 ▪
- グラフと行列 ▪
- グラフの特性と測定 ▪
- グラフの操作と変更 ▪
- ランダムグラフ ▪
- ソーシャルネットワーク分析 ▪
- グラフの特性 ▪
- 数学データ形式 ▪
- 離散数学
テキスト
Wolfram Research (2007), GraphDistanceMatrix, Wolfram言語関数, https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.
CMS
Wolfram Language. 2007. "GraphDistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html.
APA
Wolfram Language. (2007). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html
BibTeX
@misc{reference.wolfram_2026_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}", note=[Accessed: 09-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistanceMatrix.html}, note=[Accessed: 09-August-2026]}