generative_models
Algorithms to generate random graphs.
The functions in this module allow to generate graphs based on different probabilistic generative models.
erdos_renyi_gnm
¶
Generate a random graph with n nodes and m edges based on the G(n,m) model by Pal Eröds and Alfred Renyi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
the number of nodes of the graph |
required |
m
|
int
|
the number of random directed or undirected edges to be generated |
required |
mapping
|
pathpyG.core.index_map.IndexMap | None
|
optional given mapping of n nodes to node IDs. If this is not given a mapping is created |
None
|
self_loops
|
bool
|
whether or not to allow self-loops (v,v) to be generated |
False
|
multi_edges
|
bool
|
whether or not multiple identical edges are allowed |
False
|
directed
|
bool
|
whether or not to generate a directed graph |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A random graph based on the G(n,m) model. |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnm_randomize
¶
Generate a randomized version of a given graph based on the Erdös-Renyi random graph G(n,m) model.
Generate a random graph whose number of nodes, edges, edge directedness and node IDs match the corresponding values of a given network instance. Useful to generate a randomized version of a network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
A given network used to determine number of nodes, edges, node uids, and edge directedness |
required |
self_loops
|
bool
|
Whether or not the generated network can contain loops. |
False
|
multi_edges
|
bool
|
Whether or not multiple edges can be added to the same node pair |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A randomized version of the given graph based on the G(n,m) model. |
Example
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnp
¶
Generate an Erdös-Renyi random graph with n nodes and link probability p, using the G(n,p) model by Edgar Nelson Gilbert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
the number of nodes of the graph |
required |
p
|
float
|
the link probability |
required |
mapping
|
pathpyG.core.index_map.IndexMap | None
|
optional given mapping of n nodes to node IDs. If this is not given a mapping is created |
None
|
self_loops
|
bool
|
whether or not to allow self-loops (v,v) to be generated |
False
|
directed
|
bool
|
whether or not to generate a directed graph |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A random graph based on the G(n,p) model. |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnp_likelihood
¶
Calculate the likelihood of parameter p for a G(n,p) model and a given undirected graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
The link probability. |
required |
graph
|
pathpyG.core.graph.Graph
|
The undirected graph. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The likelihood of parameter p for the G(n,p) model and the given graph. |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnp_log_likelihood
¶
Calculate the log-likelihood of parameter p for a G(n,p) model and a given undirected graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
float
|
The link probability. |
required |
graph
|
pathpyG.core.graph.Graph
|
The undirected graph. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The log-likelihood of parameter p for the G(n,p) model and the given graph. |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnp_mle
¶
Calculate the maximum likelihood estimate of parameter p for a G(n,p) model and a given undirected graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
The undirected graph. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The maximum likelihood estimate of parameter p for the G(n,p) model and the given graph. |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnp_randomize
¶
Randomize a given graph based on the Erdös-Renyi random graph G(n,p) model.
The number of nodes, expected number of edges, edge directedness and node uids of the generated graph match the corresponding values of the graph given as parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
A given network used to determine number of nodes, edges, node uids, and edge directedness |
required |
self_loops
|
bool
|
Whether or not the generated network can contain loops. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A randomized version of the given graph based on the G(n,p) model. |
Source code in src/pathpyG/algorithms/generative_models.py
generate_degree_sequence
¶
Generates a random graphic degree sequence drawn from a given degree distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of nodes for which to generate the degree sequence. |
required |
distribution
|
typing.Dict[float, float] | scipy.stats.rv_continuous | scipy.stats.rv_discrete
|
The degree distribution to draw from. Can be either a dictionary specifying a custom discrete distribution (keys are degrees, values are probabilities), or a scipy.stats.rv_continuous or scipy.stats.rv_discrete object. |
required |
**distribution_args
|
typing.Any
|
Additional keyword arguments passed to the rvs method of the distribution. |
{}
|
Returns:
| Type | Description |
|---|---|
numpy.ndarray
|
_np.ndarray: A graphic degree sequence drawn from the given distribution. |
Source code in src/pathpyG/algorithms/generative_models.py
is_graphic_erdos_gallai
¶
Check Erdös and Gallai condition.
Checks whether the condition by Erdös and Gallai1 for a graphic degree sequence is fulfilled.
-
ErdÅ‘s, P.; Gallai, T. (1960), "Gráfok elÅ‘Ãrt fokszámú pontokkal", Matematikai Lapok, 11: 264-274 ↩
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degrees
|
list[int] | numpy.ndarray
|
List of integer node degrees to be tested. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the degree sequence is graphic, False otherwise. |
Source code in src/pathpyG/algorithms/generative_models.py
k_regular_random
¶
Generate a random graph in which all nodes have exactly degree \(k\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
degree of all nodes in the generated network. |
required |
n
|
typing.Optional[int]
|
number of nodes in the generated network. |
None
|
node_ids
|
typing.Optional[list]
|
Optional list of node uids that will be used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
typing.Optional[pathpyG.core.graph.Graph]
|
A random \(k\)-regular graph. |
Source code in src/pathpyG/algorithms/generative_models.py
max_edges
¶
Returns the maximum number of edges that a directed or undirected network with n nodes can possible have (with or without loops).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of nodes in the network |
required |
directed
|
bool
|
If True, return the maximum number of edges in a directed network. |
False
|
multi_edges
|
bool
|
If True, multiple edges between each node pair are allowed. In this case np.inf is returned. |
False
|
self_loops
|
bool
|
If True, include self-loops. |
False
|
Returns:
| Type | Description |
|---|---|
int | float
|
The maximum number of edges in the network. |
Examples:
Compute maximum number of edges in undirected network without self-loops and 100 nodes
Directed networks without self-loops
Directed networks with self-loops
Source code in src/pathpyG/algorithms/generative_models.py
molloy_reed
¶
Generate Molloy-Reed graph.
Generates a random undirected network without self-loops, with given degree sequence based on the Molloy-Reed algorithm. The condition proposed by Erdös and Gallai (1967) is used to test whether the degree sequence is graphic, i.e. whether a network with the given degree sequence exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
degree_sequence
|
numpy.ndarray | list[int]
|
List of integer node degrees. The number of nodes of the generated
network corresponds to |
required |
multiedge
|
bool
|
If True, allow multiple edges between the same node pair. |
False
|
relax
|
bool
|
If True, we conceptually allow self-loops and multi-edges, but do not
add them to the network. This implies that the generated graph may not
have exactly |
False
|
node_ids
|
Optional list of node IDs that will be used for Indexmapping. |
required |
Examples:
Generate random undirected network with given degree sequence
>>> import pathpyG as pp
>>> random_network = pp.algorithms.generative_models.molloy_reed([1, 1])
>>> print(random_network)
Undirected graph with 2 nodes and 1 edges
{'Edge Attributes': {}, 'Graph Attributes': {'num_nodes': "<class 'int'>"}, 'Node Attributes': {}}
Network generation fails for non-graphic degree sequence
>>> import pathpyG as pp
>>> try:
... random_network = pp.algorithms.generative_models.molloy_reed([1, 0])
... except ValueError:
... print("Caught expected ValueError for non-graphic degree sequence")
Caught expected ValueError for non-graphic degree sequence
Source code in src/pathpyG/algorithms/generative_models.py
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
molloy_reed_randomize
¶
Generates a randomized realization of a given undirected network based on the observed degree sequence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
pathpyG.core.graph.Graph
|
A given undirected network. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
typing.Optional[pathpyG.core.graph.Graph]
|
A randomized version of the given graph based on the Molloy-Reed model |
Source code in src/pathpyG/algorithms/generative_models.py
stochastic_block_model
¶
Generate a random undirected graph based on the stochastic block model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
M
|
numpy.matrix
|
n x n stochastic block matrix, where entry M[i,j] gives probability of edge to be generated between nodes in blocks i and j |
required |
z
|
numpy.ndarray
|
n-dimensional block assignment vector, where z[i] gives block assignment of i-th node |
required |
mapping
|
typing.Optional[pathpyG.core.index_map.IndexMap]
|
optional mapping of node IDs to indices. If not given, a standard mapping based on integer IDs will be created |
None
|
Source code in src/pathpyG/algorithms/generative_models.py
watts_strogatz
¶
Generate a Watts-Strogatz small-world graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of nodes in the graph. |
required |
s
|
int
|
The number of edges to attach from a new node to existing nodes. |
required |
p
|
float
|
The probability of rewiring each edge. |
0.0
|
undirected
|
bool
|
If True, the graph will be undirected. |
True
|
allow_duplicate_edges
|
bool
|
If True, allow duplicate edges in the graph. This is faster but may result in fewer edges than requested in the undirected case or duplicates in the directed case. |
True
|
allow_self_loops
|
bool
|
If True, allow self-loops in the graph. This is faster but may result in fewer edges than requested in the undirected case. |
True
|
mapping
|
pathpyG.core.index_map.IndexMap | None
|
A mapping from the node indices to node names. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Graph |
pathpyG.core.graph.Graph
|
A Watts-Strogatz small-world graph. |
Examples:
Source code in src/pathpyG/algorithms/generative_models.py
259 260 261 262 263 264 265 266 267 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 | |