Skip to content

Commit

Permalink
Added clique_number, independence_number
Browse files Browse the repository at this point in the history
  • Loading branch information
dstahlke committed Jul 17, 2022
1 parent a27730a commit f7c165b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ barabasi_albert!, static_fitness_model, static_scale_free, kronecker, dorogovtse
#community
modularity, core_periphery_deg,
local_clustering,local_clustering_coefficient, global_clustering_coefficient, triangles,
label_propagation, maximal_cliques, clique_percolation, assortativity,rich_club,
maximal_cliques, maximum_clique, clique_number,
maximal_independent_sets, maximum_independent_set, independence_number,
label_propagation, clique_percolation, assortativity,rich_club,

#generators
complete_graph, star_graph, path_graph, wheel_graph, cycle_graph,
Expand Down
109 changes: 109 additions & 0 deletions src/community/cliques.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,112 @@ function maximal_cliques end
end
return cliques
end

"""
maximum_clique(g)
Return a vector representing the node indices of a maximum clique
of the undirected graph `g`.
```jldoctest
julia> using Graphs
julia> maximum_clique(blockdiag(complete_graph(3), complete_graph(4)))
4-element Vector{Int64}:
4
5
6
7
```
"""
function maximum_clique end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function maximum_clique(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
return sort(argmax(length, maximal_cliques(g)))
end

"""
clique_number(g)
Returns the size of the largest clique of the undirected graph `g`.
```jldoctest
julia> using Graphs
julia> clique_number(blockdiag(complete_graph(3), complete_graph(4)))
4
```
"""
function clique_number end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function clique_number(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
return maximum(length, maximal_cliques(g))
end

"""
maximal_independent_sets(g)
Return a vector of vectors representing the node indices in each of the maximal
independent sets found in the undirected graph `g`.
```jldoctest
julia> using Graphs
julia> maximal_independent_sets(cycle_graph(5))
5-element Vector{Vector{Int64}}:
[5, 2]
[5, 3]
[2, 4]
[1, 4]
[1, 3]
```
"""
function maximal_independent_sets end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function maximal_independent_sets(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
return maximal_cliques(complement(g))
end

"""
maximum_independent_set(g)
Return a vector representing the node indices of a maximum independent set
of the undirected graph `g`.
### See also
[`independent_set`](@ref)
## Examples
```jldoctest
julia> using Graphs
julia> maximum_independent_set(cycle_graph(7))
3-element Vector{Int64}:
2
5
7
```
"""
function maximum_independent_set end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function maximum_independent_set(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
return maximum_clique(complement(g))
end

"""
independence_number(g)
Returns the size of the largest independent set of the undirected graph `g`.
```jldoctest
julia> using Graphs
julia> independence_number(cycle_graph(7))
3
```
"""
function independence_number end
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
@traitfn function independence_number(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
return clique_number(complement(g))
end
3 changes: 3 additions & 0 deletions src/independentset/degree_ind_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ adjacent to the fewest valid vertices in the independent set until all vertices
### Performance
Runtime: O((|V|+|E|)*log(|V|))
Memory: O(|V|)
### See also
[`maximum_independent_set`](@ref)
"""
function independent_set(
g::AbstractGraph{T},
Expand Down
3 changes: 3 additions & 0 deletions src/independentset/maximal_ind_set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Approximation Factor: maximum(degree(g))+1
### Optional Arguments
- If `seed >= 0`, a random generator is seeded with this value.
### See also
[`maximum_independent_set`](@ref)
"""
function independent_set(
g::AbstractGraph{T},
Expand Down
3 changes: 3 additions & 0 deletions test/community/cliques.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@
for g in testgraphs(h)
@test test_cliques(h, Array[[7, 4, 5], [2, 6], [3, 5], [3, 6], [3, 1]])
end

@test clique_number(cycle_graph(11)) == 2
@test independence_number(cycle_graph(11)) == 5
end

0 comments on commit f7c165b

Please sign in to comment.