From c370e03cce85e9e9c6689f120aed61b0cc89293b Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Fri, 12 Apr 2024 12:32:09 -0400 Subject: [PATCH] Faster has_vertex (#54) --- examples/Project.toml | 3 +++ src/abstractnamedgraph.jl | 7 ++++--- src/namedgraph.jl | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 examples/Project.toml diff --git a/examples/Project.toml b/examples/Project.toml new file mode 100644 index 0000000..295df54 --- /dev/null +++ b/examples/Project.toml @@ -0,0 +1,3 @@ +[deps] +Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" +NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" diff --git a/src/abstractnamedgraph.jl b/src/abstractnamedgraph.jl index b02e9f9..75add73 100644 --- a/src/abstractnamedgraph.jl +++ b/src/abstractnamedgraph.jl @@ -12,6 +12,10 @@ parent_graph(graph::AbstractNamedGraph) = not_implemented() # ? parent_graph_type(graph::AbstractNamedGraph) = not_implemented() +function Graphs.has_vertex(graph::AbstractNamedGraph, vertex) + return not_implemented() +end + parent_vertextype(graph::AbstractNamedGraph) = vertextype(parent_graph(graph)) # Convert vertex to parent vertex @@ -137,9 +141,6 @@ function parent_edges_to_edges(graph::AbstractNamedGraph, parent_edges) return map(parent_edge_to_edge(graph), parent_edges) end -# TODO: This is `O(nv(g))`, use `haskey(vertex_to_parent_vertex(g), v)` instead? -has_vertex(g::AbstractNamedGraph, v) = v in vertices(g) - function edges(graph::AbstractNamedGraph) return parent_edges_to_edges(graph, parent_edges(graph)) end diff --git a/src/namedgraph.jl b/src/namedgraph.jl index aaaf1fd..6e52c50 100644 --- a/src/namedgraph.jl +++ b/src/namedgraph.jl @@ -11,6 +11,9 @@ vertices(graph::GenericNamedGraph) = getfield(graph, :vertices) function vertex_to_parent_vertex(graph::GenericNamedGraph, vertex) return graph.vertex_to_parent_vertex[vertex] end +function Graphs.has_vertex(graph::GenericNamedGraph, vertex) + return haskey(graph.vertex_to_parent_vertex, vertex) +end function convert_vertextype(V::Type, graph::GenericNamedGraph) return GenericNamedGraph(parent_graph(graph), convert(Vector{V}, vertices(graph)))