diff --git a/Project.toml b/Project.toml index 54d9fa3..834ddb4 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/lib/GraphsExtensions/src/GraphsExtensions.jl b/src/lib/GraphsExtensions/src/GraphsExtensions.jl index f4a017d..b4d56ea 100644 --- a/src/lib/GraphsExtensions/src/GraphsExtensions.jl +++ b/src/lib/GraphsExtensions/src/GraphsExtensions.jl @@ -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") diff --git a/src/lib/GraphsExtensions/src/neighbors.jl b/src/lib/GraphsExtensions/src/neighbors.jl new file mode 100644 index 0000000..2e6de9d --- /dev/null +++ b/src/lib/GraphsExtensions/src/neighbors.jl @@ -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) diff --git a/src/lib/GraphsExtensions/test/runtests.jl b/src/lib/GraphsExtensions/test/runtests.jl index b645b64..81f7230 100644 --- a/src/lib/GraphsExtensions/test/runtests.jl +++ b/src/lib/GraphsExtensions/test/runtests.jl @@ -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, @@ -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: @@ -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