Skip to content

Commit

Permalink
Add strongly_connected_components_tarjan (JuliaGraphs#304)
Browse files Browse the repository at this point in the history
  • Loading branch information
YingboMa committed Sep 21, 2023
1 parent e53851f commit 559404d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Graphs"
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
version = "1.8.0"
version = "1.9.0"

[deps]
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"
Expand Down
1 change: 1 addition & 0 deletions src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ export
connected_components,
strongly_connected_components,
strongly_connected_components_kosaraju,
strongly_connected_components_tarjan,
weakly_connected_components,
is_connected,
is_strongly_connected,
Expand Down
44 changes: 42 additions & 2 deletions src/connectivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,49 @@ julia> strongly_connected_components(g)
[10, 11]
```
"""
function strongly_connected_components end
strongly_connected_components(g) = strongly_connected_components_tarjan(g)

"""
strongly_connected_components_tarjan(g)
Compute the strongly connected components of a directed graph `g` using Tarjan's algorithm.
Return an array of arrays, each of which is the entire connected component.
### Implementation Notes
The returned components will be ordered reverse topologically.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph([0 1 0; 1 0 1; 0 0 0]);
julia> strongly_connected_components_tarjan(g)
2-element Vector{Vector{Int64}}:
[3]
[1, 2]
julia> g = SimpleDiGraph(11)
{11, 0} directed simple Int64 graph
julia> edge_list=[(1,2),(2,3),(3,4),(4,1),(3,5),(5,6),(6,7),(7,5),(5,8),(8,9),(9,8),(10,11),(11,10)];
julia> g = SimpleDiGraph(Edge.(edge_list))
{11, 13} directed simple Int64 graph
julia> strongly_connected_components_tarjan(g)
4-element Vector{Vector{Int64}}:
[8, 9]
[5, 6, 7]
[1, 2, 3, 4]
[10, 11]
```
"""
function strongly_connected_components_tarjan end

# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function strongly_connected_components(
@traitfn function strongly_connected_components_tarjan(
g::AG::IsDirected
) where {T<:Integer,AG<:AbstractGraph{T}}
zero_t = zero(T)
Expand Down

0 comments on commit 559404d

Please sign in to comment.