Skip to content

Commit 2cfcb1a

Browse files
dstahlkeKrastanov
andauthored
Added clique_number, independence_number (#155)
Co-authored-by: Stefan Krastanov <github.acc@krastanov.org>
1 parent 5eaa071 commit 2cfcb1a

File tree

7 files changed

+174
-1
lines changed

7 files changed

+174
-1
lines changed

src/Graphs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ export
317317
label_propagation,
318318
louvain,
319319
maximal_cliques,
320+
maximum_clique,
321+
clique_number,
322+
maximal_independent_sets,
323+
maximum_independent_set,
324+
independence_number,
320325
clique_percolation,
321326
assortativity,
322327
rich_club,

src/community/cliques.jl

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,125 @@ 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+
The graph will be converted to SimpleGraph at the start of the computation.
196+
197+
```jldoctest
198+
julia> using Graphs
199+
200+
julia> maximal_independent_sets(cycle_graph(5))
201+
5-element Vector{Vector{Int64}}:
202+
[5, 2]
203+
[5, 3]
204+
[2, 4]
205+
[1, 4]
206+
[1, 3]
207+
```
208+
"""
209+
function maximal_independent_sets end
210+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
211+
@traitfn function maximal_independent_sets(
212+
g::AG::(!IsDirected)
213+
) where {T,AG<:AbstractGraph{T}}
214+
# Convert to SimpleGraph first because `complement` doesn't accept AbstractGraph.
215+
return maximal_cliques(complement(SimpleGraph(g)))
216+
end
217+
218+
"""
219+
maximum_independent_set(g)
220+
221+
Return a vector representing the node indices of a maximum independent set
222+
of the undirected graph `g`.
223+
224+
The graph will be converted to SimpleGraph at the start of the computation.
225+
226+
### See also
227+
[`independent_set`](@ref)
228+
229+
## Examples
230+
```jldoctest
231+
julia> using Graphs
232+
233+
julia> maximum_independent_set(cycle_graph(7))
234+
3-element Vector{Int64}:
235+
2
236+
5
237+
7
238+
```
239+
"""
240+
function maximum_independent_set end
241+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
242+
@traitfn function maximum_independent_set(
243+
g::AG::(!IsDirected)
244+
) where {T,AG<:AbstractGraph{T}}
245+
# Convert to SimpleGraph first because `complement` doesn't accept AbstractGraph.
246+
return maximum_clique(complement(SimpleGraph(g)))
247+
end
248+
249+
"""
250+
independence_number(g)
251+
252+
Returns the size of the largest independent set of the undirected graph `g`.
253+
254+
The graph will be converted to SimpleGraph at the start of the computation.
255+
256+
```jldoctest
257+
julia> using Graphs
258+
259+
julia> independence_number(cycle_graph(7))
260+
3
261+
```
262+
"""
263+
function independence_number end
264+
# see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax
265+
@traitfn function independence_number(g::AG::(!IsDirected)) where {T,AG<:AbstractGraph{T}}
266+
# Convert to SimpleGraph first because `complement` doesn't accept AbstractGraph.
267+
return clique_number(complement(SimpleGraph(g)))
268+
end

src/independentset/degree_ind_set.jl

Lines changed: 3 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 1 deletion
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

Lines changed: 34 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ tests = [
114114
"traversals/all_simple_paths",
115115
"community/cliques",
116116
"community/core-periphery",
117+
"community/independent_sets",
117118
"community/label_propagation",
118119
"community/louvain",
119120
"community/modularity",

0 commit comments

Comments
 (0)