

 This page uses content from Wikipedia. The original article was at Graph. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



Kosaraju's algorithm (also known as the Kosaraju–Sharir algorithm) is a linear time algorithm to find the strongly connected components of a directed graph. Aho, Hopcroft and Ullman credit it to an unpublished paper from 1978 by S. Rao Kosaraju. The same algorithm was independently discovered by Micha Sharir and published by him in 1981. It makes use of the fact that the transpose graph (the same graph with the direction of every edge reversed) has exactly the same strongly connected components as the original graph.


For this task consider the directed graph with these connections:

 0 -> 1
 1 -> 2
 2 -> 0
 3 -> 1,  3 -> 2,  3 -> 4
 4 -> 3,  4 -> 5
 5 -> 2,  5 -> 6
 6 -> 5
 7 -> 4, 7  -> 6, 7 -> 7

And report the kosaraju strongly connected component for each node.



References

 The article on Wikipedia.
Module Module1
 
    Function Kosaraju(g As List(Of List(Of Integer))) As List(Of Integer)
        Dim size = g.Count
        Dim vis(size - 1) As Boolean
        Dim l(size - 1) As Integer
        Dim x = size
 
        Dim t As New List(Of List(Of Integer))
        For i = 1 To size
            t.Add(New List(Of Integer))
        Next
 
        Dim visit As Action(Of Integer) = Sub(u As Integer)
                                              If Not vis(u) Then
                                                  vis(u) = True
                                                  For Each v In g(u)
                                                      visit(v)
                                                      t(v).Add(u)
                                                  Next
                                                  x -= 1
                                                  l(x) = u
                                              End If
                                          End Sub
 
        For i = 1 To size
            visit(i - 1)
        Next
        Dim c(size - 1) As Integer
 
        Dim assign As Action(Of Integer, Integer) = Sub(u As Integer, root As Integer)
                                                        If vis(u) Then
                                                            vis(u) = False
                                                            c(u) = root
                                                            For Each v In t(u)
                                                                assign(v, root)
                                                            Next
                                                        End If
                                                    End Sub
 
        For Each u In l
            assign(u, u)
        Next
 
        Return c.ToList
    End Function
 
    Sub Main()
        Dim g = New List(Of List(Of Integer)) From {
            New List(Of Integer) From {1},
            New List(Of Integer) From {2},
            New List(Of Integer) From {0},
            New List(Of Integer) From {1, 2, 4},
            New List(Of Integer) From {3, 5},
            New List(Of Integer) From {2, 6},
            New List(Of Integer) From {5},
            New List(Of Integer) From {4, 6, 7}
        }
 
        Dim output = Kosaraju(g)
        Console.WriteLine("[{0}]", String.Join(", ", output))
    End Sub
 
End Module