Skip to content

Commit

Permalink
Renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman committed Apr 24, 2024
1 parent d54f773 commit 8b29f39
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 55 deletions.
6 changes: 3 additions & 3 deletions ext/NamedGraphsGraphsFlowsExt/NamedGraphsGraphsFlowsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using NamedGraphs:
dist_matrix_to_parent_dist_matrix,
ordinal_graph,
parent_vertices_to_vertices,
vertex_to_parent_vertex
vertex_to_ordinal_vertex
using NamedGraphs.GraphsExtensions: GraphsExtensions, directed_graph
using SimpleTraits: SimpleTraits, @traitfn

Expand All @@ -28,8 +28,8 @@ end
)
parent_part1, parent_part2, flow = GraphsFlows.mincut(
directed_graph(ordinal_graph(graph)),
vertex_to_parent_vertex(graph, source),
vertex_to_parent_vertex(graph, target),
vertex_to_ordinal_vertex(graph, source),
vertex_to_ordinal_vertex(graph, target),
dist_matrix_to_parent_dist_matrix(graph, capacity_matrix),
algorithm,
)
Expand Down
52 changes: 26 additions & 26 deletions src/abstractnamedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ function GraphsExtensions.permute_vertices(graph::AbstractNamedGraph, permutatio
end

# Convert vertex to parent vertex
# Inverse map of `parent_vertex_to_vertex`.
vertex_to_parent_vertex(graph::AbstractNamedGraph, vertex) = not_implemented()
# Inverse map of `ordered_vertices`.
vertex_to_ordinal_vertex(graph::AbstractNamedGraph, vertex) = not_implemented()

# Convert parent vertex to vertex.
# Use `vertices`, assumes `vertices` is indexed by a parent vertex (a Vector for linear indexed parent vertices, a dictionary in general).
parent_vertex_to_vertex(graph::AbstractNamedGraph, parent_vertex) = not_implemented()
ordered_vertices(graph::AbstractNamedGraph, parent_vertex) = not_implemented()

Graphs.edgetype(graph::AbstractNamedGraph) = not_implemented()

Expand Down Expand Up @@ -111,11 +111,11 @@ parent_vertextype(graph::AbstractNamedGraph) = vertextype(ordinal_graph(graph))
Graphs.SimpleDiGraph(graph::AbstractNamedGraph) = SimpleDiGraph(ordinal_graph(graph))

# Convenient shorthands for using in higher order functions like `map`.
function vertex_to_parent_vertex(graph::AbstractNamedGraph)
return Base.Fix1(vertex_to_parent_vertex, graph)
function vertex_to_ordinal_vertex(graph::AbstractNamedGraph)
return Base.Fix1(vertex_to_ordinal_vertex, graph)
end
function parent_vertex_to_vertex(graph::AbstractNamedGraph)
return Base.Fix1(parent_vertex_to_vertex, graph)
function ordered_vertices(graph::AbstractNamedGraph)
return Base.Fix1(ordered_vertices, graph)
end

Base.zero(G::Type{<:AbstractNamedGraph}) = G()
Expand All @@ -136,15 +136,15 @@ Base.eltype(graph::AbstractNamedGraph) = eltype(vertices(graph))
parent_eltype(graph::AbstractNamedGraph) = eltype(ordinal_graph(graph))

function vertices_to_parent_vertices(graph::AbstractNamedGraph, vertices)
return map(vertex_to_parent_vertex(graph), vertices)
return map(vertex_to_ordinal_vertex(graph), vertices)
end

function vertices_to_parent_vertices(graph::AbstractNamedGraph)
return Base.Fix1(vertices_to_parent_vertices, graph)
end

function parent_vertices_to_vertices(graph::AbstractNamedGraph, parent_vertices)
return map(parent_vertex_to_vertex(graph), parent_vertices)
return map(ordered_vertices(graph), parent_vertices)
end

function parent_vertices_to_vertices(graph::AbstractNamedGraph)
Expand All @@ -156,8 +156,8 @@ parent_edges(graph::AbstractNamedGraph) = edges(ordinal_graph(graph))
parent_edgetype(graph::AbstractNamedGraph) = edgetype(ordinal_graph(graph))

function edge_to_parent_edge(graph::AbstractNamedGraph, edge::AbstractEdge)
parent_src = vertex_to_parent_vertex(graph, src(edge))
parent_dst = vertex_to_parent_vertex(graph, dst(edge))
parent_src = vertex_to_ordinal_vertex(graph, src(edge))
parent_dst = vertex_to_ordinal_vertex(graph, dst(edge))
return parent_edgetype(graph)(parent_src, parent_dst)
end

Expand All @@ -172,8 +172,8 @@ function edges_to_parent_edges(graph::AbstractNamedGraph, edges)
end

function parent_edge_to_edge(graph::AbstractNamedGraph, parent_edge::AbstractEdge)
source = parent_vertex_to_vertex(graph, src(parent_edge))
destination = parent_vertex_to_vertex(graph, dst(parent_edge))
source = ordered_vertices(graph, src(parent_edge))
destination = ordered_vertices(graph, dst(parent_edge))
return edgetype(graph)(source, destination)
end

Expand All @@ -200,13 +200,13 @@ for f in [
]
@eval begin
function $f(graph::AbstractNamedGraph, vertex)
parent_vertices = $f(ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex))
parent_vertices = $f(ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex))
return parent_vertices_to_vertices(graph, parent_vertices)
end

# Ambiguity errors with Graphs.jl
function $f(graph::AbstractNamedGraph, vertex::Integer)
parent_vertices = $f(ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex))
parent_vertices = $f(ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex))
return parent_vertices_to_vertices(graph, parent_vertices)
end
end
Expand Down Expand Up @@ -262,10 +262,10 @@ function namedgraph_neighborhood(
)
parent_distmx = dist_matrix_to_parent_dist_matrix(graph, distmx)
parent_vertices = neighborhood(
ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex), d, parent_distmx; dir
ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex), d, parent_distmx; dir
)
return [
parent_vertex_to_vertex(graph, parent_vertex) for parent_vertex in parent_vertices
ordered_vertices(graph, parent_vertex) for parent_vertex in parent_vertices
]
end

Expand All @@ -292,10 +292,10 @@ end
function namedgraph_neighborhood_dists(graph::AbstractNamedGraph, vertex, d, distmx; dir)
parent_distmx = dist_matrix_to_parent_dist_matrix(graph, distmx)
parent_vertices_and_dists = neighborhood_dists(
ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex), d, parent_distmx; dir
ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex), d, parent_distmx; dir
)
return [
(parent_vertex_to_vertex(graph, parent_vertex), dist) for
(ordered_vertices(graph, parent_vertex), dist) for
(parent_vertex, dist) in parent_vertices_and_dists
]
end
Expand Down Expand Up @@ -341,7 +341,7 @@ function GraphsExtensions.partitioned_vertices(
vertex_partitions = partitioned_vertices(
ordinal_graph(g); npartitions, nvertices_per_partition, kwargs...
)
#[inv(vertex_to_parent_vertex(g))[v] for v in partitions]
#[inv(vertex_to_ordinal_vertex(g))[v] for v in partitions]
# TODO: output the reverse of this dictionary (a Vector of Vector
# of the vertices in each partition).
# return Dictionary(vertices(g), partitions)
Expand All @@ -362,8 +362,8 @@ function namedgraph_a_star(
parent_distmx = dist_matrix_to_parent_dist_matrix(graph, distmx)
parent_shortest_path = a_star(
ordinal_graph(graph),
vertex_to_parent_vertex(graph, source),
vertex_to_parent_vertex(graph, destination),
vertex_to_ordinal_vertex(graph, source),
vertex_to_ordinal_vertex(graph, destination),
dist_matrix_to_parent_dist_matrix(graph, distmx),
heuristic,
SimpleEdge,
Expand All @@ -387,7 +387,7 @@ function Graphs.spfa_shortest_paths(
)
parent_distmx = dist_matrix_to_parent_dist_matrix(graph, distmx)
parent_shortest_paths = spfa_shortest_paths(
ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex), parent_distmx
ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex), parent_distmx
)
return Dictionary(vertices(graph), parent_shortest_paths)
end
Expand Down Expand Up @@ -438,8 +438,8 @@ function Graphs.has_path(
)
return has_path(
ordinal_graph(graph),
vertex_to_parent_vertex(graph, source),
vertex_to_parent_vertex(graph, destination);
vertex_to_ordinal_vertex(graph, source),
vertex_to_ordinal_vertex(graph, destination);
exclude_vertices=vertices_to_parent_vertices(graph, exclude_vertices),
)
end
Expand Down Expand Up @@ -564,7 +564,7 @@ end
# vertex in the traversal/spanning tree.
function namedgraph_bfs_parents(graph::AbstractNamedGraph, vertex; kwargs...)
parent_bfs_parents = bfs_parents(
ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex); kwargs...
ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex); kwargs...
)
# Works around issue in this `Dictionary` constructor:
# https://github.com/andyferris/Dictionaries.jl/blob/v0.4.1/src/Dictionary.jl#L139-L145
Expand Down
2 changes: 1 addition & 1 deletion src/dfs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
# vertex in the traversal/spanning tree.
function namedgraph_dfs_parents(graph::AbstractNamedGraph, vertex; kwargs...)
parent_dfs_parents = dfs_parents(
ordinal_graph(graph), vertex_to_parent_vertex(graph, vertex); kwargs...
ordinal_graph(graph), vertex_to_ordinal_vertex(graph, vertex); kwargs...
)
# Works around issue in this `Dictionary` constructor:
# https://github.com/andyferris/Dictionaries.jl/blob/v0.4.1/src/Dictionary.jl#L139-L145
Expand Down
12 changes: 6 additions & 6 deletions src/lib/PartitionedGraphs/src/abstractpartitionedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ using ..NamedGraphs:
AbstractNamedGraph,
ordinal_graph,
ordinal_graph_type,
parent_vertex_to_vertex,
vertex_to_parent_vertex
ordered_vertices,
vertex_to_ordinal_vertex
using ..NamedGraphs.GraphsExtensions: GraphsExtensions, add_vertices!, rem_vertices!

abstract type AbstractPartitionedGraph{V,PV} <: AbstractNamedGraph{V} end
Expand Down Expand Up @@ -47,11 +47,11 @@ Graphs.vertices(pg::AbstractPartitionedGraph) = vertices(unpartitioned_graph(pg)
function NamedGraphs.ordinal_graph(pg::AbstractPartitionedGraph)
return ordinal_graph(unpartitioned_graph(pg))
end
function NamedGraphs.vertex_to_parent_vertex(pg::AbstractPartitionedGraph, vertex)
return vertex_to_parent_vertex(unpartitioned_graph(pg), vertex)
function NamedGraphs.vertex_to_ordinal_vertex(pg::AbstractPartitionedGraph, vertex)
return vertex_to_ordinal_vertex(unpartitioned_graph(pg), vertex)
end
function NamedGraphs.parent_vertex_to_vertex(pg::AbstractPartitionedGraph, parent_vertex)
return parent_vertex_to_vertex(unpartitioned_graph(pg), parent_vertex)
function NamedGraphs.ordered_vertices(pg::AbstractPartitionedGraph, parent_vertex)
return ordered_vertices(unpartitioned_graph(pg), parent_vertex)
end
Graphs.edgetype(pg::AbstractPartitionedGraph) = edgetype(unpartitioned_graph(pg))
function NamedGraphs.ordinal_graph_type(pg::AbstractPartitionedGraph)
Expand Down
38 changes: 19 additions & 19 deletions src/namedgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,54 @@ using .GraphsExtensions:

struct GenericNamedGraph{V,G<:AbstractSimpleGraph{Int}} <: AbstractNamedGraph{V}
ordinal_graph::G
parent_vertex_to_vertex::Vector{V}
vertex_to_parent_vertex::Dictionary{V,Int}
ordered_vertices::Vector{V}
vertex_to_ordinal_vertex::Dictionary{V,Int}
end

# AbstractNamedGraph required interface.
ordinal_graph_type(G::Type{<:GenericNamedGraph}) = fieldtype(G, :ordinal_graph)
ordinal_graph(graph::GenericNamedGraph) = getfield(graph, :ordinal_graph)
function vertex_to_parent_vertex(graph::GenericNamedGraph, vertex)
return graph.vertex_to_parent_vertex[vertex]
function vertex_to_ordinal_vertex(graph::GenericNamedGraph, vertex)
return graph.vertex_to_ordinal_vertex[vertex]
end
function parent_vertex_to_vertex(graph::GenericNamedGraph, parent_vertex)
return graph.parent_vertex_to_vertex[parent_vertex]
function ordered_vertices(graph::GenericNamedGraph, parent_vertex)
return graph.ordered_vertices[parent_vertex]
end

# TODO: Order them according to the internal ordering?
Graphs.vertices(graph::GenericNamedGraph) = keys(graph.vertex_to_parent_vertex)
Graphs.vertices(graph::GenericNamedGraph) = keys(graph.vertex_to_ordinal_vertex)

function Graphs.add_vertex!(graph::GenericNamedGraph, vertex)
if vertex vertices(graph)
return false
end
add_vertex!(graph.ordinal_graph)
# Update the forward map
push!(graph.parent_vertex_to_vertex, vertex)
push!(graph.ordered_vertices, vertex)
# Update the reverse map
insert!(graph.vertex_to_parent_vertex, vertex, nv(graph.ordinal_graph))
insert!(graph.vertex_to_ordinal_vertex, vertex, nv(graph.ordinal_graph))
return true
end

function Graphs.rem_vertex!(graph::GenericNamedGraph, vertex)
if vertex vertices(graph)
return false
end
parent_vertex = graph.vertex_to_parent_vertex[vertex]
parent_vertex = graph.vertex_to_ordinal_vertex[vertex]
rem_vertex!(graph.ordinal_graph, parent_vertex)
# Insert the last vertex into the position of the vertex
# that is being deleted, then remove the last vertex.
last_vertex = last(graph.parent_vertex_to_vertex)
graph.parent_vertex_to_vertex[parent_vertex] = last_vertex
last_vertex = pop!(graph.parent_vertex_to_vertex)
graph.vertex_to_parent_vertex[last_vertex] = parent_vertex
delete!(graph.vertex_to_parent_vertex, vertex)
last_vertex = last(graph.ordered_vertices)
graph.ordered_vertices[parent_vertex] = last_vertex
last_vertex = pop!(graph.ordered_vertices)
graph.vertex_to_ordinal_vertex[last_vertex] = parent_vertex
delete!(graph.vertex_to_ordinal_vertex, vertex)
return true
end

function GraphsExtensions.rename_vertices(f::Function, g::GenericNamedGraph)
# TODO: Could be implemented as `set_vertices(g, f.(g.parent_vertex_to_vertex))`.
return GenericNamedGraph(g.ordinal_graph, f.(g.parent_vertex_to_vertex))
# TODO: Could be implemented as `set_vertices(g, f.(g.ordered_vertices))`.
return GenericNamedGraph(g.ordinal_graph, f.(g.ordered_vertices))
end

function GraphsExtensions.rename_vertices(f::Function, g::AbstractSimpleGraph)
Expand All @@ -74,7 +74,7 @@ end

function GraphsExtensions.convert_vertextype(vertextype::Type, graph::GenericNamedGraph)
return GenericNamedGraph(
ordinal_graph(graph), convert(Vector{vertextype}, graph.parent_vertex_to_vertex)
ordinal_graph(graph), convert(Vector{vertextype}, graph.ordered_vertices)
)
end

Expand Down Expand Up @@ -191,7 +191,7 @@ GenericNamedGraph() = GenericNamedGraph(Any[])
# graph = set_ordinal_graph(graph, copy(ordinal_graph(graph)))
# graph = set_vertices(graph, copy(vertices(graph)))
function Base.copy(graph::GenericNamedGraph)
return GenericNamedGraph(copy(graph.ordinal_graph), copy(graph.parent_vertex_to_vertex))
return GenericNamedGraph(copy(graph.ordinal_graph), copy(graph.ordered_vertices))
end

Graphs.edgetype(G::Type{<:GenericNamedGraph}) = NamedEdge{vertextype(G)}
Expand Down

0 comments on commit 8b29f39

Please sign in to comment.