U
    ?hM                     @   s   d Z ddlZddlZddlmZ ddlZddlmZm	Z	 ddddd	d
ddgZ
eddgdddZe	ddddZdddZdddZdddZe	dddd	Ze	dd dd
Ze	dd!ddZdd ZdS )"z0
Generators and functions for bipartite graphs.
    N)reduce)nodes_or_numberpy_random_stateconfiguration_modelhavel_hakimi_graphreverse_havel_hakimi_graphalternating_havel_hakimi_graphpreferential_attachment_graphrandom_graphgnmk_random_graphcomplete_bipartite_graph   c                    s   t d|}| rt d\}|\} ttjrXt|tjrXfdd D  |j|dd |j dd t|t|t  krt d|	 fdd	|D  d
 d| d|j
d< |S )a   Returns the complete bipartite graph `K_{n_1,n_2}`.

    The graph is composed of two partitions with nodes 0 to (n1 - 1)
    in the first and nodes n1 to (n1 + n2 - 1) in the second.
    Each node in the first is connected to each node in the second.

    Parameters
    ----------
    n1, n2 : integer or iterable container of nodes
        If integers, nodes are from `range(n1)` and `range(n1, n1 + n2)`.
        If a container, the elements are the nodes.
    create_using : NetworkX graph instance, (default: nx.Graph)
       Return graph of this type.

    Notes
    -----
    Nodes are the integers 0 to `n1 + n2 - 1` unless either n1 or n2 are
    containers of nodes. If only one of n1 or n2 are integers, that
    integer is replaced by `range` of that integer.

    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 use nx.bipartite.complete_bipartite_graph
    r   Directed Graph not supportedc                    s   g | ]} | qS  r   .0i)n1r   Z/var/www/html/venv/lib/python3.8/site-packages/networkx/algorithms/bipartite/generators.py
<listcomp>:   s     z,complete_bipartite_graph.<locals>.<listcomp>	bipartiter   z,Inputs n1 and n2 must contain distinct nodesc                 3   s    | ]} D ]}||fV  q
qd S Nr   )r   uv)bottomr   r   	<genexpr>?   s       z+complete_bipartite_graph.<locals>.<genexpr>zcomplete_bipartite_graph(z, )name)nxempty_graphis_directedNetworkXError
isinstancenumbersIntegraladd_nodes_fromlenadd_edges_fromgraph)r   Zn2create_usingGtopr   )r   r   r   r      s    

   c           	         s  t jd|t jd}| r$t dt t}t }t}||ksbt d| d| t||}t dkst dkr|S  fddt	dD }dd |D fd	dt	| D }d
d |D |
 |
 |fddt	|D  d|_|S )a  Returns 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, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    The graph is composed of two partitions. Set A has nodes 0 to
    (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
    Nodes from set A are connected to nodes in 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 use nx.bipartite.configuration_model
    r   defaultr   /invalid degree sequences, sum(aseq)!=sum(bseq),,c                    s   g | ]}|g |  qS r   r   r   r   aseqr   r   r   {   s     z'configuration_model.<locals>.<listcomp>c                 S   s   g | ]}|D ]}|qqS r   r   r   Zsubseqxr   r   r   r   |   s       c                    s   g | ]}|g |   qS r   r   r2   bseqlenar   r   r   ~   s     c                 S   s   g | ]}|D ]}|qqS r   r   r5   r   r   r   r      s       c                 3   s   | ]} | | gV  qd S r   r   r   )astubsbstubsr   r   r      s     z&configuration_model.<locals>.<genexpr>Zbipartite_configuration_model)r   r    
MultiGraphr!   r"   r'   sum_add_nodes_with_bipartite_labelmaxrangeshuffler(   r   )	r4   r8   r*   seedr+   lenbsumasumbstubsr   )r4   r:   r8   r;   r9   r   r   D   s.    "


c                    sH  t jd|t jd}| r$t dt t}t }t}||ksbt d| d| t||}t dkst dkr|S  fddt	dD }fddt	| D }|
  |r>| \}	}
|	dkrq>|
  ||	 d	 D ]>}|d
 }||
| |d  d
8  < |d dkr|| qqd|_|S )a  Returns a bipartite graph from two given degree sequences using a
    Havel-Hakimi style construction.

    The graph is composed of two partitions. Set A has nodes 0 to
    (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
    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
    -----
    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 use nx.bipartite.havel_hakimi_graph
    r   r.   r   r0   r1   c                    s   g | ]} | |gqS r   r   r2   r3   r   r   r      s     z&havel_hakimi_graph.<locals>.<listcomp>c                    s   g | ]} |  |gqS r   r   r2   r8   naseqr   r   r      s     Nr   Zbipartite_havel_hakimi_graphr   r    r<   r!   r"   r'   r=   r>   r?   r@   sortpopadd_edgeremover   )r4   r8   r*   r+   nbseqrD   rE   r:   r;   degreer   targetr   r   r4   r8   rH   r   r      s<     
c                    sF  t jd|t jd}| r$t dt t}t }t}||ksbt d| d| t||}t dkst dkr|S  fddt	dD }fddt	| D }|
  |
  |r<| \}	}
|	dkrq<|d|	 D ]>}|d	 }||
| |d  d	8  < |d dkr|| qqd
|_|S )a  Returns a bipartite graph from two given degree sequences using a
    Havel-Hakimi style construction.

    The graph is composed of two partitions. Set A has nodes 0 to
    (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
    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
    -----
    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 use nx.bipartite.reverse_havel_hakimi_graph
    r   r.   r   r0   r1   c                    s   g | ]} | |gqS r   r   r2   r3   r   r   r   
  s     z.reverse_havel_hakimi_graph.<locals>.<listcomp>c                    s   g | ]} |  |gqS r   r   r2   r7   r   r   r     s     r   Z$bipartite_reverse_havel_hakimi_graphrI   )r4   r8   r*   r+   rC   rD   rE   r:   r;   rO   r   rP   r   r   )r4   r8   r9   r   r      s<     
c                    s  t jd|t jd}| r$t dt t}t }t}||ksbt d| d| t||}t dkst dkr|S  fddt	dD }fddt	| D }|r|
  | \}	}
|	dkrq|
  |d|	d	  }||	 |	d	  d
 }dd t||D }t|t|t| k rP||  |D ]B}|d }||
| |d  d8  < |d dkrT|| qTqd|_|S )a  Returns a bipartite graph from two given degree sequences using
    an alternating Havel-Hakimi style construction.

    The graph is composed of two partitions. Set A has nodes 0 to
    (len(aseq) - 1) and set B has nodes len(aseq) to (len(bseq) - 1).
    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
    -----
    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 use nx.bipartite.alternating_havel_hakimi_graph
    r   r.   r   r0   r1   c                    s   g | ]} | |gqS r   r   r2   r3   r   r   r   S  s     z2alternating_havel_hakimi_graph.<locals>.<listcomp>c                    s   g | ]} |  |gqS r   r   r2   rG   r   r   r   T  s        Nc                 S   s   g | ]}|D ]}|qqS r   r   )r   zr6   r   r   r   r   ]  s       r   Z(bipartite_alternating_havel_hakimi_graph)r   r    r<   r!   r"   r'   r=   r>   r?   r@   rJ   rK   zipappendrL   rM   r   )r4   r8   r*   r+   rN   rD   rE   r:   r;   rO   r   ZsmallZlargerF   rP   r   r   rQ   r   r     sF    !
c           
         s:  t jd|t jd   r$t d|dkr>t d| dt}t |d fddtd|D }|r0|d r |d d }|d | |	 |k st |krt } j
|dd	  || qp fd
dt|t D }tdd |}	||	} j
|dd	  || qp||d  qjd _ S )a^  Create a bipartite graph with a preferential attachment model from
    a given single degree sequence.

    The graph is composed of two partitions. Set A has nodes 0 to
    (len(aseq) - 1) and set B has nodes starting with node len(aseq).
    The number of nodes in set B is random.

    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, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    References
    ----------
    .. [1] Guillaume, J.L. and Latapy, M.,
       Bipartite graphs as models of complex networks.
       Physica A: Statistical Mechanics and its Applications,
       2006, 371(2), pp.795-813.
    .. [2] Jean-Loup Guillaume and Matthieu Latapy,
       Bipartite structure of all complex networks,
       Inf. Process. Lett. 90, 2004, pg. 215-221
       https://doi.org/10.1016/j.ipl.2004.03.007

    Notes
    -----
    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 use nx.bipartite.preferential_attachment_graph
    r   r.   r   r   zprobability z > 1c                    s   g | ]}|g |  qS r   r   r2   r3   r   r   r     s     z1preferential_attachment_graph.<locals>.<listcomp>r   c                    s   g | ]}|g  | qS r   )rO   )r   b)r+   r   r   r     s     c                 S   s   | | S r   r   )r6   yr   r   r   <lambda>      z/preferential_attachment_graph.<locals>.<lambda>Z'bipartite_preferential_attachment_model)r   r    r<   r!   r"   r'   r>   r@   rM   randomadd_noderL   r   choicer   )
r4   pr*   rB   rH   ZvvsourcerP   ZbbZbbstubsr   )r+   r4   r   r	   k  s0    (


Fc           
      C   s`  t  }t|| |}|r"t |}d|  d| d| d|_|dkrH|S |dkr\t | |S td| }d}d}|| k rtd|  }	|d t	|	|  }||kr|| k r|| }|d }q|| k rr|
|| |  qr|r\d}d}|| k r\td|  }	|d t	|	|  }||krB|| k rB|| }|d }q|| k r|
| | | q|S )uo  Returns a bipartite random graph.

    This is a bipartite version of the binomial (Erdős-Rényi) graph.
    The graph is composed of two partitions. Set A has nodes 0 to
    (n - 1) and set B has nodes n to (n + m - 1).

    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 : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    directed : bool, optional (default=False)
        If True return a directed graph

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

    This function is not imported in the main namespace.
    To use it use nx.bipartite.random_graph

    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.
    zfast_gnp_random_graph(r1   r   r   r   g      ?)r   Graphr>   DiGraphr   r   mathlogrZ   intrL   )
nmr]   rB   directedr+   Zlpr   wlrr   r   r   r
     s@    -


c                 C   s   t  }t|| |}|r"t |}d|  d| d| d|_| dksL|dkrP|S | | }||krpt j| ||dS dd |jdd	D }tt|t| }d
}	|	|k r|	|}
|	|}|||
 krqq|
|
| |	d7 }	q|S )a  Returns 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.
    The graph is composed of two sets of nodes.
    Set A has nodes 0 to (n - 1) and set B has nodes n to (n + m - 1).

    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 : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.
    directed : bool, optional (default=False)
        If True return a directed graph

    Examples
    --------
    from nx.algorithms import bipartite
    G = bipartite.gnmk_random_graph(10,20,50)

    See Also
    --------
    gnm_random_graph

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

    This graph is a bipartite version of the `G_{nm}` random graph model.

    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 use nx.bipartite.gnmk_random_graph
    zbipartite_gnm_random_graph(r1   r   r   )r*   c                 S   s    g | ]\}}|d  dkr|qS )r   r   r   )r   re   dr   r   r   r   ?  s      z%gnmk_random_graph.<locals>.<listcomp>T)datar   )r   r`   r>   ra   r   r   Znodeslistsetr\   rL   )re   rf   krB   rg   r+   Z	max_edgesr,   r   Z
edge_countr   r   r   r   r   r     s*    ,



c                 C   sd   |  td||  tttd|dg| }|ttt||| dg|  t| |d | S )Nr   r   r   )r&   r@   dictrT   updater   Zset_node_attributes)r+   r9   rC   rV   r   r   r   r>   N  s
    $r>   )N)NN)N)N)N)NN)NF)NF)__doc__rb   r$   	functoolsr   Znetworkxr   Znetworkx.utilsr   r   __all__r   r   r   r   r   r	   r
   r   r>   r   r   r   r   <module>   s8   
,F
J
I
MFUE