ó
|£*^c           @   sõ   d  Z  d d l Z d d l Z d d l Z d d l m Z d d l Z d j d d d g ƒ Z d d	 d
 d d d d d g Z	 d d „ Z d d d „ Z d d „ Z d d „ Z d d „ Z d d d „ Z d e d „ Z d e d „ Z d „  Z d S(   s1   
Generators and functions for bipartite graphs.

iÿÿÿÿN(   t   reduces   
s   Aric Hagberg (hagberg@lanl.gov)s   Pieter Swart (swart@lanl.gov)s   Dan Schult(dschult@colgate.edu)t   configuration_modelt   havel_hakimi_grapht   reverse_havel_hakimi_grapht   alternating_havel_hakimi_grapht   preferential_attachment_grapht   random_grapht   gnmk_random_grapht   complete_bipartite_graphc            sÒ   | d k r t j ƒ  } n. | j ƒ  r9 t j d ƒ ‚ n  | } | j ƒ  t t |  ƒ ƒ } t t |  |  | ƒ ƒ ‰  | j | d d ƒ| j ˆ  d d ƒ| j	 ‡  f d †  | Dƒ ƒ d |  | f | j
 d <| S(	   s“  Return the complete bipartite graph `K_{n_1,n_2}`.

    Composed of two partitions with `n_1` nodes in the first
    and `n_2` nodes in the second. Each node in the first is
    connected to each node in the second.

    Parameters
    ----------
    n1 : integer
       Number of nodes for node set A.
    n2 : integer
       Number of nodes for node set B.
    create_using : NetworkX graph instance, optional
       Return graph of this type.

    Notes
    -----
    Node labels are the integers 0 to `n_1 + n_2 - 1`.

    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.
    s   Directed Graph not supportedt	   bipartitei    i   c         3   s(   |  ] } ˆ  D] } | | f Vq q d  S(   N(    (   t   .0t   ut   v(   t   bottom(    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pys	   <genexpr>B   s    s   complete_bipartite_graph(%d,%d)t   nameN(   t   Nonet   nxt   Grapht   is_directedt   NetworkXErrort   cleart   sett   ranget   add_nodes_fromt   add_edges_fromt   graph(   t   n1t   n2t   create_usingt   Gt   top(    (   R   s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR      s    
c         C   s  | d k r t j ƒ  } n | j ƒ  r9 t j d ƒ ‚ n  t j d | ƒ } | d k	 rg t j | ƒ n  t |  ƒ } t | ƒ } t	 |  ƒ } t	 | ƒ } | | k s¿ t j d | | f ƒ ‚ n  t
 | | | ƒ } t |  ƒ d k rç | Sg  }	 |	 j g  t d | ƒ D] }
 |
 g |  |
 ^ qƒ g  } g  |	 D] } | D] } | ^ q8q.} g  }	 |	 j g  t | | | ƒ D] }
 |
 g | |
 | ^ qmƒ g  } g  |	 D] } | D] } | ^ q¦qœ} t j | ƒ t j | ƒ | j g  t | ƒ D] } | | | | g ^ qèƒ d | _ | S(   s)  Return a random bipartite graph from two given degree sequences.

    Parameters
    ----------
    aseq : list
       Degree sequence for node set A.
    bseq : list
       Degree sequence for node set B.
    create_using : NetworkX graph instance, optional
       Return graph of this type.
    seed : integer, optional
       Seed for random number generator. 

    Nodes from the set A are connected to nodes in the set B by
    choosing randomly from the possible free stubs, one in A and
    one in B.

    Notes
    -----
    The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
    If no graph type is specified use MultiGraph with parallel edges.
    If you want a graph with no parallel edges use create_using=Graph()
    but then the resulting degree sequences might not be exact.

    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.

    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.
    s   Directed Graph not supportedi    s4   invalid degree sequences, sum(aseq)!=sum(bseq),%s,%st   bipartite_configuration_modelN(   R   t   networkxt
   MultiGraphR   R   t   empty_grapht   randomt   seedt   lent   sumt   _add_nodes_with_bipartite_labelt   maxt   extendR   t   shuffleR   R   (   t   aseqt   bseqR   R$   R   t   lenat   lenbt   sumat   sumbt   stubsR   t   astubst   subseqt   xt   bstubst   i(    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   G   s@     4&<&4	c         C   sÛ  | d k r t j ƒ  } n | j ƒ  r9 t j d ƒ ‚ n  t j d | ƒ } t |  ƒ } t | ƒ } t |  ƒ } t | ƒ } | | k s£ t j d | | f ƒ ‚ n  t | | | ƒ } t	 |  ƒ d k rË | Sg  t
 d | ƒ D] } |  | | g ^ qÛ }	 g  t
 | | | ƒ D] } | | | | g ^ q}
 |	 j ƒ  x– |	 rÍ|	 j ƒ  \ } } | d k r`Pn  |
 j ƒ  x] |
 | D]P } | d } | j | | ƒ | d c d 8<| d d k rv|
 j | ƒ qvqvWq8Wd | _ | S(   s2  Return a bipartite graph from two given degree sequences using a 
    Havel-Hakimi style construction.

    Nodes from the set A are connected to nodes in the set B by
    connecting the highest degree nodes in set A to the highest degree
    nodes in set B until all stubs are connected.

    Parameters
    ----------
    aseq : list
       Degree sequence for node set A.
    bseq : list
       Degree sequence for node set B.
    create_using : NetworkX graph instance, optional
       Return graph of this type.

    Notes
    -----
    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.

    The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
    If no graph type is specified use MultiGraph with parallel edges.
    If you want a graph with no parallel edges use create_using=Graph()
    but then the resulting degree sequences might not be exact.

    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.
    s   Directed Graph not supportedi    s4   invalid degree sequences, sum(aseq)!=sum(bseq),%s,%si   t   bipartite_havel_hakimi_graphN(   R   R    R!   R   R   R"   R%   R&   R'   R(   R   t   sortt   popt   add_edget   removeR   (   R+   R,   R   R   t   naseqt   nbseqR/   R0   R   R2   R5   t   degreeR   t   target(    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   –   sB     ,4
	 

	c         C   sÝ  | d k r t j ƒ  } n | j ƒ  r9 t j d ƒ ‚ n  t j d | ƒ } t |  ƒ } t | ƒ } t |  ƒ } t | ƒ } | | k s£ t j d | | f ƒ ‚ n  t | | | ƒ } t	 |  ƒ d k rË | Sg  t
 d | ƒ D] } |  | | g ^ qÛ }	 g  t
 | | | ƒ D] } | | | | g ^ q}
 |	 j ƒ  |
 j ƒ  xŽ |	 rÏ|	 j ƒ  \ } } | d k rjPn  x_ |
 d | !D]P } | d } | j | | ƒ | d c d 8<| d d k rx|
 j | ƒ qxqxWqBWd | _ | S(   s-  Return a bipartite graph from two given degree sequences using a
    Havel-Hakimi style construction.

    Nodes from set A are connected to nodes in the set B by connecting
    the highest degree nodes in set A to the lowest degree nodes in
    set B until all stubs are connected.

    Parameters
    ----------
    aseq : list
       Degree sequence for node set A.
    bseq : list
       Degree sequence for node set B.
    create_using : NetworkX graph instance, optional
       Return graph of this type.


    Notes
    -----
    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.

    The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
    If no graph type is specified use MultiGraph with parallel edges.
    If you want a graph with no parallel edges use create_using=Graph()
    but then the resulting degree sequences might not be exact.

    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.
    s   Directed Graph not supportedi    s4   invalid degree sequences, sum(aseq)!=sum(bseq),%s,%si   t$   bipartite_reverse_havel_hakimi_graphN(   R   R    R!   R   R   R"   R%   R&   R'   R(   R   R8   R9   R:   R;   R   (   R+   R,   R   R   R-   R.   R/   R0   R   R2   R5   R>   R   R?   (    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   ß   sB     ,4

	 
	c         C   sa  | d k r t j ƒ  } n | j ƒ  r9 t j d ƒ ‚ n  t j d | ƒ } t |  ƒ } t | ƒ } t |  ƒ } t | ƒ } | | k s£ t j d | | f ƒ ‚ n  t | | | ƒ } t	 |  ƒ d k rË | Sg  t
 d | ƒ D] } |  | | g ^ qÛ }	 g  t
 | | | ƒ D] } | | | | g ^ q}
 x&|	 rS|	 j ƒ  |	 j ƒ  \ } } | d k r`Pn  |
 j ƒ  |
 d | d !} |
 | | d } g  t | | ƒ D] } | D] } | ^ q¨qž} t | ƒ t | ƒ t | ƒ k  rõ| j | j ƒ  ƒ n  xX | D]P } | d } | j | | ƒ | d c d 8<| d d k rü|
 j | ƒ qüqüWq.Wd | _ | S(   sa  Return a bipartite graph from two given degree sequences using 
    an alternating Havel-Hakimi style construction.

    Nodes from the set A are connected to nodes in the set B by
    connecting the highest degree nodes in set A to alternatively the
    highest and the lowest degree nodes in set B until all stubs are
    connected.

    Parameters
    ----------
    aseq : list
       Degree sequence for node set A.
    bseq : list
       Degree sequence for node set B.
    create_using : NetworkX graph instance, optional
       Return graph of this type.


    Notes
    -----
    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.

    The sum of the two sequences must be equal: sum(aseq)=sum(bseq)
    If no graph type is specified use MultiGraph with parallel edges.
    If you want a graph with no parallel edges use create_using=Graph()
    but then the resulting degree sequences might not be exact.

    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.
    s   Directed Graph not supportedi    s4   invalid degree sequences, sum(aseq)!=sum(bseq),%s,%si   i   t(   bipartite_alternating_havel_hakimi_graphN(   R   R    R!   R   R   R"   R%   R&   R'   R(   R   R8   R9   t   zipt   appendR:   R;   R   (   R+   R,   R   R   R<   R=   R/   R0   R   R2   R5   R>   R   t   smallt   larget   zR4   R1   R?   (    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   *  sL      ,4	
 
/"
	c         C   s  | d k r t j ƒ  } n | j ƒ  r9 t j d ƒ ‚ n  | d k r[ t j d | ƒ ‚ n  t j d | ƒ } | d k	 r‰ t j | ƒ n  t |  ƒ } t	 | | d ƒ } g  t
 d | ƒ D] } | g |  | ^ q· } x| róxÿ | d rÞ| d d } | d j | ƒ t j ƒ  | k  s-| j ƒ  | k r_| j ƒ  }	 | j |	 d d ƒ| j | |	 ƒ qà g  t
 | | j ƒ  ƒ D] }
 |
 g | j |
 ƒ ^ qu} t d „  | ƒ } t j | ƒ }	 | j |	 d d ƒ| j | |	 ƒ qà W| j | d ƒ q× Wd | _ | S(	   s   Create a bipartite graph with a preferential attachment model from 
    a given single degree sequence.

    Parameters
    ----------
    aseq : list
       Degree sequence for node set A.
    p :  float
       Probability that a new bottom node is added.
    create_using : NetworkX graph instance, optional
       Return graph of this type.
    seed : integer, optional
       Seed for random number generator. 

    References
    ----------
    .. [1] Jean-Loup Guillaume and Matthieu Latapy,
       Bipartite structure of all complex networks,
       Inf. Process. Lett. 90, 2004, pg. 215-221
       http://dx.doi.org/10.1016/j.ipl.2004.03.007

    Notes
    -----

    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.
    s   Directed Graph not supportedi   s   probability %s > 1i    R	   c         S   s   |  | S(   N(    (   R4   t   y(    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyt   <lambda>¯  s    t'   bipartite_preferential_attachment_modelN(   R   R    R!   R   R   R"   R#   R$   R%   R'   R   R;   t   number_of_nodest   add_nodeR:   R>   R    t   choiceR   (   R+   t   pR   R$   R   R<   R   t   vvt   sourceR?   t   bt   bbt   bbstubs(    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   w  s:    -	$8	c   
      C   s   t  j ƒ  } t | |  | ƒ } | r6 t  j | ƒ } n  d |  | | f | _ | d k	 rh t j | ƒ n  | d k rx | S| d k r” t  j |  | ƒ St	 j
 d | ƒ } d } d } x— | |  k  rLt	 j
 d t j ƒ  ƒ }	 | d t |	 | ƒ } x0 | | k r%| |  k  r%| | } | d } qö W| |  k  r¶ | j | |  | ƒ q¶ q¶ W| rüd } d } xš | |  k  røt	 j
 d t j ƒ  ƒ }	 | d t |	 | ƒ } x0 | | k rÑ| |  k  rÑ| | } | d } q¢W| |  k  rb| j |  | | ƒ qbqbWn  | S(   sÙ  Return a bipartite random graph.

    This is a bipartite version of the binomial (ErdÅ‘s-RÃ©nyi) graph.

    Parameters
    ----------
    n : int
        The number of nodes in the first bipartite set.
    m : int
        The number of nodes in the second bipartite set.
    p : float
        Probability for edge creation.
    seed : int, optional
        Seed for random number generator (default=None). 
    directed : bool, optional (default=False)
        If True return a directed graph 
      
    Notes
    -----
    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.

    The bipartite random graph algorithm chooses each of the n*m (undirected) 
    or 2*nm (directed) possible edges with probability p.

    This algorithm is O(n+m) where m is the expected number of edges.
    
    The nodes are assigned the attribute 'bipartite' with the value 0 or 1
    to indicate which bipartite set the node belongs to.

    See Also
    --------
    gnp_random_graph, configuration_model

    References
    ----------
    .. [1] Vladimir Batagelj and Ulrik Brandes, 
       "Efficient generation of large random networks",
       Phys. Rev. E, 71, 036113, 2005.
    s   fast_gnp_random_graph(%s,%s,%s)i    i   g      ð?iÿÿÿÿN(   R   R   R'   t   DiGraphR   R   R#   R$   R   t   matht   logt   intR:   (
   t   nt   mRM   R$   t   directedR   t   lpR   t   wt   lr(    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR   º  sD    )

c         C   sr  t  j ƒ  } t | |  | ƒ } | r6 t j | ƒ } n  d |  | | f | _ | d k	 rh t j | ƒ n  |  d k s€ | d k r„ | S|  | } | | k r° t  j	 |  | d | ƒSg  | j
 d t ƒ D]" \ }  } | d d k rÃ |  ^ qÃ } t t | ƒ t | ƒ ƒ }	 d }
 x^ |
 | k  rmt j | ƒ } t j |	 ƒ } | | | k rPqq| j | | ƒ |
 d 7}
 qW| S(   só  Return a random bipartite graph G_{n,m,k}.

    Produces a bipartite graph chosen randomly out of the set of all graphs
    with n top nodes, m bottom nodes, and k edges.

    Parameters
    ----------
    n : int
        The number of nodes in the first bipartite set.
    m : int
        The number of nodes in the second bipartite set.
    k : int
        The number of edges
    seed : int, optional
        Seed for random number generator (default=None). 
    directed : bool, optional (default=False)
        If True return a directed graph 
        
    Examples
    --------
    from networkx.algorithms import bipartite
    G = bipartite.gnmk_random_graph(10,20,50)

    See Also
    --------
    gnm_random_graph

    Notes
    -----
    This function is not imported in the main namespace.
    To use it you have to explicitly import the bipartite package.

    If k > m * n then a complete bipartite graph is returned.

    This graph is a bipartite version of the `G_{nm}` random graph model.
    s$   bipartite_gnm_random_graph(%s,%s,%s)i   R   t   dataR	   i    N(   R    R   R'   R   RS   R   R   R#   R$   R   t   nodest   Truet   listR   RL   R:   (   RW   RX   t   kR$   RY   R   t	   max_edgest   dR   R   t
   edge_countR   R   (    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR     s.    %
;c         C   s†   |  j  t d | | ƒ ƒ t t t d | ƒ d g | ƒ ƒ } | j t t t | | | ƒ d g | ƒ ƒ ƒ t j |  d | ƒ |  S(   Ni    i   R	   (   R   R   t   dictRB   t   updateR   t   set_node_attributes(   R   R-   R.   RP   (    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyR'   N  s
    %0(   t   __doc__RT   R#   R    t	   functoolsR    R   t   joint
   __author__t   __all__R   R   R   R   R   R   R   t   FalseR   R   R'   (    (    (    s|   /home/gitlab-runner/builds/8480fa44/0/bergerc/fluidmanager-web/art-framework/bin/networkx/algorithms/bipartite/generators.pyt   <module>   s2   		(OIKMCT@