Skip to content

Commit 2316b0f

Browse files
committed
allow resetting label inside count_connected_components
1 parent a8f8d19 commit 2316b0f

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/connectivity.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,20 @@ function is_connected(g::AbstractGraph{T}) where T
147147
end
148148

149149
"""
150-
count_connected_components( g, [label, search_queue])
150+
count_connected_components( g, [label, search_queue]; reset_label::Bool=false)
151151
152152
Return the number of connected components in `g`.
153153
154154
Equivalent to `length(connected_components(g))` but uses fewer allocations by not
155155
materializing the component vectors explicitly. Additionally, mutated work-arrays `label`
156156
and `search_queue` can be provided to reduce allocations further (see
157-
[`connected_components!`](@ref)).
157+
[`_connected_components!`](@ref)).
158158
159+
## Keyword arguments
160+
- `reset_label :: Bool` (default, `false`): if `true`, `label` is reset to zero before
161+
returning.
162+
163+
## Example
159164
```
160165
julia> using Graphs
161166
@@ -174,10 +179,13 @@ julia> count_connected_components(g)
174179
function count_connected_components(
175180
g::AbstractGraph{T},
176181
label::AbstractVector=zeros(T, nv(g)),
177-
search_queue::Vector{T}=Vector{T}()
182+
search_queue::Vector{T}=Vector{T}();
183+
reset_label::Bool=false
178184
) where T
179-
connected_components!(label, g, search_queue)
180-
return count_unique(label)
185+
_connected_components!(label, g, search_queue)
186+
c = count_unique(label)
187+
reset_label && fill!(label, zero(eltype(label)))
188+
return c
181189
end
182190

183191
function count_unique(label::Vector{T}) where T

0 commit comments

Comments
 (0)