An algorithm for finding each weakly connected component of a directed graph

I am looking for an algorithm to find each loosely coupled component in a directed graph. I know that for an undirected graph you can do this via dfs, but this obviously works for a directed graph. I save my schedule as a list of offsets. For instance:

A -> B
B -> C
D -> X

So, ABC is a connected component of DX

I'm not looking for an algorithm to find highly related components !!

+4
source share
2 answers

, . a- > b, . (.. b- > a) DFS .

+3

:
- . , DFS - , , . , .

:

bimap edges
edges.putAll(graph.edges())

set vertices = graph.vertices()

list result

while !vertices.isEmpty()
    list component

    vertex a = vertices.removeAny()
    dfsTraverse(a , v -> {
        vertices.remove(v)
        component.add(v)
    })

    result.add(component)
+2

All Articles