• Kuniyuki Iwashima's avatar
    af_unix: Save O(n) setup of Tarjan's algo. · ba31b4a4
    Kuniyuki Iwashima authored
    Before starting Tarjan's algorithm, we need to mark all vertices
    as unvisited.  We can save this O(n) setup by reserving two special
    indices (0, 1) and using two variables.
    
    The first time we link a vertex to unix_unvisited_vertices, we set
    unix_vertex_unvisited_index to index.
    
    During DFS, we can see that the index of unvisited vertices is the
    same as unix_vertex_unvisited_index.
    
    When we finalise SCC later, we set unix_vertex_grouped_index to each
    vertex's index.
    
    Then, we can know (i) that the vertex is on the stack if the index
    of a visited vertex is >= 2 and (ii) that it is not on the stack and
    belongs to a different SCC if the index is unix_vertex_grouped_index.
    
    After the whole algorithm, all indices of vertices are set as
    unix_vertex_grouped_index.
    
    Next time we start DFS, we know that all unvisited vertices have
    unix_vertex_grouped_index, and we can use unix_vertex_unvisited_index
    as the not-on-stack marker.
    
    To use the same variable in __unix_walk_scc(), we can swap
    unix_vertex_(grouped|unvisited)_index at the end of Tarjan's
    algorithm.
    Signed-off-by: default avatarKuniyuki Iwashima <kuniyu@amazon.com>
    Acked-by: default avatarPaolo Abeni <pabeni@redhat.com>
    Link: https://lore.kernel.org/r/20240325202425.60930-10-kuniyu@amazon.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
    ba31b4a4
garbage.c 17 KB