-
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.
* add vertex cover * add vertex cover * fix test and rename method * docs * fix docs + small fixes * fix missing docstring --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
- Loading branch information
1 parent
d87b65d
commit f2cf29d
Showing
5 changed files
with
100 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,67 @@ | ||
""" | ||
min_vertex_cover!( | ||
model, g; | ||
var_name | ||
) | ||
Modify a JuMP model by adding the variable, constraints and objective necessary to compute a minimum vertex cove of an undirected graph. | ||
The vertex cover indicator variable will be named `var_name` | ||
""" | ||
function min_vertex_cover!(model::Model, g::AbstractGraph; integer::Bool, var_name) | ||
if is_directed(g) | ||
throw(ArgumentError("The graph must not be directed")) | ||
end | ||
|
||
ver = collect(vertices(g)) | ||
|
||
f = @variable(model, [ver]; integer=integer, base_name=String(var_name)) | ||
model[Symbol(var_name)] = f | ||
|
||
for v in ver | ||
@constraint(model, f[v] >= 0) | ||
@constraint(model, f[v] <= 1) | ||
end | ||
|
||
for e in edges(g) | ||
@constraint(model, f[src(e)] + f[dst(e)] >= 1) | ||
end | ||
|
||
obj = objective_function(model) | ||
for v in ver | ||
add_to_expression!(obj, f[v]) | ||
end | ||
@objective(model, Min, obj) | ||
|
||
return model | ||
end | ||
|
||
""" | ||
min_vertex_cover( | ||
g, optimizer | ||
) | ||
Compute a minimum vertex cover of an undirected graph. | ||
# Arguments | ||
- `g::Graphs.AbstractGraph`: an undirected graph `G = (V, E)` | ||
# Keyword arguments | ||
- `optimizer`: JuMP-compatible solver (default is `HiGHS.Optimizer`) | ||
""" | ||
function min_vertex_cover(g::AbstractGraph; integer::Bool=true, optimizer=HiGHS.Optimizer) | ||
model = Model(optimizer) | ||
set_silent(model) | ||
|
||
min_vertex_cover!(model, g; integer=integer, var_name=:cover) | ||
|
||
optimize!(model) | ||
@assert termination_status(model) == OPTIMAL | ||
|
||
cover_values = Vector(value.(model[:cover])) | ||
cover_vertices = findall(==(1), cover_values) | ||
|
||
return cover_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,18 @@ | ||
using Graphs: SimpleGraph | ||
using GraphsOptim | ||
using Test | ||
|
||
adj_matrix = [ | ||
0 1 1 0 0 0 | ||
1 0 1 1 1 1 | ||
1 1 0 0 0 0 | ||
0 1 0 0 0 0 | ||
0 1 0 0 0 0 | ||
0 1 0 0 0 0 | ||
] | ||
|
||
expected_ans_options = [Set([1, 2]), Set([2, 3])] | ||
|
||
graph = SimpleGraph(adj_matrix) | ||
|
||
@test Set(min_vertex_cover(graph)) in expected_ans_options |
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