与えられた: 任意の数のサイクルを含むことができる重み付けされていない有向グラフ (G=(E,V))。
目標: すべての頂点について、V 内のいくつかのターゲット頂点 X への最長の単純なパスが必要です
アルゴリズムのアイデア:
For each v in V
v.distanceToTarget = DepthFirstSearch(v)
Next
DepthFirstSearch(v as Vertex)
if v = target then
'Distance towards target is 0 for target itself
return 0
elseif v.isVisitedInCurrentDFSPath then
'Cycle found -> I wont find the target when I go around in cycles -> abort
return -infinity
else
'Return the maximum Distance of all Successors + 1
return max(v.Successors.ForEach(Function(v) DepthFirstSearch(v) )) + 1
end if
これはすべての場合に正しいですか?(すべての頂点からターゲットに到達できると仮定)
私のグラフのエッジの数は非常に少ないです。|E|とします。<= 3*|V| 保持します。平均時間の複雑さを計算するにはどうすればよいですか?
ありがとう!