Skip to content

Commit 559404d

Browse files
authored
Add strongly_connected_components_tarjan (#304)
1 parent e53851f commit 559404d

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Graphs"
22
uuid = "86223c79-3864-5bf0-83f7-82e725a168b6"
3-
version = "1.8.0"
3+
version = "1.9.0"
44

55
[deps]
66
ArnoldiMethod = "ec485272-7323-5ecc-a04f-4719b315124d"

src/Graphs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ export
204204
connected_components,
205205
strongly_connected_components,
206206
strongly_connected_components_kosaraju,
207+
strongly_connected_components_tarjan,
207208
weakly_connected_components,
208209
is_connected,
209210
is_strongly_connected,

src/connectivity.jl

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,49 @@ julia> strongly_connected_components(g)
220220
[10, 11]
221221
```
222222
"""
223-
function strongly_connected_components end
223+
strongly_connected_components(g) = strongly_connected_components_tarjan(g)
224+
225+
"""
226+
strongly_connected_components_tarjan(g)
227+
228+
Compute the strongly connected components of a directed graph `g` using Tarjan's algorithm.
229+
230+
Return an array of arrays, each of which is the entire connected component.
231+
232+
### Implementation Notes
233+
The returned components will be ordered reverse topologically.
234+
235+
# Examples
236+
```jldoctest
237+
julia> using Graphs
238+
239+
julia> g = SimpleDiGraph([0 1 0; 1 0 1; 0 0 0]);
240+
241+
julia> strongly_connected_components_tarjan(g)
242+
2-element Vector{Vector{Int64}}:
243+
[3]
244+
[1, 2]
245+
246+
julia> g = SimpleDiGraph(11)
247+
{11, 0} directed simple Int64 graph
248+
249+
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)];
250+
251+
julia> g = SimpleDiGraph(Edge.(edge_list))
252+
{11, 13} directed simple Int64 graph
253+
254+
julia> strongly_connected_components_tarjan(g)
255+
4-element Vector{Vector{Int64}}:
256+
[8, 9]
257+
[5, 6, 7]
258+
[1, 2, 3, 4]
259+
[10, 11]
260+
```
261+
"""
262+
function strongly_connected_components_tarjan end
263+
224264
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
225-
@traitfn function strongly_connected_components(
265+
@traitfn function strongly_connected_components_tarjan(
226266
g::AG::IsDirected
227267
) where {T<:Integer,AG<:AbstractGraph{T}}
228268
zero_t = zero(T)

0 commit comments

Comments
 (0)