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 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
|
graph object |
Source code in src/pathpyG/algorithms/generative_models.py
erdos_renyi_gnm_randomize
¶
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
|
Example:
# Generate undirected network
import pathpyG as pp
g = pp.Graph.from_edge_list([('a', 'b'), ('b', 'c'), ('d', 'e')])
r = pp.algorithms.generative_models.G_nm_randomize(g)
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 |
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
|
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 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 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
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.
Source code in src/pathpyG/algorithms/generative_models.py
generate_degree_sequence
¶
Generates a random graphic degree sequence drawn from a given degree 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 Gallai (1967) for a graphic degree sequence is fulfilled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
degrees
|
list[int] | numpy.ndarray
|
List of integer node degrees to be tested. |
required |
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 |
node_ids
|
typing.Optional[list]
|
Optional list of node uids that will be used. |
None
|
Examples:
Generate random undirected network with given degree sequence
>>> import pathpy as pp
>>> random_network = pp.algorithms.random_graphs.Molloy_Reed([1,0])
>>> print(random_network.summary())
...
Network generation fails for non-graphic sequences
>>> import pathpy as pp
>>> random_network = pp.algorithms.random_graphs.Molloy_Reed([1,0])
>>> print(random_network)
None
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
|
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 |
---|---|---|---|
degrees
|
List of integer node degrees. The number of nodes of the generated |
required | |
relax
|
bool
|
If True, we conceptually allow self-loops and multi-edges, but do not |
False
|
node_ids
|
Optional list of node IDs that will be used for Indexmapping. |
None
|
Examples:
Generate random undirected network with given degree sequence
import pathpyG as pp random_network = pp.algorithms.generative_models.molloy_reed([1,0]) print(random_network) ...
Network generation fails for non-graphic degree sequence
import pathpyG as pp random_network = pp.algorithms.generative_models.molloy_reed([1,0]) raises AttributeError
Source code in src/pathpyG/algorithms/generative_models.py
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
molloy_reed_randomize
¶
Generates a random realization of a given network based on the observed degree sequence.
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.array
|
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
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 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 |
|