Networkx "ancestor" equivalence in Julia LightGraphs

320 Views Asked by At

Is there a LightGraph function in Julia which is equivalent to ancestors function in Networkx?

2

There are 2 best solutions below

0
sbromberger On BEST ANSWER

A possibly faster way:

function ancestors(g::SimpleDiGraph{T}, src) where T <: Integer
    reverse!(g)
    a = Vector{T}()
    for (v, d) in enumerate(gdistances(g, src))
        if d < typemax(T)
            push!(a, v)
        end
    end
    reverse!(g)
    return a
end
0
sbromberger On

Not natively, but it should be easy to approximate:

function ancestors(g, src)
    reverse!(g)
    a = reduce(union, enumerate_paths(dijkstra_shortest_paths(g, src))
    reverse!(g)
    return a
end

This will need verification and it's a bit risky in the event the function exits before the second reverse! but it's much more efficient than the non-mutating reverse().