Skip to content

Commit

Permalink
fixes to tests & doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
thchr committed Mar 18, 2024
1 parent 6b49e56 commit 391cbab
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
30 changes: 16 additions & 14 deletions src/traversals/all_simple_paths.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
all_simple_paths(g, u, v; cutoff=nv(g)) --> Graphs.SimplePathIterator
Returns an iterator that generates all simple paths in the graph `g` from a source vertex
`u` to a target vertex `v` or iterable of target vertices `vs`.
Expand All @@ -13,30 +13,32 @@ The maximum path length (i.e., number of edges) is limited by the keyword argume
omitted.
## Examples
```jldoctest
julia> using Graphs
julia> g = complete_graph(4)
```jldoctest allsimplepaths; setup = :(using Graphs)
julia> g = complete_graph(4);
julia> spi = all_simple_paths(g, 1, 4)
Graphs.SimplePathIterator(1 → 4)
SimplePathIterator{SimpleGraph{Int64}}(1 → 4)
julia> collect(spi)
5-element Vector{Vector{Int64}}:
[1, 4]
[1, 3, 4]
[1, 3, 2, 4]
[1, 2, 4]
[1, 2, 3, 4]
[1, 2, 4]
[1, 3, 2, 4]
[1, 3, 4]
[1, 4]
```
We can restrict the search to paths of length less than a specified cut-off (here, 2 edges):
```jldoctest
```jldoctest allsimplepaths; setup = :(using Graphs)
julia> collect(all_simple_paths(g, 1, 4; cutoff=2))
3-element Vector{Vector{Int64}}:
[1, 2, 4]
[1, 3, 4]
[1, 4]
```
"""
function all_simple_paths(
g::AbstractGraph{T},
u::T,
g::AbstractGraph{T},
u::T,
vs;
cutoff::T=nv(g)
) where T <: Integer
Expand Down Expand Up @@ -99,7 +101,7 @@ end
Returns the next simple path in `spi`, according to a depth-first search.
"""
function Base.iterate(
spi::SimplePathIterator{T},
spi::SimplePathIterator{T},
state::SimplePathIteratorState=SimplePathIteratorState(spi)
) where T <: Integer

Expand All @@ -121,7 +123,7 @@ function Base.iterate(
continue
end

child = children[next_childe_index]
child = children[next_childe_index]
first(state.stack)[2] += 1 # move child index forward
child in state.visited && continue

Expand Down
12 changes: 5 additions & 7 deletions test/traversals/all_simple_paths.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
paths = all_simple_paths(g, 1, 4)
@test Set(p for p in paths) == Set([[1, 2, 3, 4]])
@test Set(collect(paths)) == Set([[1, 2, 3, 4]])
@test 1 == length(paths)


# single path with cutoff
g = complete_graph(4)
@test collect(all_simple_paths(g, 1, 4; cutoff=2)) == [[1, 2, 4], [1, 3, 4], [1, 4]]

# two paths
Expand All @@ -17,13 +16,12 @@
paths = all_simple_paths(g, 1, [4, 5])
@test Set(p for p in paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]])
@test Set(collect(paths)) == Set([[1, 2, 3, 4], [1, 2, 3, 5]])
@test 2 == length(paths)

# two paths with cutoff
g = path_graph(4)
add_vertex!(g)
add_edge!(g, 3, 5)
paths = all_simple_paths(g, 1, [4, 5], cutoff=3)
paths = all_simple_paths(g, 1, [4, 5]; cutoff=3)
@test Set(p for p in paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]])

# two targets in line emits two paths
Expand All @@ -47,7 +45,7 @@
add_edge!(g, 2, 3)
add_edge!(g, 3, 4)
add_edge!(g, 3, 5)
paths = all_simple_paths(g, 1, [4, 5], cutoff=3)
paths = all_simple_paths(g, 1, [4, 5]; cutoff=3)
@test Set(p for p in paths) == Set([[1, 2, 3, 4], [1, 2, 3, 5]])

# digraph with a cycle
Expand Down Expand Up @@ -107,7 +105,7 @@
[2, 6, 5, 3, 4],
])

paths = all_simple_paths(g, 2, [3, 4], cutoff=3)
paths = all_simple_paths(g, 2, [3, 4]; cutoff=3)
@test Set(p for p in paths) == Set([
[2, 3],
[2, 4, 5, 3],
Expand All @@ -117,7 +115,7 @@
[2, 6, 5, 4],
])

paths = all_simple_paths(g, 2, [3, 4], cutoff=2)
paths = all_simple_paths(g, 2, [3, 4]; cutoff=2)
@test Set(p for p in paths) == Set([
[2, 3],
[2, 4],
Expand Down

0 comments on commit 391cbab

Please sign in to comment.