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

Function to return the adjacent nodes of a given node at a distance greater than 1 #288

Open
Tortar opened this issue Jul 27, 2023 · 3 comments · May be fixed by #389
Open

Function to return the adjacent nodes of a given node at a distance greater than 1 #288

Tortar opened this issue Jul 27, 2023 · 3 comments · May be fixed by #389
Labels
enhancement New feature or request

Comments

@Tortar
Copy link
Contributor

Tortar commented Jul 27, 2023

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

function nearby_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 _ in 2:radius
        thislevel = @view nearby[k+1:end]
        isempty(thislevel) && return nearby
        k = length(nearby)
        k == n && return nearby
    	for v in thislevel
    	    for w in nearby_positions(graph, v)
    	        if w  seen
    	            push!(seen, w)
    	            push!(nearby, w)
    	        end
    	    end
    	end
    end
    return nearby
end

function nearby_positions(graph, node::Int, neighbor_type::Symbol = :default,)
    if neighbor_type == :default
        neighbors(graph, node)
    elseif neighbor_type == :in
        inneighbors(graph, node)
    elseif neighbor_type == :out
        outneighbors(graph, node)
    else
        all_neighbors(graph, node)
    end
end

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

@Tortar Tortar added the bug Something isn't working label Jul 27, 2023
@gdalle gdalle added enhancement New feature or request and removed bug Something isn't working labels Jul 27, 2023
@gdalle
Copy link
Member

gdalle commented Aug 28, 2023

This sounds cool, does it differ much from a generic BFS iterator? I think we don't have that at the moment (see #163)

@Tortar
Copy link
Contributor Author

Tortar commented Aug 31, 2023

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!

@gdalle
Copy link
Member

gdalle commented Sep 6, 2023

Cool, thanks :) no rush anyway, as you can see we're a bit behind on reviews

@Tortar Tortar linked a pull request Jun 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants