Max Independent Set Example
The Maximum Independent Set problem finds the largest set of vertices in a graph such that no two are adjacent. It is NP-hard and complementary to the minimum vertex cover problem.
import getpass
import os
import numpy as np
from dotenv import load_dotenv
from luna_quantum.algorithms import SCIP
from luna_usecases.max_independent_set import (
MaxIndependentSetCollection,
MaxIndependentSetData,
MaxIndependentSetFormulation,
MaxIndependentSetInstance,
)
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. An independent set contains no adjacent nodes.
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 = MaxIndependentSetData.from_adjacency_matrix(adjacency_matrix=adj, node_names=node_names)
print(data.to_string())
Plot Data
Visualize the graph structure.
Create Formulation
Maximize selected nodes while ensuring no two are adjacent.
Max Independent Set 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 independent set
Total: 5 binary variables
Objective:
maximize sum_i x[i]
Constraints:
1. Edge exclusion (6 constraints):
x[i] + x[j] <= 1 for all edges (i,j)
Create Instance
Combine data and formulation into a solvable instance.
instance = MaxIndependentSetInstance(data=data, formulation=formulation)
print(instance.to_string())
Data:Max Independent Set Data:
Nodes: 5
Edges: 6
Formulation:Max Independent Set 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 independent set
Total: 5 binary variables
Objective:
maximize sum_i x[i]
Constraints:
1. Edge exclusion (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())
/Users/maximilianjanetschek/PycharmProjects/luna-usecases/.venv/lib/python3.13/site-packages/rich/live.py:260:
UserWarning: install "ipywidgets" for Jupyter support
warnings.warn('install "ipywidgets" for Jupyter support')
Plot Solution
Visualize the optimal solution.
Collections
Generate a benchmark collection of random instances for batch processing.