Graph Generators API¶
Graph generators provide convenient methods for creating test graphs and quantum graphs for research and development.
Stochastic Block Model Generators¶
Functions for generating graphs with block structure, useful for testing clustering algorithms.
kmeanssa_ng.quantum_graph.generators.generate_sbm ¶
Generate a Stochastic Block Model quantum graph.
Creates a quantum graph from a stochastic block model with uniform edge lengths and node weights.
Args: sizes: Number of nodes in each block. Defaults to [50, 50]. Must be a non-empty list of positive integers. p: Matrix of edge probabilities. Element (r, s) gives the density of edges from block r to block s. Must be symmetric for undirected graphs. Defaults to [[0.7, 0.1], [0.1, 0.7]]. Must be a square matrix with probabilities in [0, 1].
Returns: A quantum graph representing the SBM.
Raises: ValueError: If sizes is empty, contains non-positive values, or if p is not a valid probability matrix matching sizes.
Example:
# Two balanced clusters with high intra-cluster, low inter-cluster edges
graph = generate_sbm(sizes=[50, 50], p=[[0.7, 0.1], [0.1, 0.7]])
Source code in src/kmeanssa_ng/quantum_graph/generators.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | |
kmeanssa_ng.quantum_graph.generators.generate_random_sbm ¶
generate_random_sbm(
sizes: list[int] | None = None,
p: list[list[float]] | None = None,
weights: list[float] | None = None,
lengths: list[list[float]] | None = None,
) -> QuantumGraph
Generate an SBM quantum graph with block-specific edge lengths and node weights.
Args: sizes: Number of nodes in each block. Defaults to [50, 50]. p: Matrix of edge probabilities. Defaults to [[0.7, 0.1], [0.1, 0.7]]. weights: Node weight for each block. Defaults to [1, 1]. lengths: Matrix of edge lengths. Element (i, j) gives the length for edges between blocks i and j. Defaults to [[1, 4], [4, 1]].
Returns: A quantum graph with block-specific attributes.
Example:
# Two clusters with different intra/inter-cluster distances
graph = generate_random_sbm(
sizes=[50, 50],
p=[[0.7, 0.1], [0.1, 0.7]],
weights=[1, 1],
lengths=[[1, 4], [4, 1]] # Longer inter-cluster edges
)
Source code in src/kmeanssa_ng/quantum_graph/generators.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
Simple Graph Generators¶
Functions for generating simple test graphs with known structure.
kmeanssa_ng.quantum_graph.generators.generate_simple_graph ¶
generate_simple_graph(
n_a: int = 5,
n_aa: int = 3,
bridge_length: float = 2.0,
**attr,
) -> QuantumGraph
Generate a symmetric two-cluster graph connected by a bridge.
Creates a graph with two symmetric star-like clusters (A and B) connected by a single edge. Each cluster has a central node with n_a neighbors, and each neighbor has n_aa further neighbors.
Args: n_a: Number of neighbors for each central node (must be >= 0). n_aa: Number of second-level neighbors (must be >= 0). bridge_length: Length of the edge connecting the two clusters (must be > 0). **attr: Additional graph attributes.
Returns: A quantum graph with two symmetric clusters.
Raises: ValueError: If n_a, n_aa < 0 or bridge_length <= 0.
Example:
Source code in src/kmeanssa_ng/quantum_graph/generators.py
kmeanssa_ng.quantum_graph.generators.generate_simple_random_graph ¶
generate_simple_random_graph(
n_a: int = 5,
n_b: int = 5,
lam_a: int = 0,
lam_b: int = 0,
bridge_length: float = 10.0,
**attr,
) -> QuantumGraph
Generate a random two-cluster graph with Poisson branching.
Similar to generate_simple_graph but with: - Asymmetric clusters (different sizes) - Random edge lengths - Poisson-distributed third-level neighbors
Args: n_a: Number of first-level neighbors of A0. n_b: Number of first-level neighbors of B0. lam_a: Poisson parameter for A cluster third-level branching. lam_b: Poisson parameter for B cluster third-level branching. bridge_length: Mean length of the bridge edge (actual length is uniform random). **attr: Additional graph attributes.
Returns: A random quantum graph with two clusters.
Source code in src/kmeanssa_ng/quantum_graph/generators.py
NetworkX Integration¶
Utilities for converting existing NetworkX graphs to quantum graphs.
kmeanssa_ng.quantum_graph.generators.as_quantum_graph ¶
as_quantum_graph(
graph: Graph,
node_weight: float = 1.0,
edge_length: float = 1.0,
edge_weight: float = 1.0,
) -> QuantumGraph
Convert a NetworkX graph to a quantum graph with uniform attributes.
Args: graph: The NetworkX graph to convert. node_weight: Uniform weight to assign to all nodes. edge_length: Uniform length to assign to all edges. edge_weight: Uniform weight to assign to all edges.
Returns: The converted quantum graph.
Example: