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

Add is_bipartite(::Graph) #4499

Merged
merged 12 commits into from
Jan 27, 2025
1 change: 1 addition & 0 deletions docs/src/Combinatorics/graphs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/Combinatorics/Graphs/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions src/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions test/Combinatorics/Graph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading