Skip to content

Commit

Permalink
Graphs: add_edge! and rem_edge! now return useful bools
Browse files Browse the repository at this point in the history
  • Loading branch information
lkastner committed Dec 5, 2023
1 parent ef9215b commit 3a86c02
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
61 changes: 42 additions & 19 deletions src/Combinatorics/Graphs/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,53 +86,67 @@ _has_node(G::Graph, node::Int64) = 0 < node <= nv(G)
add_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
Add edge `(s,t)` to the graph `g`.
Return `true` if a new edge `(s,t)` was added, `false` otherwise.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> add_edge!(g, 1, 2)
true
julia> add_edge!(g, 1, 2)
false
julia> ne(g)
1
```
"""
function add_edge!(g::Graph{T}, source::Int64, target::Int64) where {T <: Union{Directed, Undirected}}
@req _has_node(g, source) && _has_node(g, target) "Nodes must be between 1 and $(nv(g)), but edge given is $source -- $target"
Polymake._add_edge(pm_object(g), source-1, target-1)
_has_node(g, source) && _has_node(g, target) || return false
old_nedges = nedges(g)
Polymake._add_edge(pm_object(g), source-1, target-1)
return nedges(g) == old_nedges + 1
end


@doc raw"""
rem_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
Remove edge `(s,t)` from the graph `g`.
Return `true` if there was an edge from `s` to `t` and it got removed, `false`
otherwise.
# Examples
```jldoctest
julia> g = Graph{Directed}(2);
julia> add_edge!(g, 1, 2);
julia> add_edge!(g, 1, 2)
true
julia> ne(g)
1
julia> rem_edge!(g, 1, 2);
julia> rem_edge!(g, 1, 2)
true
julia> ne(g)
0
```
"""
function rem_edge!(g::Graph{T}, s::Int64, t::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
return Polymake._rem_edge(pmg, s-1, t-1)
has_edge(g, s, t) || return false
old_nedges = nedges(g)
Polymake._rem_edge(pm_object(g), s-1, t-1)
return nedges(g) == old_nedges - 1
end


@doc raw"""
add_vertex!(g::Graph{T}) where {T <: Union{Directed, Undirected}}
Add a vertex to the graph `g`. The return value is the new vertex.
Add a vertex to the graph `g`. Return `true` if there a new vertex was actually
added.
# Examples
```jldoctest
Expand All @@ -142,22 +156,25 @@ julia> nv(g)
2
julia> add_vertex!(g)
3
true
julia> nv(g)
3
```
"""
function add_vertex!(g::Graph{T}) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
return Polymake._add_vertex(pmg) + 1
old_nvertices = nv(g)
Polymake._add_vertex(pmg)
return nv(g) - 1 == old_nvertices
end


@doc raw"""
rem_vertex!(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
Remove the vertex `v` from the graph `g`.
Remove the vertex `v` from the graph `g`. Return `true` if node `v` existed and
was actually removed, `false` otherwise.
# Examples
```jldoctest
Expand All @@ -167,23 +184,27 @@ julia> nv(g)
2
julia> rem_vertex!(g, 1)
true
julia> nv(g)
1
```
"""
function rem_vertex!(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
pmg = pm_object(g)
result = Polymake._rem_vertex(pmg, v-1)
Polymake._squeeze(pmg)
return result
_has_node(g, v) || return false
pmg = pm_object(g)
old_nvertices = nv(g)
result = Polymake._rem_vertex(pmg, v-1)
Polymake._squeeze(pmg)
return nv(g) + 1 == old_nvertices
end


@doc raw"""
add_vertices!(g::Graph{T}, n::Int64) where {T <: Union{Directed, Undirected}}
Add a `n` new vertices to the graph `g`.
Add a `n` new vertices to the graph `g`. Return the number of vertices that
were actually added to the graph `g`.
# Examples
```jldoctest
Expand All @@ -199,9 +220,11 @@ julia> nv(g)
```
"""
function add_vertices!(g::Graph{T}, n::Int64) where {T <: Union{Directed, Undirected}}
for i = 1:n
add_vertex!(g)
end
result = 0
for i = 1:n
result += add_vertex!(g) ? 1 : 0
end
return result
end


Expand Down
5 changes: 2 additions & 3 deletions test/Combinatorics/Graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
rem_edge!(g, 1, 2)
@test ne(g) == 0
@test !has_edge(g, 1, 2)
v = add_vertex!(g)
@test v == 6
@test add_vertex!(g)
@test nv(g) == 6
@test has_vertex(g, 6)
rem_vertex!(g, 1)
Expand Down Expand Up @@ -121,6 +120,6 @@

@testset "errors" begin
g = Graph{Undirected}(1)
@test_throws ArgumentError add_edge!(g,1,2)
@test !add_edge!(g,1,2)
end
end

0 comments on commit 3a86c02

Please sign in to comment.