From ddd13b616eb56ce31c8517f77969bd157898b9f2 Mon Sep 17 00:00:00 2001 From: Joey Date: Tue, 14 May 2024 19:41:22 -0400 Subject: [PATCH 1/6] Add nth nearest neighbors and tests --- src/lib/GraphsExtensions/src/neighbors.jl | 8 ++++++++ test/test_graphsextensions.jl | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 src/lib/GraphsExtensions/src/neighbors.jl create mode 100644 test/test_graphsextensions.jl diff --git a/src/lib/GraphsExtensions/src/neighbors.jl b/src/lib/GraphsExtensions/src/neighbors.jl new file mode 100644 index 0000000..0aa2011 --- /dev/null +++ b/src/lib/GraphsExtensions/src/neighbors.jl @@ -0,0 +1,8 @@ +using Graphs: AbstractGraph, neighborhood + +function nth_nearest_neighbors(g::AbstractGraph, v, n::Int) + isone(n) && return neighborhood(g, v, 1) + return setdiff(neighborhood(g, v, n), neighborhood(g, v, n - 1)) +end + +next_nearest_neighbors(g::AbstractGraph, v) = nth_nearest_neighbors(g, v, 2) diff --git a/test/test_graphsextensions.jl b/test/test_graphsextensions.jl new file mode 100644 index 0000000..8934e54 --- /dev/null +++ b/test/test_graphsextensions.jl @@ -0,0 +1,23 @@ +@eval module $(gensym()) +using NamedGraphs.NamedGraphGenerators: named_grid +using NamedGraphs.GraphsExtensions: next_nearest_neighbors, nth_nearest_neighbors +using Test: @test, @testset + +#TODO: Add tests for other graphs extensions +@testset "GraphsExtensions" begin + @testset "Test nth nearest neighbours" begin + L = 10 + g = named_grid((L, 1)) + vstart = (1, 1) + @test only(nth_nearest_neighbors(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(nth_nearest_neighbors(g, v_middle, L - 1), corners) + end +end +end From 92dc185968853c133100e61001f50ca6139c3bc3 Mon Sep 17 00:00:00 2001 From: Joey Date: Wed, 15 May 2024 07:51:36 -0400 Subject: [PATCH 2/6] Refactor neioghborghood -> vertices_at_distance --- src/lib/GraphsExtensions/src/GraphsExtensions.jl | 1 + src/lib/GraphsExtensions/src/neighbors.jl | 12 +++++++----- test/test_graphsextensions.jl | 9 +++++---- 3 files changed, 13 insertions(+), 9 deletions(-) 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 index 0aa2011..be6fdda 100644 --- a/src/lib/GraphsExtensions/src/neighbors.jl +++ b/src/lib/GraphsExtensions/src/neighbors.jl @@ -1,8 +1,10 @@ -using Graphs: AbstractGraph, neighborhood +using Graphs: AbstractGraph, neighborhood_dists -function nth_nearest_neighbors(g::AbstractGraph, v, n::Int) - isone(n) && return neighborhood(g, v, 1) - return setdiff(neighborhood(g, v, n), neighborhood(g, v, n - 1)) +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)] + iszero(n) && return neighborhood + return setdiff(neighborhood, closer_neighborhood) end -next_nearest_neighbors(g::AbstractGraph, v) = nth_nearest_neighbors(g, v, 2) +next_nearest_neighbors(g::AbstractGraph, v) = vertices_at_distance(g, v, 2) diff --git a/test/test_graphsextensions.jl b/test/test_graphsextensions.jl index 8934e54..6a3607d 100644 --- a/test/test_graphsextensions.jl +++ b/test/test_graphsextensions.jl @@ -1,15 +1,16 @@ @eval module $(gensym()) +using Graphs: AbstractGraph, neighborhood_dists using NamedGraphs.NamedGraphGenerators: named_grid -using NamedGraphs.GraphsExtensions: next_nearest_neighbors, nth_nearest_neighbors +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 nth nearest neighbours" begin + @testset "Test vertices at distance" begin L = 10 g = named_grid((L, 1)) vstart = (1, 1) - @test only(nth_nearest_neighbors(g, vstart, L - 1)) == (L, 1) + @test only(vertices_at_distance(g, vstart, L - 1)) == (L, 1) @test only(next_nearest_neighbors(g, vstart)) == (3, 1) L = 9 @@ -17,7 +18,7 @@ using Test: @test, @testset 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(nth_nearest_neighbors(g, v_middle, L - 1), corners) + @test issetequal(vertices_at_distance(g, v_middle, L - 1), corners) end end end From 29a491e39059d9d72ad4174a1bcbe44946ca9290 Mon Sep 17 00:00:00 2001 From: Joey Date: Wed, 15 May 2024 08:58:33 -0400 Subject: [PATCH 3/6] Move tests to graphsextensions tests. Refactor function --- src/lib/GraphsExtensions/src/neighbors.jl | 8 +++----- src/lib/GraphsExtensions/test/runtests.jl | 11 ++++++++++- test/test_graphsextensions.jl | 24 ----------------------- 3 files changed, 13 insertions(+), 30 deletions(-) delete mode 100644 test/test_graphsextensions.jl diff --git a/src/lib/GraphsExtensions/src/neighbors.jl b/src/lib/GraphsExtensions/src/neighbors.jl index be6fdda..cb62a29 100644 --- a/src/lib/GraphsExtensions/src/neighbors.jl +++ b/src/lib/GraphsExtensions/src/neighbors.jl @@ -1,10 +1,8 @@ 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)] - iszero(n) && return neighborhood - return setdiff(neighborhood, closer_neighborhood) +function vertices_at_distance(g::AbstractGraph, vertex, distance::Int) + n_dists = neighborhood_dists(g, vertex, distance) + return map(first, filter(==(distance) ∘ last, n_dists)) 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..f6533a4 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 length(vertices_at_distance(g, 5, 3)) == 2 end end diff --git a/test/test_graphsextensions.jl b/test/test_graphsextensions.jl deleted file mode 100644 index 6a3607d..0000000 --- a/test/test_graphsextensions.jl +++ /dev/null @@ -1,24 +0,0 @@ -@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 From 3c385240d64b5504ee42d1b384dd9b18f7c56896 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 15 May 2024 10:44:31 -0400 Subject: [PATCH 4/6] Update src/lib/GraphsExtensions/src/neighbors.jl --- src/lib/GraphsExtensions/src/neighbors.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/GraphsExtensions/src/neighbors.jl b/src/lib/GraphsExtensions/src/neighbors.jl index cb62a29..2e6de9d 100644 --- a/src/lib/GraphsExtensions/src/neighbors.jl +++ b/src/lib/GraphsExtensions/src/neighbors.jl @@ -1,8 +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)) + 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) From b14c63d592fc382b317830e43548762baae854fc Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 15 May 2024 10:44:37 -0400 Subject: [PATCH 5/6] Update src/lib/GraphsExtensions/test/runtests.jl --- src/lib/GraphsExtensions/test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/GraphsExtensions/test/runtests.jl b/src/lib/GraphsExtensions/test/runtests.jl index f6533a4..81f7230 100644 --- a/src/lib/GraphsExtensions/test/runtests.jl +++ b/src/lib/GraphsExtensions/test/runtests.jl @@ -587,6 +587,6 @@ using Test: @test, @test_broken, @test_throws, @testset 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 + @test issetequal(vertices_at_distance(g, 5, 3), [2, 8]) end end From 76516d9ffe6f02c4347d1f7629aa9165ef3b8072 Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Wed, 15 May 2024 11:26:11 -0400 Subject: [PATCH 6/6] Try removing Random compat entry --- Project.toml | 1 - 1 file changed, 1 deletion(-) 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"