Yesterday learned a new algorithm Tarjan algorithm, feel recently did not learn how to ... (recently there is a sentiment ah, is to learn certain through the practice to progress.) Now just understand why high school teacher stressed must brush questions, of course, after finishing the summary of the topic is also very important!
This Tarjan algorithm is used to find strong unicom components, in the online read a few blog, and then did a topic, feel this algorithm is nice ah ...
If you do not learn this algorithm, I will definitely want to direct DFS bar Orz ... dfs to see if each point can reach the other points of the connected component, as if this is very troublesome ah, but also to record this point from where the ... Such a thought, as if direct DFS I can not do ah ... (Orz too much food.)
Then take a look at the algorithm of the Tarjan play wild ...
The algorithm uses two arrays (low and DFN arrays) and a data structure stack and a tag array stk[] to record whether a point is in the stack, and personally think that these two arrays are the key to this algorithm! Low is used to record the subscript of the sub-tree of the current connected component (the sub-tree subscript is the smallest, because we are also starting from the subscript small point of the Dfs), DFN used to record the current point is accessed at the time, so in the subsequent operation, we do not go to modify the value of DFN (because a point of the access time of We are going to change the value of low.
The steps of the algorithm are as follows:
Step1: Initialize LOW,DFN, low[u]=dfn[u]=++time;
Step2: Press the U stack and mark it as stk[u]=1 in the stack;
Step3: A For loop to find the point v adjacent to U, here are two cases (this is the DFS simulation)
1) If V is not accessed, then go to DFS (v), if there is a ring, it will cause low[v] to be updated to the DFN value of the root of the subtree, so when DFS ends, we update low[u]=min (Low[v], low[u]), someone will ask, if there is no ring it? Without the ring is better, without the ring, V is a search from U low[v] initial value is greater than low[v] initial value, so Low[u]=min (Low[u], low[v]) does not change.
2) If V has been visited, see if it is not in the current stack, if so, the description V and U belong to a subtrees tree (then we update low[v]=min (Dfn[u], low[v]))
Step4: If a point of the adjacent edge has been searched, then jumped from the STEP3, this time to judge a point U low[u] and dfn[u] is equal, the equivalent of a strong connected component of the root, can prove that: if it is not the root, Then the low of you in Step3 has been updated to the root of the DFN, and the root DFN value is certainly smaller than low[u], so low[u] must have been updated and not equal to Dfn[u]. When Low[u] and dfn[u] are equal, we put all pops on the same point of the stack that belong to a connected component, and set the Stk[u]=0; (indicates that the current element is no longer in the stack) ... Because we are from the node under the small point to start the DFS, so a link component of the root node is definitely the node small subscript point, so that you can ensure that the pop operation can be successfully all the points of the link pop out!
Tarjan algorithm to find strong Unicom component