Skip to content

Max Clique Example

Download Notebook


The Maximum Clique problem finds the largest complete subgraph in a given graph. It is NP-hard and has applications in social network analysis, bioinformatics, and coding theory.

import getpass
import os

import numpy as np
from dotenv import load_dotenv
from luna_quantum.algorithms import SCIP

from luna_usecases.max_clique import (
    MaxCliqueCollection,
    MaxCliqueData,
    MaxCliqueFormulation,
    MaxCliqueInstance,
)

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 clique is a fully connected subgraph.

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 = MaxCliqueData.from_adjacency_matrix(adjacency_matrix=adj, node_names=node_names)
print(data.to_string())
Max Clique Data:
  Nodes: 5
  Edges: 6

Plot Data

Visualize the graph structure.

data.plot()

<Axes: title={'center': 'Max Clique — 5 nodes, 6 edges'}>
png

Create Formulation

Maximize the number of selected mutually connected nodes.

formulation = MaxCliqueFormulation()
print(formulation.to_string(data))
Max Clique Formulation:
  Nodes: 5

Decision Variables:
  x[i] in {0,1} for i = 0, ..., 4
  x[i] = 1 if node i is in the clique
  Total: 5 binary variables

Objective:
  maximize sum_i x[i]

Constraints:
  1. Non-edge exclusion (4 constraints):
     x[i] + x[j] <= 1  for all non-edges (i,j)

Create Instance

Combine data and formulation into a solvable instance.

instance = MaxCliqueInstance(data=data, formulation=formulation)
print(instance.to_string())
Data:Max Clique Data:
  Nodes: 5
  Edges: 6
Formulation:Max Clique Formulation:
  Nodes: 5

Decision Variables:
  x[i] in {0,1} for i = 0, ..., 4
  x[i] = 1 if node i is in the clique
  Total: 5 binary variables

Objective:
  maximize sum_i x[i]

Constraints:
  1. Non-edge exclusion (4 constraints):
     x[i] + x[j] <= 1  for all non-edges (i,j)

Formulate Model

Translate the instance into a mathematical optimization model.

model = instance.formulate()

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')






2026-05-29 11:35:07 INFO     Sleeping for 5.0 seconds. Waiting and checking a function in a loop.




Max Clique Solution:
  Clique size: 3
  Valid: True
  Clique nodes: ['A', 'B', 'C']

Plot Solution

Visualize the optimal solution.

uc_solution.plot(data)

<Axes: title={'center': 'Max Clique — size=3, valid=True'}>
png

Collections

Generate a benchmark collection of random instances for batch processing.

collection = MaxCliqueCollection.from_random(min_nodes=4, max_nodes=6, num_instances=2, seed=42)
model = collection.instances[0].formulate()
print(model)
Model: max_clique<max_clique>
Maximize
  x_0 + x_1 + x_2 + x_3
Subject To
  non_edge_1_2: x_1 + x_2 <= 1
  non_edge_1_3: x_1 + x_3 <= 1
Binary
  x_0 x_1 x_2 x_3