add_random_edges

add_random_edges#

graph_tool.generation.add_random_edges(g, M, parallel=False, self_loops=False, weight=None)[git][source]#

Add new edges to a graph, chosen uniformly at random.

Parameters:
gGraph

Graph to be modified.

Mint

Number of edges to be added. If parallel == False, it cannot exceed the number of vertex pairs that are not yet connected (otherwise a ValueError is raised).

parallelbool (optional, default: False)

Whether to allow parallel edges to be added.

self_loopsbool (optional, default: False)

Whether to allow self-loops to be added.

weightEdgePropertyMap (optional, default: None)

Integer edge multiplicities. If supplied, this will be incremented for edges already in the graph, instead of new edges being added.

See also

remove_random_edges

Remove random edges from the graph

Notes

If the graph is not being filtered, this algorithm runs in time \(O(M)\) if parallel == True or \(O(M\left<k\right>)\) if parallel == False, where \(\left<k\right>\) is the average degree of the graph.

For filtered graphs, this algorithm runs in time \(O(M + E)\) if parallel == True or \(O(M\left<k\right> + E)\) if parallel == False, where \(E\) is the number of edges in the graph.

Examples

Generating a Newman–Watts–Strogatz small-world graph:

>>> g = gt.circular_graph(100)
>>> gt.add_random_edges(g, 30)

The graph is drawn as follows, with the vertices placed on a circle in the order of their indices and the new edges highlighted:

>>> x = 2 * np.pi * np.arange(100) / 100
>>> pos = g.new_vp("vector<double>",
...                vals=np.array([np.cos(x), np.sin(x)]).T)
>>> new = g.new_ep("int", vals=np.arange(g.num_edges()) >= 100)
>>> cmap = matplotlib.colors.ListedColormap(["#999999", "#e41a1c"])
>>> gt.graph_draw(g, pos=pos, vertex_size=4, edge_color=new, ecmap=cmap,
...               output="add_random_edges-nws.pdf")
<...>
../_images/add_random_edges-nws.png

Newman–Watts–Strogatz graph obtained by adding 30 random edges to a cycle of 100 vertices, placed on a circle in the order of their indices. The added edges are red, and the edges of the cycle gray.#