グラフ g 内の頂点間の距離の行列を返す.
GraphDistanceMatrix[g,d]
グラフ g 内で最長距離を d とした頂点間の距離の行列を返す.
GraphDistanceMatrix[{vw,…},…]
規則 vw を使ってグラフ g を指定する.
GraphDistanceMatrix
グラフ g 内の頂点間の距離の行列を返す.
GraphDistanceMatrix[g,d]
グラフ g 内で最長距離を d とした頂点間の距離の行列を返す.
GraphDistanceMatrix[{vw,…},…]
規則 vw を使ってグラフ g を指定する.
詳細とオプション
- GraphDistanceMatrixは最短経路行列としても知られている.
- GraphDistanceMatrixはSparseArrayオブジェクトか通常の行列を返す.
- 距離行列 dijの項目は頂点 viから頂点 vjまでの最短距離を与える.
- 距離行列の対角項目 diiは常に0である.
- 頂点 viから頂点 vjまでの経路が存在しない場合,項目 dijはInfinity(∞)である.
- GraphDistanceMatrix[g,d]で頂点 viから頂点 vjまでに d ステップ以下の経路が存在しない場合,項目 dijはInfinityになる.
- 頂点 viはVertexList[g]によって与えられる順序であるとみなされる.
- 重み付きグラフについては,距離は頂点 viから頂点 vjまでの,任意の経路に沿った重みの和の最低のものである.
- 使用可能なオプション
-
EdgeWeight Automatic 各辺の重み Method Automatic 使用するメソッド - Methodの使用可能な設定値には,"Dijkstra","FloydWarshall","Johnson"がある.
例題
すべて開く すべて閉じるスコープ (8)
GraphDistanceMatrixは無向グラフに使うことができる:
GraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[[image]]//MatrixFormGraphDistanceMatrix[{1 -> 2, 2 -> 3, 3 -> 1, 3 -> 4, 4 -> 5, 3 -> 5}]//MatrixFormg = GridGraph[{10, 10}];Timing[GraphDistanceMatrix[g][[All, 1]]//Dimensions]GraphDistanceを使って同じ計算をするとより時間がかかる:
Timing[Table[GraphDistance[g, 1, v], {v, VertexList[g]}]//Dimensions]GraphDistanceMatrixは大きいグラフに使うことができる:
g = GridGraph[{10, 10, 10, 10}];Timing[GraphDistanceMatrix[g][[All, 1]]//Dimensions]必要なのが1列のみのでグラフが大きい場合には,GraphDistanceを使った方が速い:
Timing[Table[GraphDistance[g, 1, v], {v, VertexList[g]}]//Dimensions]オプション (6)
Method (6)
{PetersenGraph[4, 1, VertexShapeFunction -> "Name"], PetersenGraph[4, 1, EdgeWeight -> {4, 0, 3, 1, 3, 2, 7, 8, 5, 2, 1, 6}, VertexShapeFunction -> "Name"]}Table[GraphDistanceMatrix[g]//MatrixForm, {g, %}]"UnitWeight"メソッドはすべての辺に重み1を使う:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexShapeFunction -> "Name"]GraphDistanceMatrix[%, Method -> "UnitWeight"]//MatrixForm"Dijkstra"は辺の重みが正のグラフにのみ使うことができる:
GridGraph[{2, 3}, EdgeWeight -> {5, 2, 2, 5, 1, 2, 2}, VertexShapeFunction -> "Name"]GraphDistanceMatrix[%, Method -> "Dijkstra"]//MatrixForm"FloydWarshall"は負の辺の重みを含む有向グラフに使うことができる:
WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[%, Method -> "FloydWarshall"]//MatrixForm"Johnson"は負の辺の重みを含む有向グラフに使うことができる:
WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[%, Method -> "Johnson"]//MatrixForm"Johnson"は疎なグラフでは"FloydWarshall"メソッドより速いことがある:
StarGraph[6, EdgeWeight -> RandomReal[{1, 5}, 5]]GraphDistanceMatrix[%, Method -> "Johnson"]//MatrixFormアプリケーション (2)
{a = PetersenGraph[7, 2, VertexSize -> {1 -> Medium}], b = Graph[Range[4], {12, 34}, VertexSize -> {1 -> Medium}], c = Graph[Range[3], {12, 32, 31}, VertexSize -> {1 -> Small}, EdgeStyle -> Arrowheads[Small]]}Table[Max[GraphDistanceMatrix[g][[1]]], {g, {a, b, c}}]強連結グラフの場合,結果はVertexEccentricityのものと一致する:
Table[VertexEccentricity[g, 1], {g, {a, b, c}}]g = GridGraph[{2, 3, 4}]Max /@ GraphDistanceMatrix[g]% === Table[VertexEccentricity[g, v], {v, VertexList[g]}]特性と関係 (5)
距離行列の行と列はVertexListで与えられた順に従う:
g = [image];VertexList[g]TableForm[GraphDistanceMatrix[g], TableHeadings -> {c = Style[#, Red]& /@ %, c}]PetersenGraph[5, 2]Diagonal[GraphDistanceMatrix[%]]GraphDistanceを使って距離行列を求めることができる:
g = GridGraph[{2, 3}]Table[GraphDistance[g, i, j], {i, VertexList[g]}, {j, VertexList[g]}] == GraphDistanceMatrix[g]連結グラフでは,頂点のVertexEccentricityは距離行列を使って計算できる:
g = GridGraph[{3, 4}]GraphDistanceMatrix[g][[VertexIndex[g, 1], All]]//MaxVertexEccentricity[g, 1]別々の連結要素に属する2つの頂点間の距離はInfinityである:
g = [image];ConnectedGraphQ[g]GraphDistanceMatrix[g]//MatrixForm考えられる問題 (2)
g = Graph[{23, 12}, EdgeWeight -> {1, 10}, VertexShapeFunction -> "Name"](d = GraphDistanceMatrix[g])//MatrixFormGraphDistance[g, 2, 3] == d[[2, 3]]VertexList[g]Graph等の関数を呼ぶときに頂点を明示的に並べることで問題を解決する:
h = Graph[Range[3], {23, 12}, EdgeWeight -> {1, 10}, VertexShapeFunction -> "Name"]VertexList[h]GraphDistanceMatrix[h]//MatrixFormGraphDistance[h, 2, 3] == %[[2, 3]]"BellmanFord"は有効なMethodオプションではない:
g = WeightedAdjacencyGraph[(| | | | | | |
| - | - | -- | - | - | - |
| ∞ | 2 | 5 | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | 2 | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
| ∞ | ∞ | -2 | ∞ | ∞ | 2 |
| ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
| ∞ | ∞ | ∞ | ∞ | 2 | ∞ |), VertexShapeFunction -> "Name", AbsoluteOptions[GridGraph[{2, 3}], VertexCoordinates]]GraphDistanceMatrix[g, Method -> "BellmanFord"]//MatrixForm代りに"FloydWarshall","Johnson"あるいはデフォルトで選択されるMethodを使う:
GraphDistanceMatrix[g, Method -> "FloydWarshall"]//MatrixFormGraphDistanceMatrix[g, Method -> "Johnson"]//MatrixFormGraphDistanceMatrix[g]//MatrixFormテキスト
Wolfram Research (2010), GraphDistanceMatrix, Wolfram言語関数, https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html (2015年に更新).
CMS
Wolfram Language. 2010. "GraphDistanceMatrix." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2015. https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html.
APA
Wolfram Language. (2010). GraphDistanceMatrix. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html
BibTeX
@misc{reference.wolfram_2026_graphdistancematrix, author="Wolfram Research", title="{GraphDistanceMatrix}", year="2015", howpublished="\url{https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}", note=[Accessed: 04-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistancematrix, organization={Wolfram Research}, title={GraphDistanceMatrix}, year={2015}, url={https://reference.wolfram.com/language/ref/GraphDistanceMatrix.html}, note=[Accessed: 04-November-2026]}