can we use only one index array in Tarjan algorithm?

81 views Asked by At

In Tarjan algorithm, there are two index arrays, one numbers the nodes consecutively in the order in which they are discovered. the other represents the smallest index can be reachable from v through v's subtree, this is algorithm's pseudocode

    tarjan(u)
{
    DFN[u]=Low[u]=++Index                      
    Stack.push(u)                            
    for each (u, v) in E                    
        if (v is not visted)              
            tarjan(v)                
            Low[u] = min(Low[u], Low[v])
        else if (v in S)                  
            Low[u] = min(Low[u], DFN[v])
    if (DFN[u] == Low[u])                      
        repeat
            v = S.pop                  
            print v
        until (u== v)
}

but I think low array can be removed, and the algorithm changes to

tarjan(u)
{
    if(DFN[u]) // assume all elements in DFN is initialized to 0
        return DFN[u]
    DFN[u]=++Index                      
    Stack.push(u)      
    res = DFN[u]                     
    for each (u, v) in E                                          
        res = min(res, tarjan(v))
    if (DFN[u] == res)                      
        repeat
            v = S.pop                  
            print v
        until (u== v)
    return res
}

I did some test on small graph, the result is the same with standard tarjan. but I'm not sure if it can find strongly connected components in all kinds of graphs successfully. so is this algorithm right or it can only pass weak test cases.

1

There are 1 answers

1
David Eisenstat On

I agree with MrSmith42 that exhaustive enumeration of small examples is a good way to gain confidence.

Even after adding the missing return res, however, I think that your algorithm is still incorrect. Two vertex graph, 2 → 1. If we traverse 1 and then 2, the value of res for 2 will be 1, causing {2} to be missed as a strong component.