Skip to content

Commit

Permalink
Combinatorics: added in(::Int, ::Edge), signed_incidence_matrix(::Gra…
Browse files Browse the repository at this point in the history
…ph{Undirected}), connectivity(::Graph{Undirected}) (#4450)

Co-authored-by: Benjamin Lorenz <[email protected]>
  • Loading branch information
YueRen and benlorenz authored Jan 13, 2025
1 parent 1b9a50d commit 7219bae
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/src/Combinatorics/graphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rem_vertices!(g::Graph{T}, a::AbstractVector{Int64}) where {T <: Union{Directed,
adjacency_matrix(g::Graph)
all_neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
automorphism_group_generators(g::Graph{T}) where {T <: Union{Directed, Undirected}}
connectivity(g::Graph{Undirected})
complete_graph(n::Int64)
complete_bipartite_graph(n::Int64, m::Int64)
degree(g::Graph, v::Int)
Expand All @@ -67,6 +68,7 @@ inneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
neighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
outneighbors(g::Graph{T}, v::Int64) where {T <: Union{Directed, Undirected}}
shortest_path_dijkstra
signed_incidence_matrix(g::Graph)
is_isomorphic(g1::Graph{T}, g2::Graph{T}) where {T <: Union{Directed, Undirected}}
is_isomorphic_with_permutation(G1::Graph, G2::Graph)
```
Expand Down
46 changes: 43 additions & 3 deletions src/Combinatorics/Graphs/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ Vector{Int}(e::Edge) = [src(e), dst(e)]

Base.isless(a::Edge, b::Edge) = Base.isless(Vector{Int}(a), Vector{Int}(b))

Base.in(i::Int, a::Edge) = (i==src(a) || i==dst(a))

rem_edge!(g::Graph{T}, e::Edge) where {T <: Union{Directed, Undirected}} =
rem_edge!(g, src(e), dst(e))

Expand Down Expand Up @@ -642,9 +644,9 @@ function incidence_matrix(g::Graph{T}) where {T <: Union{Directed, Undirected}}
end

@doc raw"""
signed_incidence_matrix(g::Graph{Directed})
signed_incidence_matrix(g::Graph)
Return a signed incidence matrix representing a directed graph `g`.
Return a signed incidence matrix representing a graph `g`. If `g` is directed, sources will have sign `-1` and targest will have sign `+1`. If `g` is undirected, vertices of larger index will have sign `-1` and vertices of smaller index will have sign `+1`.
# Examples
```jldoctest
Expand All @@ -659,9 +661,22 @@ julia> signed_incidence_matrix(g)
0 1 -1 0 0
0 0 1 -1 0
0 0 0 1 -1
julia> g = Graph{Undirected}(5);
julia> add_edge!(g,1,2); add_edge!(g,2,3); add_edge!(g,3,4); add_edge!(g,4,5); add_edge!(g,5,1);
julia> signed_incidence_matrix(g)
5×5 Matrix{Int64}:
1 0 0 1 0
-1 1 0 0 0
0 -1 1 0 0
0 0 -1 0 1
0 0 0 -1 -1
```
"""
signed_incidence_matrix(g::Graph{Directed}) = convert(Matrix{Int}, Polymake.graph.signed_incidence_matrix(pm_object(g)))
signed_incidence_matrix(g::Graph) = convert(Matrix{Int}, Polymake.graph.signed_incidence_matrix(pm_object(g)))

################################################################################
################################################################################
Expand Down Expand Up @@ -754,6 +769,31 @@ function shortest_path_dijkstra(g::Graph{T}, s::Int64, t::Int64; reverse::Bool=f
return Polymake.to_one_based_indexing(result)
end

@doc raw"""
connectivity(g::Graph{Undirected})
Return the connectivity of the undirected graph `g`.
# Examples
```jldoctest
julia> g = complete_graph(3);
julia> connectivity(g)
2
julia> rem_edge!(g, 2, 3);
julia> connectivity(g)
1
julia> rem_edge!(g, 1, 3);
julia> connectivity(g)
0
```
"""
connectivity(g::Graph{Undirected}) = Polymake.graph.connectivity(g)::Int

@doc raw"""
is_connected(g::Graph{Undirected})
Expand Down
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ export conjugate_group
export conjugate_transpose
export connected_components
export connected_sum
export connectivity
export connectivity_function
export contains
export continued_fraction_hirzebruch_jung
Expand Down
12 changes: 10 additions & 2 deletions test/Combinatorics/Graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
@test signed_incidence_matrix(g) == Matrix([-1 0 1; 1 -1 0; 0 1 -1; 0 0 0])

e = Edge(1,2)
@test 1 in e
@test 2 in e
@test !(3 in e)
end

triangle = simplex(2)
Expand All @@ -42,7 +47,7 @@
egpos = vertex_edge_graph(pos)
egpl = vertex_edge_graph(pl)
egplc = vertex_edge_graph(pl, modulo_lineality=true)

@testset "graphs from polytopes" begin
@test n_vertices(egtriangle) == 3
@test n_edges(egtriangle) == 3
Expand Down Expand Up @@ -127,17 +132,20 @@

g = Graph{Undirected}(5)
@test !is_connected(g)
@test connectivity(g) == 0
@test length(connected_components(g)) == 5

add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 1, 3)
add_edge!(g, 4, 5)
@test !is_connected(g)
@test connectivity(g) == 0
@test length(connected_components(g)) == 2

add_edge!(g, 3, 5)
@test is_connected(g)
@test connectivity(g) == 1
@test length(connected_components(g)) == 1
@test diameter(g) == 3
end
Expand All @@ -153,7 +161,7 @@

@test n_vertices(G1) == 12
@test n_edges(G1) == 3

x2 = [[11,3],[3,5],[4,5],[2,4],[2,3]]
G2 = graph_from_edges(Undirected, x2, 13)

Expand Down

0 comments on commit 7219bae

Please sign in to comment.