Skip to content

Commit f7c165b

Browse files
committed
Added clique_number, independence_number
1 parent a27730a commit f7c165b

File tree

5 files changed

+121
-1
lines changed

5 files changed

+121
-1
lines changed

src/Graphs.jl

+3-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ barabasi_albert!, static_fitness_model, static_scale_free, kronecker, dorogovtse
120120
#community
121121
modularity, core_periphery_deg,
122122
local_clustering,local_clustering_coefficient, global_clustering_coefficient, triangles,
123-
label_propagation, maximal_cliques, clique_percolation, assortativity,rich_club,
123+
maximal_cliques, maximum_clique, clique_number,
124+
maximal_independent_sets, maximum_independent_set, independence_number,
125+
label_propagation, clique_percolation, assortativity,rich_club,
124126

125127
#generators
126128
complete_graph, star_graph, path_graph, wheel_graph, cycle_graph,

src/community/cliques.jl

+109
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,112 @@ function maximal_cliques end
137137
end
138138
return cliques
139139
end
140+
141+
"""
142+
maximum_clique(g)
143+
144+
Return a vector representing the node indices of a maximum clique
145+
of the undirected graph `g`.
146+
147+
```jldoctest
148+
julia> using Graphs
149+
150+
julia> maximum_clique(blockdiag(complete_graph(3), complete_graph(4)))
151+
4-element Vector{Int64}:
152+
4
153+
5
154+
6
155+
7
156+
```
157+
"""
158+
function maximum_clique end
159+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
160+
@traitfn function maximum_clique(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
161+
return sort(argmax(length, maximal_cliques(g)))
162+
end
163+
164+
"""
165+
clique_number(g)
166+
167+
Returns the size of the largest clique of the undirected graph `g`.
168+
169+
```jldoctest
170+
julia> using Graphs
171+
172+
julia> clique_number(blockdiag(complete_graph(3), complete_graph(4)))
173+
4
174+
```
175+
"""
176+
function clique_number end
177+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
178+
@traitfn function clique_number(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
179+
return maximum(length, maximal_cliques(g))
180+
end
181+
182+
"""
183+
maximal_independent_sets(g)
184+
185+
Return a vector of vectors representing the node indices in each of the maximal
186+
independent sets found in the undirected graph `g`.
187+
188+
```jldoctest
189+
julia> using Graphs
190+
191+
julia> maximal_independent_sets(cycle_graph(5))
192+
5-element Vector{Vector{Int64}}:
193+
[5, 2]
194+
[5, 3]
195+
[2, 4]
196+
[1, 4]
197+
[1, 3]
198+
```
199+
"""
200+
function maximal_independent_sets end
201+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
202+
@traitfn function maximal_independent_sets(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
203+
return maximal_cliques(complement(g))
204+
end
205+
206+
"""
207+
maximum_independent_set(g)
208+
209+
Return a vector representing the node indices of a maximum independent set
210+
of the undirected graph `g`.
211+
212+
### See also
213+
[`independent_set`](@ref)
214+
215+
## Examples
216+
```jldoctest
217+
julia> using Graphs
218+
219+
julia> maximum_independent_set(cycle_graph(7))
220+
3-element Vector{Int64}:
221+
2
222+
5
223+
7
224+
```
225+
"""
226+
function maximum_independent_set end
227+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
228+
@traitfn function maximum_independent_set(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
229+
return maximum_clique(complement(g))
230+
end
231+
232+
"""
233+
independence_number(g)
234+
235+
Returns the size of the largest independent set of the undirected graph `g`.
236+
237+
```jldoctest
238+
julia> using Graphs
239+
240+
julia> independence_number(cycle_graph(7))
241+
3
242+
```
243+
"""
244+
function independence_number end
245+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
246+
@traitfn function independence_number(g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}}
247+
return clique_number(complement(g))
248+
end

src/independentset/degree_ind_set.jl

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ adjacent to the fewest valid vertices in the independent set until all vertices
1717
### Performance
1818
Runtime: O((|V|+|E|)*log(|V|))
1919
Memory: O(|V|)
20+
21+
### See also
22+
[`maximum_independent_set`](@ref)
2023
"""
2124
function independent_set(
2225
g::AbstractGraph{T},

src/independentset/maximal_ind_set.jl

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ Approximation Factor: maximum(degree(g))+1
1919
2020
### Optional Arguments
2121
- If `seed >= 0`, a random generator is seeded with this value.
22+
23+
### See also
24+
[`maximum_independent_set`](@ref)
2225
"""
2326
function independent_set(
2427
g::AbstractGraph{T},

test/community/cliques.jl

+3
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@
5252
for g in testgraphs(h)
5353
@test test_cliques(h, Array[[7, 4, 5], [2, 6], [3, 5], [3, 6], [3, 1]])
5454
end
55+
56+
@test clique_number(cycle_graph(11)) == 2
57+
@test independence_number(cycle_graph(11)) == 5
5558
end

0 commit comments

Comments
 (0)