diff --git a/docs/src/Combinatorics/graphs.md b/docs/src/Combinatorics/graphs.md index eaab41497e7a..544e33610a9a 100644 --- a/docs/src/Combinatorics/graphs.md +++ b/docs/src/Combinatorics/graphs.md @@ -71,6 +71,7 @@ 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) +is_bipartite(g::Graph{Undirected}) ``` ### Edges diff --git a/src/Combinatorics/Graphs/functions.jl b/src/Combinatorics/Graphs/functions.jl index 7dd36ab2a212..075f8b288dd9 100644 --- a/src/Combinatorics/Graphs/functions.jl +++ b/src/Combinatorics/Graphs/functions.jl @@ -1339,3 +1339,20 @@ function laplacian_matrix(g::Graph) A = matrix(ZZ, adjacency_matrix(g)) return D-A end + +@doc raw""" + is_bipartite(g::Graph{Undirected}) + +Returns true if the undirected graph `g` is bipartite. + +# Examples +```jldoctest +julia> g = graph_from_edges([[1,2],[2,3],[3,4]]); + +julia> is_bipartite(g) +true +``` +""" +function is_bipartite(g::Graph{Undirected}) + return Polymake.graph.Graph{Undirected}(ADJACENCY=pm_object(g)).BIPARTITE::Bool +end diff --git a/src/exports.jl b/src/exports.jl index 5e85af449871..347d6892aefc 100644 --- a/src/exports.jl +++ b/src/exports.jl @@ -814,6 +814,7 @@ export is_bicoset export is_bijective export is_binary export is_binomial +export is_bipartite export is_bounded export is_canonically_isomorphic export is_canonically_isomorphic_with_map diff --git a/test/Combinatorics/Graph.jl b/test/Combinatorics/Graph.jl index 546a0d80b4d7..a20770652179 100644 --- a/test/Combinatorics/Graph.jl +++ b/test/Combinatorics/Graph.jl @@ -190,4 +190,15 @@ @test matrix(ZZ, adjacency_matrix(G1)) == matrix(ZZ, [0 1 1 0; 1 0 0 1; 1 0 0 1; 0 1 1 0]) @test laplacian_matrix(G1) == matrix(ZZ, [2 -1 -1 0; -1 2 0 -1; -1 0 2 -1; 0 -1 -1 2]) end + + @testset "is_bipartite" begin + G0 = Graph{Undirected}(3) + add_edge!(G0,1,2) + add_edge!(G0,1,3) + add_edge!(G0,2,3) + @test is_bipartite(G0) == false + + G1 = graph_from_edges([[1,2],[2,3],[3,4]]) + @test is_bipartite(G1) == true + end end