Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combinatorics: added in(::Int, ::Edge), signed_incidence_matrix(::Graph{Undirected}), connectivity(::Graph{Undirected}) #4450

Merged
merged 7 commits into from
Jan 13, 2025
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 @@ -641,9 +643,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 @@ -658,9 +660,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 @@ -753,6 +768,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)
YueRen marked this conversation as resolved.
Show resolved Hide resolved

@doc raw"""
is_connected(g::Graph{Undirected})

Expand Down
9 changes: 7 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 @@ -153,7 +158,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
Loading