You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to propose an extension to the neighbors function (neighbors, inneighbors, outneighbors, all_neighbors) such that it is possible to find the adjacent nodes of a given node of a graph at a distance greater than 1.
For a starting implementation, this is a slightly modified version we use in Agents.jl to find the neighbors nodes in a graph for an arbitrary distance:
using Graphs
functionnearby_positions(graph, node::Integer, radius::Integer)
nearby =copy(nearby_positions(graph, node))
radius ==1&&return nearby
seen =Set{Int}(nearby)
push!(seen, node)
k, n =0, nv(graph)
for _ in2:radius
thislevel =@view nearby[k+1:end]
isempty(thislevel) &&return nearby
k =length(nearby)
k == n &&return nearby
for v in thislevel
for w innearby_positions(graph, v)
if w ∉ seen
push!(seen, w)
push!(nearby, w)
endendendendreturn nearby
endfunctionnearby_positions(graph, node::Int, neighbor_type::Symbol=:default,)
if neighbor_type ==:defaultneighbors(graph, node)
elseif neighbor_type ==:ininneighbors(graph, node)
elseif neighbor_type ==:outoutneighbors(graph, node)
elseall_neighbors(graph, node)
endend
Here the extension returns a new vector, I think it would be probably better to return an iterator instead. Networkx can do it with the single_shortest_path_length function
edit: sorry for the bug label, didn't notice that the issue template I used would have automatically added it
The text was updated successfully, but these errors were encountered:
I think it should be taken into account that here I used a different data structure, here I used a Set to save the visited nodes, while in #163 it is used a list of length of the numbers of nodes in the graph, which is probably better than a set at higher values of depth, but worse for lower depths.
But I think that I can start with the implementation in that PR to work out a different version of the iterator when someone calls the function with a given distance, thanks for the reference! Will investigate and open up a PR when I find the time!
I'd like to propose an extension to the
neighbors
function (neighbors
,inneighbors
,outneighbors
,all_neighbors
) such that it is possible to find the adjacent nodes of a given node of a graph at a distance greater than 1.For a starting implementation, this is a slightly modified version we use in Agents.jl to find the neighbors nodes in a graph for an arbitrary distance:
Here the extension returns a new vector, I think it would be probably better to return an iterator instead. Networkx can do it with the
single_shortest_path_length
functionedit: sorry for the bug label, didn't notice that the issue template I used would have automatically added it
The text was updated successfully, but these errors were encountered: