Tarjan's Algorithm: Time Complexity and slight modification possibility

戏子无情 提交于 2019-12-02 04:11:49

1) Your first question: It can easily been done in O(1) , just maintain a boolean array inStack, the moment node n is put in the stack, flag inStack[n] to true. When you pop it off the stack, flag it back to false.

2) Not much different between w.index and w.lowlink , but this is easier to read, as we will understand that this condition is to check for the case from Node A ->B ->C ->A, which checking when node C can reach a predecessor node A or not. Keep in mind that at the moment we updating C, node A lowlink is not updated properly yet.

The Tarjan algorithm is based on the fact that a node will be the root of a SCC if and only if from that node, we cannot reach any predecessor node (which means it has the lowest lowlink in its SCC and also equals to this node's index). So the condition is just implemented this idea in the most straight forward way, if we encounter a node that is already visited, we check whether this node is a predecessor of the current node or not (which is determined by its index , also the level of this node in the graph)

Actually I figured out a way to check for w to be in S or not in constant time. Simply have a boolean field inStack.

While pushing node w in stack, make w.inStack = true and while popping it, make it false.

But the second question still remains. Can we make the slight modification without disturbing the algorithm?

  1. For the second question, 'Can we make the slight modification', my guess is you can. For the lowlink value is to indicate to a node, who has no more un-visited neighbors and which has dfs index = lowlink that it is the root of a component, and thus can pop the stack and print.

    Hence a component nodes, lowlink value can be set to any value greater than or equal to the dfs index of the components root.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!