Minimum Vertex Cover Example
The Minimum Vertex Cover problem finds the smallest set of vertices such that every edge has at least one endpoint in the set. It is NP-hard and one of Karp's 21 NP-complete problems.
import getpass
import os
import numpy as np
from dotenv import load_dotenv
from local_solver.scip import SCIP
from luna_usecases.minimum_vertex_cover import (
MinimumVertexCoverCollection,
MinimumVertexCoverData,
MinimumVertexCoverFormulation,
MinimumVertexCoverInstance,
)
load_dotenv()
if "LUNA_API_KEY" not in os.environ:
os.environ["LUNA_API_KEY"] = getpass.getpass("Enter your Luna API key: ")
Create Data
Define a 5-node graph. A vertex cover touches every edge.
adj = np.array(
[
[0, 1, 1, 0, 0],
[1, 0, 1, 1, 0],
[1, 1, 0, 0, 1],
[0, 1, 0, 0, 1],
[0, 0, 1, 1, 0],
]
)
node_names = ["A", "B", "C", "D", "E"]
data = MinimumVertexCoverData.from_adjacency_matrix(adjacency_matrix=adj, node_names=node_names)
print(data.to_string())
Plot Data
Visualize the graph structure.
Create Formulation
Minimize selected nodes while ensuring every edge has at least one endpoint selected.
Minimum Vertex Cover Formulation:
Nodes: 5
Edges: 6
Decision Variables:
x[i] in {0,1} for i = 0, ..., 4
x[i] = 1 if node i is in the vertex cover
Total: 5 binary variables
Objective:
minimize sum_i x[i]
Constraints:
1. Edge coverage (6 constraints):
x[i] + x[j] >= 1 for all edges (i,j)
Create Instance
Combine data and formulation into a solvable instance.
instance = MinimumVertexCoverInstance(data=data, formulation=formulation)
print(instance.to_string())
Data:Minimum Vertex Cover Data:
Nodes: 5
Edges: 6
Formulation:Minimum Vertex Cover Formulation:
Nodes: 5
Edges: 6
Decision Variables:
x[i] in {0,1} for i = 0, ..., 4
x[i] = 1 if node i is in the vertex cover
Total: 5 binary variables
Objective:
minimize sum_i x[i]
Constraints:
1. Edge coverage (6 constraints):
x[i] + x[j] >= 1 for all edges (i,j)
Formulate Model
Translate the instance into a mathematical optimization model.
Solve and Interpret
Solve the model with SCIP and interpret the raw result into a use-case-specific solution.
scip = SCIP()
job = scip.run(model)
sol = job.result()
uc_solution = instance.interpret(sol)
print(uc_solution.to_string())
2026-05-13 06:40:17 INFO Running SCIP for model minimum_vertex_cover<minimum_vertex_cover>
INFO Completed SCIP optimization for model minimum_vertex_cover<minimum_vertex_cover> in 0.01s
Plot Solution
Visualize the optimal solution.
Collections
Generate a benchmark collection of random instances for batch processing.