GraphDistance[g,i,j]
グラフ g の頂点 i から頂点 j までの距離を返す.
GraphDistance
GraphDistance[g,i,j]
グラフ g の頂点 i から頂点 j までの距離を返す.
詳細とオプション
- GraphDistanceの機能はWolfram言語の組込み関数GraphDistanceで利用できるようになった.
- GraphDistanceを使うためには,まずグラフユーティリティパッケージをロードしなくてはならない.それにはNeeds["GraphUtilities`"]を実行する必要がある.
- GraphDistanceは1つの頂点から別の頂点へのグラフ距離を返す.i から j への経路が存在しない場合はInfinityが返される.デフォルトでは,各辺は重み1を持つものとされる.
- 次のオプションを与えることができる:
-
Weighted False 距離の計算で辺の重みを使うかどうかを指定する
例題
すべて開く すべて閉じる例 (2)
Needs["GraphUtilities`"]g = {1 -> 2, 2 -> 3, 3 -> 1, 2 -> 4, 4 -> 5, 5 -> 6, 6 -> 4};GraphPlot[g, VertexLabeling -> True, DirectedEdges -> True]GraphDistance[g, 1, 5]この関数の代わりにWolframシステムのGraphDistanceが使われるようになった:
g = Graph[{1 -> 2, 2 -> 3, 3 -> 1, 2 -> 4, 4 -> 5, 5 -> 6, 6 -> 4}]GraphDistance[g, 1, 5]スコープ (1)
Needs["GraphUtilities`"]g = {1 -> 2, 2 -> 3, 3 -> 1, 2 -> 4, 4 -> 5, 5 -> 6, 6 -> 4};GraphPlot[g, VertexLabeling -> True, DirectedEdges -> True]GraphDistance[g, 1, 5]GraphDistance[g, 4, 1]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]}&)]GraphDistance[g, 1, 3]GraphDistance[g, 1, 3, Weighted -> True]アプリケーション (1)
Needs["GraphUtilities`"]以下でノードが n 個,次数が d のランダムな有向グラフを生成する:
randomGraph[n_, d_] := SparseArray[Floor[RandomReal[{0, 1} + (d/n), {n, n}]]]頂点が100個で次数が2の200個のランダムな有向グラフのうちの頂点1と頂点100の間の平均距離を計算する:
Mean[DeleteCases[Table[
GraphDistance[randomGraph[100, 2], 1, 100],
{200}], ∞]]//NListLinePlot[Table[{degree, Mean[DeleteCases[Table[
GraphDistance[randomGraph[100, degree], 1, 100],
{200}], ∞]]}, {degree, 2, 10}], AxesOrigin -> {0, 0}, AxesLabel -> {"degree", "mean distance"}]考えられる問題 (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]}&)]辺が単位重みであると仮定すると頂点1から3までの距離は2である:
GraphDistance[g, 1, 3]GraphDistance[g, 1, 3, Weighted -> True]GraphPath[g, 1, 3]GraphPath[g, 1, 3, Weighted -> False]テクニカルノート
関連するガイド
-
▪
- グラフユーティリティパッケージ ▪
- グラフとネットワーク ▪
- グラフの可視化 ▪
- グラフ上の計算 ▪
- グラフの構築と表現 ▪
- グラフと行列 ▪
- グラフの特性と測定 ▪
- グラフの操作と変更 ▪
- ランダムグラフ ▪
- ソーシャルネットワーク分析 ▪
- グラフの特性 ▪
- 数学データ形式 ▪
- 離散数学
テキスト
Wolfram Research (2007), GraphDistance, Wolfram言語関数, https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistance.html.
CMS
Wolfram Language. 2007. "GraphDistance." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistance.html.
APA
Wolfram Language. (2007). GraphDistance. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistance.html
BibTeX
@misc{reference.wolfram_2026_graphdistance, author="Wolfram Research", title="{GraphDistance}", year="2007", howpublished="\url{https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistance.html}", note=[Accessed: 10-August-2026]}
BibLaTeX
@online{reference.wolfram_2026_graphdistance, organization={Wolfram Research}, title={GraphDistance}, year={2007}, url={https://reference.wolfram.com/language/GraphUtilities/ref/GraphDistance.html}, note=[Accessed: 10-August-2026]}