Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vertex cover #19

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/src/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ min_cost_assignment
GraphsOptim.min_cost_assignment!
```

## Minimum Vertex Cover

```@docs
min_vertex_cover
olegfafurin marked this conversation as resolved.
Show resolved Hide resolved
```

Finds a subset $S \subset V$ of vertices of an undirected graph $G = (V,E)$ such that $\forall (u,v) \in E: u \in S \lor v \in S$

## Graph matching

!!! danger "Work in progress"
Expand Down
2 changes: 2 additions & 0 deletions src/GraphsOptim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ using OptimalTransport: sinkhorn
export min_cost_flow
export min_cost_assignment
export FAQ, GOAT, graph_matching
export min_vertex_cover
export fractional_chromatic_number, fractional_clique_number
export shortest_path

include("utils.jl")
include("flow.jl")
include("assignment.jl")
include("graph_matching.jl")
include("vertex_cover.jl")
include("fractional_coloring.jl")
include("shortest_path.jl")

Expand Down
68 changes: 68 additions & 0 deletions src/vertex_cover.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
vertex_cover!(
olegfafurin marked this conversation as resolved.
Show resolved Hide resolved
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; var_name)
if is_directed(g)
throw(ArgumentError("The graph must not be directed"))
end

ver = collect(vertices(g))
edge_tuples = [(src(ed), dst(ed)) for ed in edges(g)]

f = @variable(model, [ver]; integer=true, base_name=String(var_name))
gdalle marked this conversation as resolved.
Show resolved Hide resolved
model[Symbol(var_name)] = f

for v in ver
@constraint(model, f[v] >= 0)
@constraint(model, f[v] <= 1)
end

for (u, v) in edge_tuples
olegfafurin marked this conversation as resolved.
Show resolved Hide resolved
@constraint(model, f[u] + f[v] >= 1)
end

obj = objective_function(model)
for v in ver
add_to_expression!(obj, f[v])
end
@objective(model, Min, obj)

return model
end

"""
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; optimizer=HiGHS.Optimizer)
model = Model(optimizer)
set_silent(model)

min_vertex_cover!(model, g; var_name=:cover)

optimize!(model)
@assert termination_status(model) == OPTIMAL

cover_values = Vector(value.(model[:cover]))
gdalle marked this conversation as resolved.
Show resolved Hide resolved
cover_vertices = findall(==(1), cover_values)

return cover_vertices
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ using Test
include("graph_matching.jl")
end

@testset verbose = true "Vertex cover" begin
include("vertex_cover.jl")
end

@testset verbose = true "Fractional coloring" begin
include("fractional_coloring.jl")
end
Expand Down
18 changes: 18 additions & 0 deletions test/vertex_cover.jl
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([Set([1, 2]), Set([2, 3])])
olegfafurin marked this conversation as resolved.
Show resolved Hide resolved

graph = SimpleGraph(adj_matrix)

@test Set(min_vertex_cover(graph)) in expected_ans_options
Loading