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:
- g
Graph Graph to be modified.
- M
int Number of edges to be added. If
parallel == False, it cannot exceed the number of vertex pairs that are not yet connected (otherwise aValueErroris raised).- parallel
bool(optional, default:False) Whether to allow parallel edges to be added.
- self_loops
bool(optional, default:False) Whether to allow self-loops to be added.
- weight
EdgePropertyMap(optional, default:None) Integer edge multiplicities. If supplied, this will be incremented for edges already in the graph, instead of new edges being added.
- g
See also
remove_random_edgesRemove random edges from the graph
Notes
If the graph is not being filtered, this algorithm runs in time \(O(M)\) if
parallel == Trueor \(O(M\left<k\right>)\) ifparallel == 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 == Trueor \(O(M\left<k\right> + E)\) ifparallel == 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") <...>
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.#