Skip to content

Commit

Permalink
migrated from LightGraphs.jl to Graphs.jl (#8)
Browse files Browse the repository at this point in the history
* Update Project.toml

* updated LightGraphs to Graphs

* Update runtests.jl
  • Loading branch information
tomsmierz authored Jan 31, 2024
1 parent baf5779 commit 75f6686
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
8 changes: 3 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ authors = ["Konrad Jałowiecki <[email protected]>", "Łukasz Pawela <lukasz.
version = "0.4.3"

[deps]
LightGraphs = "093fc24a-ae57-5d10-9952-331d41423f4d"
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
MetaGraphs = "626554b9-1ddb-594c-aa3c-2596fe9399a5"

[compat]
LightGraphs = "1.3"
MetaGraphs = "0.6"
julia = "1.5"

MetaGraphs = "0.7"
julia = ">= 1.5"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Expand Down
38 changes: 19 additions & 19 deletions src/LabelledGraphs.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LabelledGraphs

using LightGraphs
using Graphs
using MetaGraphs
export LabelledGraph, LabelledDiGraph, LabelledEdge

Expand All @@ -17,8 +17,8 @@ module LabelledGraphs
dst::T
end

LightGraphs.src(e::LabelledEdge{T}) where T = e.src
LightGraphs.dst(e::LabelledEdge{T}) where T = e.dst
Graphs.src(e::LabelledEdge{T}) where T = e.src
Graphs.dst(e::LabelledEdge{T}) where T = e.dst

Base.:(==)(e1::LabelledEdge, e2::LabelledEdge) = src(e1) == src(e2) && dst(e1) == dst(e2)

Expand All @@ -37,53 +37,53 @@ module LabelledGraphs
LabelledGraph(labels, S(length(labels)))

# --- Querying vertices ---
LightGraphs.nv(g::LabelledGraph) = length(g.labels)
Graphs.nv(g::LabelledGraph) = length(g.labels)

LightGraphs.vertices(g::LabelledGraph) = g.labels
Graphs.vertices(g::LabelledGraph) = g.labels

LightGraphs.has_vertex(g::LabelledGraph, v) = v in g.labels
Graphs.has_vertex(g::LabelledGraph, v) = v in g.labels


# --- Querying edges ---
LightGraphs.ne(g::LabelledGraph) = ne(g.inner_graph)
Graphs.ne(g::LabelledGraph) = ne(g.inner_graph)

LightGraphs.edges(g::LabelledGraph) =
Graphs.edges(g::LabelledGraph) =
map(e -> LabelledEdge(g.labels[src(e)], g.labels[dst(e)]), edges(g.inner_graph))

LightGraphs.has_edge(g::LabelledGraph, s, d) =
Graphs.has_edge(g::LabelledGraph, s, d) =
has_vertex(g, s) &&
has_vertex(g, d) &&
has_edge(g.inner_graph, g.reverse_label_map[s], g.reverse_label_map[d])

LightGraphs.has_edge(g::LabelledGraph, e::LightGraphs.AbstractEdge) =
Graphs.has_edge(g::LabelledGraph, e::Graphs.AbstractEdge) =
has_edge(g, src(e), dst(e))


# --- Querying neighborhoods ---
LightGraphs.outneighbors(g::LabelledGraph{S, T}, v::T) where S where T =
Graphs.outneighbors(g::LabelledGraph{S, T}, v::T) where S where T =
[g.labels[u] for u outneighbors(g.inner_graph, g.reverse_label_map[v])]

LightGraphs.inneighbors(g::LabelledGraph{S, T}, v::T) where S where T =
Graphs.inneighbors(g::LabelledGraph{S, T}, v::T) where S where T =
[g.labels[u] for u inneighbors(g.inner_graph, g.reverse_label_map[v])]

LightGraphs.all_neighbors(g::LabelledGraph{S, T}, v::T) where S where T =
Graphs.all_neighbors(g::LabelledGraph{S, T}, v::T) where S where T =
collect(union(Set(inneighbors(g, v)), Set(outneighbors(g, v))))


# --- Querying other graph properties ---
LightGraphs.is_directed(::Type{LabelledGraph{S, T}}) where S where T = is_directed(S)
Graphs.is_directed(::Type{LabelledGraph{S, T}}) where S where T = is_directed(S)


# --- Mutations ---
# Warning: this might need further adjustments if we incorporate support for static graphs,
# as they are immutable.
LightGraphs.add_edge!(lg::LabelledGraph{S, T}, s::T, d::T) where S where T =
Graphs.add_edge!(lg::LabelledGraph{S, T}, s::T, d::T) where S where T =
add_edge!(lg.inner_graph, lg.reverse_label_map[s], lg.reverse_label_map[d])

LightGraphs.add_edge!(lg::LabelledGraph{S, T}, e::AbstractEdge{T}) where S where T =
Graphs.add_edge!(lg::LabelledGraph{S, T}, e::AbstractEdge{T}) where S where T =
add_edge!(lg, src(e), dst(e))

function LightGraphs.add_vertex!(lg::LabelledGraph{S, T}, v::T) where S where T
function Graphs.add_vertex!(lg::LabelledGraph{S, T}, v::T) where S where T
if v lg.labels
throw(ArgumentError("Duplicate labels are not allowed"))
end
Expand All @@ -92,7 +92,7 @@ module LabelledGraphs
push!(lg.reverse_label_map, v => nv(lg.inner_graph))
end

function LightGraphs.add_vertices!(lg::LabelledGraph{S, T}, vertices::Vector{T}) where S where T
function Graphs.add_vertices!(lg::LabelledGraph{S, T}, vertices::Vector{T}) where S where T
if any(v lg.labels for v vertices)
throw(ArgumentError("Duplicate labels are not allowed"))
end
Expand Down Expand Up @@ -213,7 +213,7 @@ module LabelledGraphs
has_prop(lg.inner_graph, prop)
end

function LightGraphs.induced_subgraph(
function Graphs.induced_subgraph(
lg::LabelledGraph{S, T}, vertices::Vector{T}
) where {S, T}
sub_ig, _vmap = induced_subgraph(lg.inner_graph, [lg.reverse_label_map[v] for v in vertices])
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LightGraphs
using Graphs
using MetaGraphs
using Test

Expand Down

0 comments on commit 75f6686

Please sign in to comment.