def RandomGNP(n, p, seed=None, fast=True, method='Sage'): r""" Returns a random graph on `n` nodes. Each edge is inserted independently with probability `p`. INPUTS: - ``n`` -- number of nodes of the digraph - ``p`` -- probability of an edge - ``seed`` -- integer seed for random number generator (default=None). - ``fast`` -- boolean set to True (default) to use the algorithm with time complexity in `O(n+m)` proposed in [3]_. It is designed for generating large sparse graphs. It is faster than other methods for *LARGE* instances (try it to know whether it is useful for you). - ``method`` -- By default (```method='Sage'``), this function uses the method implemented in ```sage.graphs.graph_generators_pyx.pyx``. When ``method='networkx'``, this function calls the NetworkX function ``fast_gnp_random_graph``, unless ``fast=False``, then ``gnp_random_graph``. Try them to know which method is the best for you. The ``fast`` parameter is not taken into account by the 'Sage' method so far. REFERENCES: .. [1] P. Erdos and A. Renyi. On Random Graphs, Publ. Math. 6, 290 (1959). .. [2] E. N. Gilbert. Random Graphs, Ann. Math. Stat., 30, 1141 (1959). .. [3] V. Batagelj and U. Brandes. Efficient generation of large random networks. Phys. Rev. E, 71, 036113, 2005. PLOTTING: When plotting, this graph will use the default spring-layout algorithm, unless a position dictionary is specified. EXAMPLES: We show the edge list of a random graph on 6 nodes with probability `p = .4`:: sage: set_random_seed(0) sage: graphs.RandomGNP(6, .4).edges(labels=False) [(0, 1), (0, 5), (1, 2), (2, 4), (3, 4), (3, 5), (4, 5)] We plot a random graph on 12 nodes with probability `p = .71`:: sage: gnp = graphs.RandomGNP(12,.71) sage: gnp.show() # long time We view many random graphs using a graphics array:: sage: g = [] sage: j = [] sage: for i in range(9): ... k = graphs.RandomGNP(i+3,.43) ... g.append(k) ... sage: for i in range(3): ... n = [] ... for m in range(3): ... n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False)) ... j.append(n) ... sage: G = sage.plot.graphics.GraphicsArray(j) sage: G.show() # long time sage: graphs.RandomGNP(4,1) Complete graph: Graph on 4 vertices TESTS:: sage: graphs.RandomGNP(50,.2,method=50) Traceback (most recent call last): ... ValueError: 'method' must be equal to 'networkx' or to 'Sage'. sage: set_random_seed(0) sage: graphs.RandomGNP(50,.2, method="Sage").size() 243 sage: graphs.RandomGNP(50,.2, method="networkx").size() 258 """ if n < 0: raise ValueError("The number of nodes must be positive or null.") if 0.0 > p or 1.0 < p: raise ValueError("The probability p must be in [0..1].") if seed is None: seed = current_randstate().long_seed() if p == 1: from sage.graphs.generators.basic import CompleteGraph return CompleteGraph(n) if method == 'networkx': import networkx if fast: G = networkx.fast_gnp_random_graph(n, p, seed=seed) else: G = networkx.gnp_random_graph(n, p, seed=seed) return graph.Graph(G) elif method in ['Sage', 'sage']: # We use the Sage generator from sage.graphs.graph_generators_pyx import RandomGNP as sageGNP return sageGNP(n, p) else: raise ValueError("'method' must be equal to 'networkx' or to 'Sage'.")
def RandomGNP(n, p, seed=None, fast=True, method='Sage'): r""" Returns a random graph on `n` nodes. Each edge is inserted independently with probability `p`. INPUTS: - ``n`` -- number of nodes of the graph - ``p`` -- probability of an edge - ``seed`` -- integer seed for random number generator (default=None). - ``fast`` -- boolean set to True (default) to use the algorithm with time complexity in `O(n+m)` proposed in [BatBra2005]_. It is designed for generating large sparse graphs. It is faster than other methods for *LARGE* instances (try it to know whether it is useful for you). - ``method`` -- By default (```method='Sage'``), this function uses the method implemented in ```sage.graphs.graph_generators_pyx.pyx``. When ``method='networkx'``, this function calls the NetworkX function ``fast_gnp_random_graph``, unless ``fast=False``, then ``gnp_random_graph``. Try them to know which method is the best for you. The ``fast`` parameter is not taken into account by the 'Sage' method so far. REFERENCES: .. [ErdRen1959] P. Erdos and A. Renyi. On Random Graphs, Publ. Math. 6, 290 (1959). .. [Gilbert1959] E. N. Gilbert. Random Graphs, Ann. Math. Stat., 30, 1141 (1959). .. [BatBra2005] V. Batagelj and U. Brandes. Efficient generation of large random networks. Phys. Rev. E, 71, 036113, 2005. PLOTTING: When plotting, this graph will use the default spring-layout algorithm, unless a position dictionary is specified. EXAMPLES: We show the edge list of a random graph on 6 nodes with probability `p = .4`:: sage: set_random_seed(0) sage: graphs.RandomGNP(6, .4).edges(labels=False) [(0, 1), (0, 5), (1, 2), (2, 4), (3, 4), (3, 5), (4, 5)] We plot a random graph on 12 nodes with probability `p = .71`:: sage: gnp = graphs.RandomGNP(12,.71) sage: gnp.show() # long time We view many random graphs using a graphics array:: sage: g = [] sage: j = [] sage: for i in range(9): ....: k = graphs.RandomGNP(i+3,.43) ....: g.append(k) sage: for i in range(3): ....: n = [] ....: for m in range(3): ....: n.append(g[3*i + m].plot(vertex_size=50, vertex_labels=False)) ....: j.append(n) sage: G = sage.plot.graphics.GraphicsArray(j) sage: G.show() # long time sage: graphs.RandomGNP(4,1) Complete graph: Graph on 4 vertices TESTS:: sage: graphs.RandomGNP(50,.2,method=50) Traceback (most recent call last): ... ValueError: 'method' must be equal to 'networkx' or to 'Sage'. sage: set_random_seed(0) sage: graphs.RandomGNP(50,.2, method="Sage").size() 243 sage: graphs.RandomGNP(50,.2, method="networkx").size() 258 """ if n < 0: raise ValueError("The number of nodes must be positive or null.") if 0.0 > p or 1.0 < p: raise ValueError("The probability p must be in [0..1].") if seed is None: seed = current_randstate().long_seed() if p == 1: from sage.graphs.generators.basic import CompleteGraph return CompleteGraph(n) if method == 'networkx': import networkx if fast: G = networkx.fast_gnp_random_graph(n, p, seed=seed) else: G = networkx.gnp_random_graph(n, p, seed=seed) return Graph(G) elif method in ['Sage', 'sage']: # We use the Sage generator from sage.graphs.graph_generators_pyx import RandomGNP as sageGNP return sageGNP(n, p) else: raise ValueError("'method' must be equal to 'networkx' or to 'Sage'.")