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 3 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
8 changes: 8 additions & 0 deletions src/lib/GraphsExtensions/src/neighbors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Graphs: AbstractGraph, neighborhood_dists

function vertices_at_distance(g::AbstractGraph, vertex, distance::Int)
n_dists = neighborhood_dists(g, vertex, distance)
return map(first, filter(==(distance) ∘ last, n_dists))
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
end

next_nearest_neighbors(g::AbstractGraph, v) = vertices_at_distance(g, v, 2)
11 changes: 10 additions & 1 deletion src/lib/GraphsExtensions/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ using NamedGraphs.GraphsExtensions:
is_self_loop,
leaf_vertices,
minimum_distance_to_leaves,
next_nearest_neighbors,
non_leaf_edges,
outdegrees,
permute_vertices,
Expand All @@ -81,7 +82,8 @@ using NamedGraphs.GraphsExtensions:
tree_graph_node,
undirected_graph,
undirected_graph_type,
vertextype
vertextype,
vertices_at_distance
using Test: @test, @test_broken, @test_throws, @testset

# TODO: Still need to test:
Expand Down Expand Up @@ -579,5 +581,12 @@ using Test: @test, @test_broken, @test_throws, @testset
g′ = path_graph(4)
rem_edges!(g′, [2 => 3, 3 => 4])
@test g′ == g

#vertices at distance
L = 10
g = path_graph(L)
@test only(vertices_at_distance(g, 1, L - 1)) == L
@test only(next_nearest_neighbors(g, 1)) == 3
@test length(vertices_at_distance(g, 5, 3)) == 2
mtfishman marked this conversation as resolved.
Show resolved Hide resolved
end
end
Loading