Ticket #6828: trac_6828.patch

File trac_6828.patch, 3.0 KB (added by ncohen, 4 years ago)
  • sage/graphs/graph_generators.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1253978666 -7200
    # Node ID 74e786d98284ba120522437747af08a1803b61ab
    # Parent  e92ac6a4c5205df0810f1d950a5b6d9520e6d2f0
    RandomBipartite function in graph_generators
    
    diff -r e92ac6a4c520 -r 74e786d98284 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 any 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            sage: g.vertices() 
     2641            [(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (1, 0), (1, 1)] 
     2642             
     2643        TESTS:: 
     2644 
     2645            sage: g=graphs.RandomBipartite(5,-3,0.5) 
     2646            Traceback (most recent call last): 
     2647            ... 
     2648            ValueError: n1 and n2 should be integers strictly greater than 0 
     2649            sage: g=graphs.RandomBipartite(5,3,1.5) 
     2650            Traceback (most recent call last): 
     2651            ... 
     2652            ValueError: Parameter p is a probability, and so should be a real value between 0 and 1 
     2653 
     2654        """ 
     2655        if not (p>=0 and p<=1): 
     2656            raise ValueError, "Parameter p is a probability, and so should be a real value between 0 and 1" 
     2657        if not (n1>0 and n2>0): 
     2658            raise ValueError, "n1 and n2 should be integers strictly greater than 0" 
     2659 
     2660        from numpy.random import uniform 
     2661        import sage.graphs.bipartite_graph as bipartite_graph 
     2662        from sage.graphs.graph import Graph 
     2663 
     2664        g=Graph() 
     2665 
     2666        S1=[(0,i) for i in range(n1)] 
     2667        S2=[(1,i) for i in range(n2)] 
     2668        g.add_vertices(S1) 
     2669        g.add_vertices(S2) 
     2670        [g.add_edge((0,v),(1,w)) for v in range(n1) for w in range(n2) if uniform()<=p] 
     2671 
     2672        return bipartite_graph.BipartiteGraph(g,[S1,S2],name="Random bipartite graph of size "+str(n1) +"+"+str(n2)+" with edge probability "+str(p))             
     2673 
    26232674    def RandomGNM(self, n, m, dense=False, seed=None): 
    26242675        """ 
    26252676        Returns a graph randomly picked out of all graphs on n vertices