# How to get vertices in the order they are discovered in BFS?

**URL:** <https://discourse.julialang.org/t/how-to-get-vertices-in-the-order-they-are-discovered-in-bfs/53966>\
**Category:** New to Julia\
**Created:** [January 26, 2021, 10:37am UTC](https://discourse.julialang.org/t/how-to-get-vertices-in-the-order-they-are-discovered-in-bfs/53966 "2021-01-26T10:37:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlelarge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlelarge/32/20845_2.png) [@mlelarge](https://discourse.julialang.org/u/mlelarge)\
**Post date:** [January 26, 2021, 10:37am UTC](https://discourse.julialang.org/t/how-to-get-vertices-in-the-order-they-are-discovered-in-bfs/53966/1 "2021-01-26T10:37:19Z")

</div>

Hi,  
I am learning LightGraphs.jl and would like to get the list of vertices in my graph in the order they are discovered in [BFS](https://juliagraphs.org/LightGraphs.jl/stable/pathing/#LightGraphs.bfs_tree).  
Is there a simple way?

---

<div class="post-metadata">

**Author:** ![mlelarge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlelarge/32/20845_2.png) [@mlelarge](https://discourse.julialang.org/u/mlelarge)\
**Post date:** [February 1, 2021, 10:09pm UTC](https://discourse.julialang.org/t/how-to-get-vertices-in-the-order-they-are-discovered-in-bfs/53966/2 "2021-02-01T22:09:24Z")

</div>

In case, someone is interested, here is what I had in mind, **BFS as an iterator** :

```julia
using LightGraphs

struct BFS{T}
    g::AbstractGraph{T}
    list_visited::Vector{T}
    list_next::Vector{T}
end

function Base.iterate(b::BFS, state=1)
    push!(b.list_visited,state)
    if length(b.list_visited) == nv(b.g)
        return (state, 0)
    elseif length(b.list_visited) > nv(b.g)
        nothing
    else
        for v in outneighbors(b.g,state) 
            v in b.list_visited || push!(b.list_next, v)
        end
        v = popfirst!(b.list_next)
        return (state, v)
    end
end

```
