Skip to content

Commit

Permalink
Add vertices_at_distance and next_nearest_neighbors (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeyT1994 authored May 16, 2024
1 parent 0feb539 commit a8a6d1d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
1 change: 0 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ KaHyPar = "0.3.1"
LinearAlgebra = "1.7"
Metis = "1.4"
PackageExtensionCompat = "1"
Random = "1.7"
SimpleTraits = "0.9"
SparseArrays = "1.7"
SplitApplyCombine = "1.2.2"
Expand Down
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)
vertices_and_distances = neighborhood_dists(g, vertex, distance)
return map(first, filter(==(distance) last, vertices_and_distances))
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 issetequal(vertices_at_distance(g, 5, 3), [2, 8])
end
end

0 comments on commit a8a6d1d

Please sign in to comment.