Skip to content

Commit

Permalink
Rename dependency LightGraphs to Graphs
Browse files Browse the repository at this point in the history
  • Loading branch information
Liozou committed Nov 1, 2021
1 parent 5f79d6a commit ea9dfa0
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 86 deletions.
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name = "PeriodicGraphs"
uuid = "18c5b727-b240-4874-878a-f2e242435bab"
authors = ["Lionel Zoubritzky [email protected]"]
version = "0.2.1"
version = "0.3.0"

[deps]
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

[compat]
LightGraphs = "1.3"
Graphs = "1.3"
StaticArrays = "0.12, 1.0"
julia = "1.4"

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![codecov](https://codecov.io/gh/Liozou/PeriodicGraphs.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/Liozou/PeriodicGraphs.jl)
<!-- [![Aqua QA](https://img.shields.io/badge/Aqua.jl-%F0%9F%8C%A2-aqua.svg)](https://github.com/tkf/Aqua.jl) -->

This module allows to manipulate `N`-dimensional periodic graphs, using the new type `PeriodicGraph{N}`. This is a subtype of `AbstractGraph{Int}` from [LightGraphs.jl](https://github.com/JuliaGraphs/LightGraphs.jl/) and it extends its API.
This module allows to manipulate `N`-dimensional periodic graphs, using the new type `PeriodicGraph{N}`. This is a subtype of `AbstractGraph{Int}` from [Graphs.jl](https://github.com/JuliaGraphs/Graphs.jl/) and it extends its API.

The main difference with a simple graph is the notion of offset. Each vertex, of type `PeriodicVertex{N}` is uniquely defined using a numeric identifier (a positive integer, like for any simple graph) associated with the offset of the cell in which the designated vertex is, compared to a fixed reference cell. For instance, all vertices in the reference cell have a zero offset, and may be built like so:
```julia
Expand All @@ -22,10 +22,10 @@ An edge, of type `PeriodicEdge{N}`, is defined by its representative starting fr
julia> PeriodicEdge{4}(2, PeriodicVertex{4}(3, (0,1,0,0)))
PeriodicEdge{4}(2, 3, (0,1,0,0))

julia> LightGraphs.src(PeriodicEdge{3}(5, 6, (1,0,2)))
julia> Graphs.src(PeriodicEdge{3}(5, 6, (1,0,2)))
5

julia> LightGraphs.dst(PeriodicEdge{3}(5, 6, (1,0,2)))
julia> Graphs.dst(PeriodicEdge{3}(5, 6, (1,0,2)))
6

julia> PeriodicGraphs.ofs(PeriodicEdge{3}(5, 6, (1,0,2)))
Expand Down Expand Up @@ -61,9 +61,9 @@ julia> string(ans) # to obtain the compact representation from the graph
"3 1 2 1 0 0 1 3 0 1 0 2 2 0 0 1 2 3 0 1 1"
```

Note that `PeriodicGraph`s are undirected: for this reason, any edge in the graph of the form `(u, v, (x1, x2, ..., xN))` has a reverse edge in the graph of the form `(v, u, (-x1, -x2, ..., -xN))`. For this reason, calling `LightGraphs.edges` on a `PeriodicGraph` yields an iterator to the edges of the graph that will only show the edges under the canonical form `(u, v, ofs)` with either `u < v` or `u == v && ofs > zero(ofs)`.
Note that `PeriodicGraph`s are undirected: for this reason, any edge in the graph of the form `(u, v, (x1, x2, ..., xN))` has a reverse edge in the graph of the form `(v, u, (-x1, -x2, ..., -xN))`. For this reason, calling `Graphs.edges` on a `PeriodicGraph` yields an iterator to the edges of the graph that will only show the edges under the canonical form `(u, v, ofs)` with either `u < v` or `u == v && ofs > zero(ofs)`.

Neighbors can be obtained using LightGraphs's `neighbors` function, with the same API. It will return the list of neighbors of the given vertex, assuming the vertex is in the reference cell. Each given is given as a `PeriodicVertex`, which contains the offset compared to the reference cell. For instance:
Neighbors can be obtained using Graphs's `neighbors` function, with the same API. It will return the list of neighbors of the given vertex, assuming the vertex is in the reference cell. Each given is given as a `PeriodicVertex`, which contains the offset compared to the reference cell. For instance:
```julia
julia> g = PeriodicGraph("3 1 2 1 0 0 1 3 0 1 0 2 2 0 0 1 2 3 0 1 1")
PeriodicGraph3D(3, PeriodicEdge3D[(1, 2, (1,0,0)), (1, 3, (0,1,0)), (2, 2, (0,0,1)), (2, 3, (0,1,1))])
Expand Down
62 changes: 31 additions & 31 deletions src/PeriodicGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module PeriodicGraphs

using LinearAlgebra
using StaticArrays
using LightGraphs
using Graphs

export PeriodicVertex, PeriodicEdge, PeriodicGraph,
PeriodicVertex1D, PeriodicEdge1D, PeriodicGraph1D,
Expand Down Expand Up @@ -125,14 +125,14 @@ This function is not part of the official API, use with caution.
struct unsafe_edge{N} end

"""
PeriodicEdge{N} <: LightGraphs.SimpleGraphs.AbstractSimpleEdge{Int}
PeriodicEdge{N} <: Graphs.SimpleGraphs.AbstractSimpleEdge{Int}
Edge type for an `N`-periodic graph.
An edge is uniquely determined by the vertex identifiers of its source and
destination, and the cell offset between the source vertex and the destination vertex.
"""
struct PeriodicEdge{N} <: LightGraphs.SimpleGraphs.AbstractSimpleEdge{Int}
struct PeriodicEdge{N} <: Graphs.SimpleGraphs.AbstractSimpleEdge{Int}
src::Int
dst::PeriodicVertex{N}
function (::Type{unsafe_edge{N}})(src, dst) where N
Expand Down Expand Up @@ -178,11 +178,11 @@ const PeriodicEdge1D = PeriodicEdge{1}
const PeriodicEdge2D = PeriodicEdge{2}
const PeriodicEdge3D = PeriodicEdge{3}

function LightGraphs.reverse(e::PeriodicEdge{N}) where N
function Graphs.reverse(e::PeriodicEdge{N}) where N
return unsafe_edge{N}(e.dst.v, e.src, .-e.dst.ofs)
end
LightGraphs.src(e::PeriodicEdge) = e.src
LightGraphs.dst(e::PeriodicEdge) = e.dst.v
Graphs.src(e::PeriodicEdge) = e.src
Graphs.dst(e::PeriodicEdge) = e.dst.v

"""
ofs(v::PeriodicVertex)
Expand Down Expand Up @@ -422,13 +422,13 @@ julia> ndims(PeriodicGraph2D(7))
"""
ndims

LightGraphs.ne(g::PeriodicGraph) = g.ne[]
LightGraphs.nv(g::PeriodicGraph) = length(g.nlist)
LightGraphs.vertices(g::PeriodicGraph) = Base.OneTo(nv(g))
LightGraphs.edges(g::PeriodicGraph{N}) where {N} = PeriodicEdgeIter{N}(g)
Graphs.ne(g::PeriodicGraph) = g.ne[]
Graphs.nv(g::PeriodicGraph) = length(g.nlist)
Graphs.vertices(g::PeriodicGraph) = Base.OneTo(nv(g))
Graphs.edges(g::PeriodicGraph{N}) where {N} = PeriodicEdgeIter{N}(g)
eltype(g::PeriodicGraph{N}) where {N} = PeriodicVertex{N}
LightGraphs.edgetype(::PeriodicGraph{N}) where {N} = PeriodicEdge{N}
function LightGraphs.has_edge(g::PeriodicGraph, s, d)
Graphs.edgetype(::PeriodicGraph{N}) where {N} = PeriodicEdge{N}
function Graphs.has_edge(g::PeriodicGraph, s, d)
((s < 1) | (s > nv(g))) && return false
#=@inbounds=# begin
start = g.directedgestart[s]
Expand Down Expand Up @@ -456,7 +456,7 @@ function find_edges(g::PeriodicGraph{N}, s::Int, d::Int) where N
return g.nlist[s][rng]
end
end
function LightGraphs.has_edge(g::PeriodicGraph, e::PeriodicEdge)
function Graphs.has_edge(g::PeriodicGraph, e::PeriodicEdge)
s, d = e.src, e.dst
((s < 1) | (s > nv(g))) && return false
#=@inbounds=# begin
Expand All @@ -466,21 +466,21 @@ function LightGraphs.has_edge(g::PeriodicGraph, e::PeriodicEdge)
return i <= length(g.nlist[s]) && g.nlist[s][i] == d
end
end
LightGraphs.outneighbors(g::PeriodicGraph, v::Integer) = g.nlist[v]
LightGraphs.inneighbors(g::PeriodicGraph, v::Integer) = outneighbors(g, v)
Graphs.outneighbors(g::PeriodicGraph, v::Integer) = g.nlist[v]
Graphs.inneighbors(g::PeriodicGraph, v::Integer) = outneighbors(g, v)
zero(::Type{PeriodicGraph{N}}) where N = PeriodicGraph{N}(0)
LightGraphs.is_directed(::Type{<:PeriodicGraph}) = false
@static if isdefined(LightGraphs, :has_contiguous_vertices)
@inline LightGraphs.has_contiguous_vertices(::Type{<:PeriodicGraph}) = true
Graphs.is_directed(::Type{<:PeriodicGraph}) = false
@static if isdefined(Graphs, :has_contiguous_vertices)
@inline Graphs.has_contiguous_vertices(::Type{<:PeriodicGraph}) = true
end
LightGraphs.has_vertex(g::PeriodicGraph, v::Integer) = 1 <= v <= nv(g)
function LightGraphs.SimpleGraphs.add_vertices!(g::PeriodicGraph{N}, n::Integer) where N
Graphs.has_vertex(g::PeriodicGraph, v::Integer) = 1 <= v <= nv(g)
function Graphs.SimpleGraphs.add_vertices!(g::PeriodicGraph{N}, n::Integer) where N
append!(g.nlist, [PeriodicVertex{N}[] for _ in 1:n])
append!(g.directedgestart, [1 for _ in 1:n])
g.width[] = -1
return n
end
function LightGraphs.SimpleGraphs.add_vertex!(g::PeriodicGraph{N}) where N
function Graphs.SimpleGraphs.add_vertex!(g::PeriodicGraph{N}) where N
push!(g.nlist, PeriodicVertex{N}[])
push!(g.directedgestart, 1)
g.width[] = -1
Expand All @@ -503,7 +503,7 @@ function _add_edge!(g::PeriodicGraph, e::PeriodicEdge, ::Val{check}) where check
return true
end
end
function LightGraphs.add_edge!(g::PeriodicGraph, e::PeriodicEdge)
function Graphs.add_edge!(g::PeriodicGraph, e::PeriodicEdge)
(src(e) < 1 || src(e) > nv(g) || dst(e) < 1 || dst(e) > nv(g)) && return false
success = _add_edge!(g, e, Val(true)) && _add_edge!(g, reverse(e), Val(false))
if success
Expand All @@ -529,7 +529,7 @@ function _rem_edge!(g::PeriodicGraph, e::PeriodicEdge, ::Val{check}) where check
return true
end
end
function LightGraphs.rem_edge!(g::PeriodicGraph, e::PeriodicEdge)
function Graphs.rem_edge!(g::PeriodicGraph, e::PeriodicEdge)
(src(e) < 1 || src(e) > nv(g) || dst(e) < 1 || dst(e) > nv(g)) && return false
success = _rem_edge!(g, e, Val(true)) && _rem_edge!(g, reverse(e), Val(false))
if success
Expand All @@ -540,7 +540,7 @@ function LightGraphs.rem_edge!(g::PeriodicGraph, e::PeriodicEdge)
end


function LightGraphs.SimpleGraphs.rem_vertices!(g::PeriodicGraph{N}, t::AbstractVector{<:Integer}, keep_order::Bool=false) where N
function Graphs.SimpleGraphs.rem_vertices!(g::PeriodicGraph{N}, t::AbstractVector{<:Integer}, keep_order::Bool=false) where N
isempty(t) && return collect(1:nv(g))
sort!(t)
(first(t) < 1 || last(t) > nv(g)) && throw(ArgumentError("Vertices to be removed must be in the range 1:nv(g)."))
Expand Down Expand Up @@ -618,7 +618,7 @@ function LightGraphs.SimpleGraphs.rem_vertices!(g::PeriodicGraph{N}, t::Abstract
return vmap
end

function LightGraphs.SimpleGraphs.rem_vertex!(g::PeriodicGraph, v::Integer)
function Graphs.SimpleGraphs.rem_vertex!(g::PeriodicGraph, v::Integer)
n = nv(g)
return length(rem_vertices!(g, [v])) == n - 1
end
Expand Down Expand Up @@ -729,7 +729,7 @@ function vertex_permutation(g::PeriodicGraph{N}, vlist) where N
return PeriodicGraph{N}(g.ne[], edges, startoffsets)
end

function LightGraphs.induced_subgraph(g::PeriodicGraph{N}, vlist::AbstractVector{U}) where {N, U<:Integer}
function Graphs.induced_subgraph(g::PeriodicGraph{N}, vlist::AbstractVector{U}) where {N, U<:Integer}
allunique(vlist) || __throw_unique_vlist()
n = length(vlist)
n == nv(g) && return (vertex_permutation(g, vlist), vlist)
Expand Down Expand Up @@ -815,7 +815,7 @@ end
@noinline __throw_invalid_axesswap() = throw(DimensionMismatch("The number of axes must match the dimension of the graph"))


function LightGraphs.connected_components(g::PeriodicGraph)
function Graphs.connected_components(g::PeriodicGraph)
nvg = nv(g)
label = zeros(Int, nvg)

Expand All @@ -835,7 +835,7 @@ function LightGraphs.connected_components(g::PeriodicGraph)
end
end
end
c, d = LightGraphs.components(label)
c, d = Graphs.components(label)
return c
end

Expand Down Expand Up @@ -900,7 +900,7 @@ function graph_width!(g::PeriodicGraph{N}) where N
g.width[] = width == nv(g)+1 ? Rational(maxa) : width
end

function LightGraphs._neighborhood(g::Union{PeriodicGraph{0},PeriodicGraph{1},PeriodicGraph{2},PeriodicGraph{3}}, v::Integer, d::Real, distmx::AbstractMatrix{U}, neighborfn::Function) where U <: Real
function Graphs._neighborhood(g::Union{PeriodicGraph{0},PeriodicGraph{1},PeriodicGraph{2},PeriodicGraph{3}}, v::Integer, d::Real, distmx::AbstractMatrix{U}, neighborfn::Function) where U <: Real
@assert typeof(neighborfn) === typeof(outneighbors)
N = ndims(g)
Q = Tuple{PeriodicVertex{N}, U}[]
Expand Down Expand Up @@ -932,7 +932,7 @@ function LightGraphs._neighborhood(g::Union{PeriodicGraph{0},PeriodicGraph{1},Pe
return Q
end

function LightGraphs._neighborhood(g::PeriodicGraph{N}, v::Integer, d::Real, distmx::AbstractMatrix{U}, neighborfn::Function) where {N,U <: Real}
function Graphs._neighborhood(g::PeriodicGraph{N}, v::Integer, d::Real, distmx::AbstractMatrix{U}, neighborfn::Function) where {N,U <: Real}
@assert typeof(neighborfn) === typeof(outneighbors)
Q = Tuple{PeriodicVertex, U}[]
d < zero(U) && return Q
Expand Down Expand Up @@ -964,7 +964,7 @@ Compute the list of numbers of `n`-th neighbors of vertex `v` in graph `g`, for
`1 ≤ n ≤ dmax`.
"""
function coordination_sequence(g::PeriodicGraph, v::Integer, dmax)
Q = LightGraphs._neighborhood(g, v, dmax, weights(g), outneighbors)
Q = Graphs._neighborhood(g, v, dmax, weights(g), outneighbors)
popfirst!(Q)
ret = zeros(Int, dmax)
for (_, d) in Q
Expand Down
Loading

2 comments on commit ea9dfa0

@Liozou
Copy link
Owner Author

@Liozou Liozou commented on ea9dfa0 Nov 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/47857

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.0 -m "<description of version>" ea9dfa032273be6f925b9821d356b8dca461d65c
git push origin v0.3.0

Please sign in to comment.