-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added maximum (weight) cliques (#21)
* format * include test * Add to obj --------- Co-authored-by: Guillaume Dalle <[email protected]>
- Loading branch information
1 parent
2295e2e
commit 1dd2f5e
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
|
||
""" | ||
maximum_weight_clique!(model, g; var_name) | ||
Computes in-place in the JuMP model a maximum-weighted clique of `g`. | ||
An optional `vertex_weights` vector can be passed to the graph, defaulting to uniform weights (computing a maximum size clique). | ||
""" | ||
function maximum_weight_clique!( | ||
model::Model, g::AbstractGraph; binary::Bool=true, var_name, vertex_weights=ones(nv(g)) | ||
) | ||
if is_directed(g) | ||
throw(ArgumentError("The graph must not be directed")) | ||
end | ||
g_vertices = collect(vertices(g)) | ||
f = @variable(model, [g_vertices]; binary=binary, base_name=String(var_name)) | ||
model[Symbol(var_name)] = f | ||
@constraint( | ||
model, | ||
packing_constraint[i=1:nv(g), j=1:nv(g); i ≠ j && !has_edge(g, i, j)], | ||
f[i] + f[j] <= 1, | ||
) | ||
obj = objective_function(model) | ||
add_to_expression!(obj, dot(f, vertex_weights)) | ||
@objective(model, Max, obj) | ||
return model | ||
end | ||
|
||
""" | ||
maximum_weight_clique(g; optimizer, binary, vertex_weights) | ||
Computes a maximum-weighted clique of `g`. | ||
""" | ||
function maximum_weight_clique( | ||
g::AbstractGraph; | ||
binary::Bool=true, | ||
vertex_weights=ones(nv(g)), | ||
optimizer=HiGHS.Optimizer, | ||
) | ||
model = Model(optimizer) | ||
set_silent(model) | ||
maximum_weight_clique!( | ||
model, g; binary=binary, vertex_weights=vertex_weights, var_name=:clique | ||
) | ||
optimize!(model) | ||
@assert termination_status(model) == OPTIMAL | ||
clique_variables = Vector(model[:clique]) | ||
clique_vertices = findall(v -> value(v) > 0.5, clique_variables) | ||
return clique_vertices | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using GraphsOptim | ||
using Graphs | ||
using Test | ||
|
||
g = Graphs.random_regular_graph(10, 5) | ||
|
||
for _ in 1:10 | ||
vertex_weights = rand(nv(g)) | ||
clique = GraphsOptim.maximum_weight_clique(g; vertex_weights=vertex_weights) | ||
if length(clique) > 1 | ||
for idx in 1:(length(clique) - 1) | ||
@test Graphs.has_edge(g, clique[idx], clique[idx + 1]) | ||
end | ||
end | ||
end | ||
|
||
g2 = complete_graph(3) | ||
add_vertex!(g2) | ||
add_edge!(g2, 3, 4) | ||
clique = GraphsOptim.maximum_weight_clique(g2) | ||
@test sort(clique) == 1:3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters