Skip to content

Commit da1d31e

Browse files
committed
updates from code-review
1 parent f440525 commit da1d31e

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/connectivity.jl

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ Fill `label` with the `id` of the connected component in the undirected graph
77
`g` to which it belongs. Return a vector representing the component assigned
88
to each vertex. The component value is the smallest vertex ID in the component.
99
10-
A `search_queue`, an empty `Vector{eltype(edgetype(g))}`, can be provided to reduce
11-
allocations if `connected_components!` is intended to be called multiple times sequentially.
12-
If not provided, it is automatically instantiated.
10+
## Optional arguments
11+
- `search_queue`, an empty `Vector{eltype(edgetype(g))}`, can be provided to avoid
12+
reallocating this work array repeatedly on repeated calls of `connected_components!`.
13+
If not provided, it is automatically instantiated.
1314
14-
### Performance
15+
## Performance
1516
This algorithm is linear in the number of edges of the graph.
1617
"""
1718
function connected_components!(
1819
label::AbstractVector{T}, g::AbstractGraph{T}, search_queue::Vector{T}=Vector{T}()
1920
) where {T}
20-
isempty(search_queue) || error("provided `search_queue` is not empty")
21+
empty!(search_queue)
2122
for u in vertices(g)
2223
label[u] != zero(T) && continue
2324
label[u] = u
@@ -152,13 +153,17 @@ end
152153
Return the number of connected components in `g`.
153154
154155
Equivalent to `length(connected_components(g))` but uses fewer allocations by not
155-
materializing the component vectors explicitly. Additionally, mutated work-arrays `label`
156-
and `search_queue` can be provided to reduce allocations further (see
157-
[`connected_components!`](@ref)).
156+
materializing the component vectors explicitly.
157+
158+
## Optional arguments
159+
Mutated work arrays, `label` and `search_queue` can be provided to avoid allocating these
160+
arrays repeatedly on repeated calls of `count_connected_components`.
161+
For `g :: AbstractGraph{T}`, `label` must be a zero-initialized `Vector{T}` of length
162+
`nv(g)` and `search_queue` a `Vector{T}`. See also [`connected_components!`](@ref).
158163
159164
## Keyword arguments
160-
- `reset_label :: Bool` (default, `false`): if `true`, `label` is reset to zero before
161-
returning.
165+
- `reset_label :: Bool` (default, `false`): if `true`, `label` is reset to a zero-vector
166+
before returning.
162167
163168
## Example
164169
```

0 commit comments

Comments
 (0)