给定:一个未加权的有向图 (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| 持有。我将如何计算平均时间复杂度?
谢谢!