Ticket #6828: random_bipartite.patch

File random_bipartite.patch, 2.5 KB (added by ncohen, 10 years ago)
• sage/graphs/graph_generators.py

`diff -r 684eea91ff22 -r c0dc40a14607 sage/graphs/graph_generators.py`
 a Random Graphs: - RandomGNP - RandomBarabasiAlbert - RandomBipartite - RandomGNM - RandomNewmanWattsStrogatz - RandomHolmeKim Random Graphs: - RandomGNP - RandomBarabasiAlbert - RandomBipartite - RandomGNM - RandomNewmanWattsStrogatz - RandomHolmeKim import networkx return graph.Graph(networkx.barabasi_albert_graph(n,m,seed)) def RandomBipartite(self, n1,n2, p): r""" Returns a bipartite graph with `n1+n2` vertices such that each edge from `[n1]` to `[n2]` exists with probability `p` INPUT: - ``n1,n2`` : Cardinalities of the two sets - ``p``   : Probability for an edge to exist EXAMPLE: sage: g=graphs.RandomBipartite(5,2,0.5) """ from numpy.random import uniform import sage.graphs.bipartite_graph as bipartite_graph pos_dict = {} c1 = 1 # scaling factor for top row c2 = 1 # scaling factor for bottom row c3 = 0 # pad to center if top row has 1 node c4 = 0 # pad to center if bottom row has 1 node if n1 > n2: if n2 == 1: c4 = (n1-1)/2 else: c2 = ((n1-1)/(n2-1)) elif n2 > n1: if n1 == 1: c3 = (n2-1)/2 else: c1 = ((n2-1)/(n1-1)) for i in range(n1): x = c1*i + c3 y = 1 pos_dict[i] = [x,y] for i in range(n1+n2)[n1:]: x = c2*(i-n1) + c4 y = 0 pos_dict[i] = [x,y] g=graph.Graph() g.add_vertices(range(n1+n2)) [g.add_edge(v,w+n1) for v in range(n1) for w in range(n2) if uniform()<=p] return bipartite_graph.BipartiteGraph(g, pos=pos_dict, name="Random bipartite graph") def RandomGNM(self, n, m, dense=False, seed=None): """ Returns a graph randomly picked out of all graphs on n vertices