| | 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 | |