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

Add vertices_at_distance and next_nearest_neighbors #77

Merged
merged 6 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions src/lib/GraphsExtensions/src/GraphsExtensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module GraphsExtensions
include("abstractgraph.jl")
include("abstracttrees.jl")
include("boundary.jl")
include("neighbors.jl")
include("shortestpaths.jl")
include("symrcm.jl")
include("partitioning.jl")
Expand Down
10 changes: 10 additions & 0 deletions src/lib/GraphsExtensions/src/neighbors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Graphs: AbstractGraph, neighborhood_dists

function vertices_at_distance(g::AbstractGraph, vertex, n::Int)
neighborhood = [first(v) for v in neighborhood_dists(g, vertex, n)]
closer_neighborhood = [first(v) for v in neighborhood_dists(g, vertex, n - 1)]
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
iszero(n) && return neighborhood
return setdiff(neighborhood, closer_neighborhood)
end

next_nearest_neighbors(g::AbstractGraph, v) = vertices_at_distance(g, v, 2)
24 changes: 24 additions & 0 deletions test/test_graphsextensions.jl
JoeyT1994 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@eval module $(gensym())
using Graphs: AbstractGraph, neighborhood_dists
using NamedGraphs.NamedGraphGenerators: named_grid
using NamedGraphs.GraphsExtensions: next_nearest_neighbors, vertices_at_distance
using Test: @test, @testset

#TODO: Add tests for other graphs extensions
@testset "GraphsExtensions" begin
@testset "Test vertices at distance" begin
L = 10
g = named_grid((L, 1))
vstart = (1, 1)
@test only(vertices_at_distance(g, vstart, L - 1)) == (L, 1)
@test only(next_nearest_neighbors(g, vstart)) == (3, 1)

L = 9
g = named_grid((L, L))
v_middle = (ceil(Int64, L / 2), ceil(Int64, L / 2))
corners = [(L, 1), (1, L), (L, L), (1, 1)]
@test length(next_nearest_neighbors(g, v_middle)) == 8
@test issetequal(vertices_at_distance(g, v_middle, L - 1), corners)
end
end
end
Loading