diff --git a/Project.toml b/Project.toml index 54d9fa3..54c1fae 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "NamedGraphs" uuid = "678767b0-92e7-4007-89e4-4527a8725b19" authors = ["Matthew Fishman and contributors"] -version = "0.6.0" +version = "0.6.2" [deps] AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" @@ -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/README.md b/README.md index f08b0e1..80f4a88 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ julia> ] add NamedGraphs -This packages introduces graph types with named edges, which are built on top of the `Graph`/`SimpleGraph` type in the [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl) package that only have contiguous integer edges (i.e. linear indexing). +This packages introduces graph types with named vertices, which are built on top of the `Graph`/`SimpleGraph` type in the [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl) package that only have contiguous integer vertices (i.e. linear indexing). The vertex names can be strings, tuples of integers, or other unique identifiers (anything that is hashable). diff --git a/examples/README.jl b/examples/README.jl index a0a7596..b9e49ae 100644 --- a/examples/README.jl +++ b/examples/README.jl @@ -15,7 +15,7 @@ #' ## Introduction -#' This packages introduces graph types with named edges, which are built on top of the `Graph`/`SimpleGraph` type in the [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl) package that only have contiguous integer edges (i.e. linear indexing). +#' This packages introduces graph types with named vertices, which are built on top of the `Graph`/`SimpleGraph` type in the [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl) package that only have contiguous integer vertices (i.e. linear indexing). The vertex names can be strings, tuples of integers, or other unique identifiers (anything that is hashable). #' There is a supertype `AbstractNamedGraph` that defines an interface and fallback implementations of standard #' Graphs.jl operations, and two implementations: `NamedGraph` and `NamedDiGraph`. 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 diff --git a/src/steiner_tree.jl b/src/steiner_tree.jl index 1672955..ecffb05 100644 --- a/src/steiner_tree.jl +++ b/src/steiner_tree.jl @@ -9,7 +9,11 @@ using SimpleTraits: SimpleTraits, Not, @traitfn map(v -> vertex_positions(g)[v], term_vert), dist_matrix_to_position_dist_matrix(g, distmx), ) - return typeof(g)(position_tree, map(v -> ordered_vertices(g)[v], vertices(position_tree))) + tree = typeof(g)(position_tree, map(v -> ordered_vertices(g)[v], vertices(position_tree))) + for v in copy(vertices(tree)) + iszero(degree(tree, v)) && rem_vertex!(tree, v) + end + return tree end @traitfn function Graphs.steiner_tree( diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index f28f96c..eddaf0f 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -675,7 +675,8 @@ end st = steiner_tree(g, terminal_vertices) es = [(1, 2) => (1, 3), (1, 3) => (1, 4), (1, 4) => (2, 4), (2, 4) => (3, 4)] @test ne(st) == 4 - @test nv(st) == 12 + @test nv(st) == 5 + @test !any(v -> iszero(degree(st, v)), vertices(st)) for e in es @test has_edge(st, e) end