Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added iterators for dfs and bfs #163

Merged
merged 40 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
d5922fc
added iterators for dfs and bfs
kylebeggs Aug 16, 2022
fa40087
fixed exisintg identifier issue
kylebeggs Aug 17, 2022
1d513a5
added iterators for dfs and bfs
kylebeggs Aug 16, 2022
a279eca
fixed exisintg identifier issue
kylebeggs Aug 17, 2022
d6b120c
add abstract iterator state and fix first iter arg
kylebeggs Aug 22, 2022
9e941c8
iterator refactor
kylebeggs Sep 19, 2022
0ff24ef
created iterators src folder, added kruskal
kylebeggs Sep 20, 2022
1f59073
created iterators src folder, added kruskal
kylebeggs Mar 15, 2023
3a5794a
Merge branch 'JuliaGraphs:master' into master
kylebeggs Jul 8, 2023
1bcc1c1
multi source version for bfs and dfs
Tortar Jan 29, 2024
9bfafbe
fix
Tortar Jan 29, 2024
48a2512
Merge branch 'master' into master
Tortar Jan 29, 2024
72c6840
remove kruskal
Tortar Jan 29, 2024
dd63b60
Update Graphs.jl
Tortar Jan 29, 2024
c6fdf27
remove kruskal from pages
Tortar Jan 29, 2024
27aa945
format
Tortar Jan 29, 2024
b5df963
format 2
Tortar Jan 29, 2024
c14111f
format branch
Tortar Jan 29, 2024
790572e
improve perf of bfs
Tortar Jan 29, 2024
4f6bd50
optimize bfs
Tortar Jan 29, 2024
8f4223f
use copy of source nodes
Tortar Jan 29, 2024
a56ced8
Update bfs.jl
Tortar Jan 29, 2024
19a4aa6
improve dfs
Tortar Jan 29, 2024
e52f03a
Update bfs.jl
Tortar Jan 29, 2024
a96d943
use size unknown
Tortar Mar 2, 2024
f5f60cc
Update bfs.jl
Tortar Mar 2, 2024
09f1edd
Merge branch 'JuliaGraphs:master' into master
Tortar Mar 2, 2024
80c1d0f
Clean up
gdalle Mar 5, 2024
a48b4c0
Fix typo
gdalle Mar 5, 2024
971c790
Fix tests and length
gdalle Mar 5, 2024
af494f6
Remove ref
gdalle Mar 5, 2024
74c003b
Fix eltype
gdalle Mar 5, 2024
22df731
Merge branch 'JuliaGraphs:master' into master
Tortar Apr 28, 2024
9022e24
address review comments
Tortar Apr 28, 2024
c89176e
some more comments
Tortar Apr 28, 2024
9fd1a3a
Update bfs.jl
Tortar Apr 28, 2024
a3b7a9e
Update bfs.jl
Tortar Apr 28, 2024
a26a165
Update dfs.jl
Tortar Apr 28, 2024
de59874
formatting
Tortar Apr 28, 2024
10e7c6e
Apply suggestions from code review
gdalle May 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed exisintg identifier issue
  • Loading branch information
kylebeggs committed Mar 15, 2023
commit a279eca823bc5578fd45216d936604055ed78ab5
2 changes: 1 addition & 1 deletion src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export
is_cyclic, topological_sort_by_dfs, dfs_tree, dfs_parents,

# iterators
DFS, BFS, iterate,
DFSIterator, BFSIterator, iterate,

# random
randomwalk,
Expand Down
32 changes: 17 additions & 15 deletions src/traversals/iterators.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
"""
abstract type TraversalAlgorithm
abstract type IteratorAlgorithm

`TraversalAlgorithm` is an abstract type which specifies using depth-first traversal [`DFS`](@ref) or breadth-first traversal [`BFS`](@ref).
`IteratorAlgorithm` is an abstract type which specifies using depth-first traversal [`DFSIterator`](@ref) or breadth-first traversal [`BFSIterator`](@ref).
"""
abstract type TraversalAlgorithm end
abstract type IteratorAlgorithm end
kylebeggs marked this conversation as resolved.
Show resolved Hide resolved


"""
struct DFS <: TraversalAlgorithm
struct DFSIterator <: IteratorAlgorithm

`DFS` is a struct which specifies using depth-first traversal to iterate through a graph. A source node must be supplied to construct this iterator as `DFS(g::AbstractGraph, source::Int)`.

`DFSIterator` is a struct which specifies using depth-first traversal to iterate through a graph. A source node must be supplied to construct this iterator as `DFSIterator(g::AbstractGraph, source::Int)`.

# Examples
```julia-repl
julia> g = smallgraph(:house)
{5, 6} undirected simple Int64 graph

julia> for node in DFS(g, 1)
julia> for node in DFSIterator(g, 1)
display(node)
end
1
Expand All @@ -26,14 +28,14 @@ julia> for node in DFS(g, 1)
5
```
"""
struct DFS <: TraversalAlgorithm
struct DFSIterator <: IteratorAlgorithm
graph::AbstractGraph
source::Int
end


"""
struct BFS <: TraversalAlgorithm
struct BFSIterator <: IteratorAlgorithm

`BFS` is a struct which specifies using breadth-first traversal to iterate through a graph. A source node must be supplied to construct this iterator as `BFS(g::AbstractGraph, source::Int)`.

Expand All @@ -42,7 +44,7 @@ end
julia> g = smallgraph(:house)
{5, 6} undirected simple Int64 graph

julia> for node in BFS(g, 1)
julia> for node in BFSIterator(g, 1)
display(node)
end
1
Expand All @@ -52,7 +54,7 @@ julia> for node in BFS(g, 1)
5
```
"""
struct BFS <: TraversalAlgorithm
struct BFSIterator <: IteratorAlgorithm
graph::AbstractGraph
source::Int
end
Expand All @@ -70,11 +72,11 @@ end


"""
Base.iterate(t::TraversalAlgorithm)
Base.iterate(t::IteratorAlgorithm)

First iteration to visit each node.
"""
function Base.iterate(t::TraversalAlgorithm)
function Base.iterate(t::IteratorAlgorithm)
visited = falses(nv(t.graph))
visited[t.source] = true
kylebeggs marked this conversation as resolved.
Show resolved Hide resolved
state = GraphIteratorState(visited, [t.source])
Expand All @@ -83,11 +85,11 @@ end


"""
Base.iterate(t::DFS, state::GraphIteratorState)
Base.iterate(t::DFSIterator, state::GraphIteratorState)

Iterator to visit each node in a depth-first manner.
"""
function Base.iterate(t::DFS, state::GraphIteratorState)
function Base.iterate(t::DFSIterator, state::GraphIteratorState)
while !isempty(state.queue)
kylebeggs marked this conversation as resolved.
Show resolved Hide resolved
for node in outneighbors(t.graph, state.queue[end])
if !state.visited[node]
Expand All @@ -102,11 +104,11 @@ function Base.iterate(t::DFS, state::GraphIteratorState)
end

"""
Base.iterate(t::BFS, state::GraphIteratorState)
Base.iterate(t::BFSIterator, state::GraphIteratorState)

Iterator to visit each node in a breadth-first manner.
"""
function Base.iterate(t::BFS, state::GraphIteratorState)
function Base.iterate(t::BFSIterator, state::GraphIteratorState)
while !isempty(state.queue)
for node in outneighbors(t.graph, state.queue[1])
if !state.visited[node]
Expand Down
8 changes: 4 additions & 4 deletions test/traversals/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
add_edge!(g,3,1)
add_edge!(g,4,7)

@testset "DFS" begin
@testset "DFSIterator" begin
for g in testgraphs(g)
nodes_visited = fill(0, nv(g))
for (i, node) in enumerate(DFS(g, 6))
for (i, node) in enumerate(DFSIterator(g, 6))
nodes_visited[i] = node
end
@test nodes_visited[1:2] == [6, 3]
Expand All @@ -30,10 +30,10 @@
end
end

@testset "BFS" begin
@testset "BFSIterator" begin
for g in testgraphs(g)
nodes_visited = fill(0, nv(g))
for (i, node) in enumerate(BFS(g, 6))
for (i, node) in enumerate(BFSIterator(g, 6))
nodes_visited[i] = node
end
@test nodes_visited[1] == 6
Expand Down