Skip to content

Commit 470df1c

Browse files
committed
Added clique_number, independence_number
1 parent 42ba34e commit 470df1c

File tree

7 files changed

+168
-1
lines changed

7 files changed

+168
-1
lines changed

src/Graphs.jl

+5
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,11 @@ export
314314
triangles,
315315
label_propagation,
316316
maximal_cliques,
317+
maximum_clique,
318+
clique_number,
319+
maximal_independent_sets,
320+
maximum_independent_set,
321+
independence_number,
317322
clique_percolation,
318323
assortativity,
319324
rich_club,

src/community/cliques.jl

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

src/independentset/maximal_ind_set.jl

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Approximation Factor: maximum(degree(g))+1
2020
### Optional Arguments
2121
- `rng=nothing`: set the Random Number Generator.
2222
- If `seed >= 0`, a random generator is seeded with this value.
23+
24+
### See also
25+
[`maximum_independent_set`](@ref)
2326
"""
2427
function independent_set(
2528
g::AbstractGraph{T},

test/community/cliques.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212

1313
function test_cliques(graph, expected)
1414
# Make test results insensitive to ordering
15-
return setofsets(@inferred(maximal_cliques(graph))) == setofsets(expected)
15+
okay_maximal = setofsets(@inferred(maximal_cliques(graph))) == setofsets(expected)
16+
okay_maximum = Set(@inferred(maximum_clique(graph))) in setofsets(expected)
17+
okay_maximum2 =
18+
length(@inferred(maximum_clique(graph))) == maximum(length.(expected))
19+
okay_number = @inferred(clique_number(graph)) == maximum(length.(expected))
20+
return okay_maximal && okay_maximum && okay_maximum2 && okay_number
1621
end
1722

1823
gx = SimpleGraph(3)

test/community/independent_sets.jl

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
##################################################################
2+
#
3+
# Maximal independent sets of undirected graph
4+
# Derived from Graphs.jl: https://github.com/julialang/Graphs.jl
5+
#
6+
##################################################################
7+
8+
@testset "Independent Sets" begin
9+
function setofsets(array_of_arrays)
10+
return Set(map(Set, array_of_arrays))
11+
end
12+
13+
function test_independent_sets(graph, expected)
14+
# Make test results insensitive to ordering
15+
okay_maximal =
16+
setofsets(@inferred(maximal_independent_sets(graph))) == setofsets(expected)
17+
okay_maximum = Set(@inferred(maximum_independent_set(graph))) in setofsets(expected)
18+
okay_maximum2 =
19+
length(@inferred(maximum_independent_set(graph))) == maximum(length.(expected))
20+
okay_number = @inferred(independence_number(graph)) == maximum(length.(expected))
21+
return okay_maximal && okay_maximum && okay_maximum2 && okay_number
22+
end
23+
24+
gx = SimpleGraph(3)
25+
add_edge!(gx, 1, 2)
26+
for g in test_generic_graphs(gx)
27+
@test test_independent_sets(g, Array[[1, 3], [2, 3]])
28+
end
29+
add_edge!(gx, 2, 3)
30+
for g in test_generic_graphs(gx)
31+
@test test_independent_sets(g, Array[[1, 3], [2]])
32+
end
33+
@test independence_number(cycle_graph(11)) == 5
34+
end

test/runtests.jl

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ tests = [
128128
"traversals/eulerian",
129129
"community/cliques",
130130
"community/core-periphery",
131+
"community/independent_sets",
131132
"community/label_propagation",
132133
"community/modularity",
133134
"community/clustering",

0 commit comments

Comments
 (0)