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 b  
    8989    Random Graphs:
    9090        - RandomGNP
    9191        - RandomBarabasiAlbert
     92        - RandomBipartite
    9293        - RandomGNM
    9394        - RandomNewmanWattsStrogatz
    9495        - RandomHolmeKim
     
    207208                Random Graphs:
    208209                    - RandomGNP
    209210                    - RandomBarabasiAlbert
     211                    - RandomBipartite
    210212                    - RandomGNM
    211213                    - RandomNewmanWattsStrogatz
    212214                    - RandomHolmeKim
     
    26202622        import networkx
    26212623        return graph.Graph(networkx.barabasi_albert_graph(n,m,seed))
    26222624
     2625    def RandomBipartite(self, n1,n2, p):
     2626        r"""
     2627        Returns a bipartite graph with `n1+n2` vertices
     2628        such that each edge from `[n1]` to `[n2]` exists
     2629        with probability `p`
     2630
     2631        INPUT:
     2632
     2633            - ``n1,n2`` : Cardinalities of the two sets
     2634            - ``p``   : Probability for an edge to exist
     2635           
     2636           
     2637        EXAMPLE:
     2638       
     2639            sage: g=graphs.RandomBipartite(5,2,0.5)
     2640
     2641        """
     2642        from numpy.random import uniform
     2643        import sage.graphs.bipartite_graph as bipartite_graph
     2644
     2645        pos_dict = {}
     2646        c1 = 1 # scaling factor for top row
     2647        c2 = 1 # scaling factor for bottom row
     2648        c3 = 0 # pad to center if top row has 1 node
     2649        c4 = 0 # pad to center if bottom row has 1 node
     2650        if n1 > n2:
     2651            if n2 == 1:
     2652                c4 = (n1-1)/2
     2653            else:
     2654                c2 = ((n1-1)/(n2-1))
     2655        elif n2 > n1:
     2656            if n1 == 1:
     2657                c3 = (n2-1)/2
     2658            else:
     2659                c1 = ((n2-1)/(n1-1))
     2660        for i in range(n1):
     2661            x = c1*i + c3
     2662            y = 1
     2663            pos_dict[i] = [x,y]
     2664        for i in range(n1+n2)[n1:]:
     2665            x = c2*(i-n1) + c4
     2666            y = 0
     2667            pos_dict[i] = [x,y]
     2668
     2669        g=graph.Graph()
     2670        g.add_vertices(range(n1+n2))
     2671
     2672        [g.add_edge(v,w+n1) for v in range(n1) for w in range(n2) if uniform()<=p]
     2673        return bipartite_graph.BipartiteGraph(g, pos=pos_dict, name="Random bipartite graph")
     2674           
     2675
    26232676    def RandomGNM(self, n, m, dense=False, seed=None):
    26242677        """
    26252678        Returns a graph randomly picked out of all graphs on n vertices