
    h                         S r SSKrSSKrSSKJr  SS/r\R                  " SS9 SS j5       r\R                  " SS	S
9SS j5       r	g)z@
====================
Biadjacency matrices
====================
    N)_generate_weighted_edgesbiadjacency_matrixfrom_biadjacency_matrixweight)
edge_attrsc                 v  ^^^ SSK n[        U5      nUS:X  a  [        R                  " S5      e[        U5      [        [	        U5      5      :w  a  Sn[        R                  " U5      eUc   [        [	        U 5      [	        U5      -
  5      n[        U5      n	[        U5      [        [	        U5      5      :w  a  Sn[        R                  " U5      e[        [        U[        R                  " 5       5      5      m[        [        U[        R                  " 5       5      5      mU R                  5       S:X  a  / / / pn
O%[        UUU4S jU R                  USS9 5       6 u  pnUR                  R                  XU44Xy4US	9n UR                  U5      $ ! [         a  n[        R                  " S
U 35      UeSnAff = f)a  Returns the biadjacency matrix of the bipartite graph G.

Let `G = (U, V, E)` be a bipartite graph with node sets
`U = u_{1},...,u_{r}` and `V = v_{1},...,v_{s}`. The biadjacency
matrix [1]_ is the `r` x `s` matrix `B` in which `b_{i,j} = 1`
if, and only if, `(u_i, v_j) \in E`. If the parameter `weight` is
not `None` and matches the name of an edge attribute, its value is
used instead of 1.

Parameters
----------
G : graph
   A NetworkX graph

row_order : list of nodes
   The rows of the matrix are ordered according to the list of nodes.

column_order : list, optional
   The columns of the matrix are ordered according to the list of nodes.
   If column_order is None, then the ordering of columns is arbitrary.

dtype : NumPy data-type, optional
    A valid NumPy dtype used to initialize the array. If None, then the
    NumPy default is used.

weight : string or None, optional (default='weight')
   The edge data key used to provide each value in the matrix.
   If None, then each edge has weight 1.

format : str in {'bsr', 'csr', 'csc', 'coo', 'lil', 'dia', 'dok'}
    The type of the matrix to be returned (default 'csr').  For
    some algorithms different implementations of sparse matrices
    can perform better.  See [2]_ for details.

Returns
-------
M : SciPy sparse array
    Biadjacency matrix representation of the bipartite graph G.

Notes
-----
No attempt is made to check that the input graph is bipartite.

For directed bipartite graphs only successors are considered as neighbors.
To obtain an adjacency matrix with ones (or weight values) for both
predecessors and successors you have to generate two biadjacency matrices
where the rows of one of them are the columns of the other, and then add
one to the transpose of the other.

See Also
--------
adjacency_matrix
from_biadjacency_matrix

References
----------
.. [1] https://en.wikipedia.org/wiki/Adjacency_matrix#Adjacency_matrix_of_a_bipartite_graph
.. [2] Scipy Dev. References, "Sparse Matrices",
   https://docs.scipy.org/doc/scipy/reference/sparse.html
r   Nzrow_order is empty listz5Ambiguous ordering: `row_order` contained duplicates.z8Ambiguous ordering: `column_order` contained duplicates.c              3      >#    U  H3  u  pnUT;   d  M  UT;   d  M  TU   TU   UR                  TS 5      4v   M5     g7f   N)get).0uvd	col_index	row_indexr   s       V/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/bipartite/matrix.py	<genexpr>%biadjacency_matrix.<locals>.<genexpr>e   sK      <GA!	> ?&'9n ?1y|QUU61-=><s   >>">T)data)shapedtypezUnknown sparse array format: )scipylennxNetworkXErrorsetlistdictzip	itertoolscountnumber_of_edgesedgessparse	coo_arrayasformat
ValueError)G	row_ordercolumn_orderr   r   formatspnlenmsgmlenrowcolr   Aerrr   r   s       `          @@r   r   r      s   @ y>Dqy899
9~S^,,Es##CFS^34|D
<CL 122Hs##SIOO$567ISy'89:IaR$$ wwytw<
$ 			T:.tl%PARzz&!! R!>vhGHcQRs   >F 
F8F33F8T)graphsreturns_graphc                   ^ [         R                  " SU5      nU R                  u  mnUR                  [	        T5      SS9  UR                  [	        TTU-   5      SS9  U4S j[        U 5       5       nU R                  R                  S;   a>  UR                  5       (       a)  [        R                  R                  nU" S U 5       5      nUR                  XRS9  U$ )a  Creates a new bipartite graph from a biadjacency matrix given as a
SciPy sparse array.

Parameters
----------
A: scipy sparse array
  A biadjacency matrix representation of a graph

create_using: NetworkX graph
   Use specified graph for result.  The default is Graph()

edge_attribute: string
   Name of edge attribute to store matrix numeric value. The data will
   have the same type as the matrix entry (int, float, (real,imag)).

Notes
-----
The nodes are labeled with the attribute `bipartite` set to an integer
0 or 1 representing membership in part 0 or part 1 of the bipartite graph.

If `create_using` is an instance of :class:`networkx.MultiGraph` or
:class:`networkx.MultiDiGraph` and the entries of `A` are of
type :class:`int`, then this function returns a multigraph (of the same
type as `create_using`) with parallel edges. In this case, `edge_attribute`
will be ignored.

See Also
--------
biadjacency_matrix
from_numpy_array

References
----------
[1] https://en.wikipedia.org/wiki/Adjacency_matrix#Adjacency_matrix_of_a_bipartite_graph
r   )	bipartiter   c              3   8   >#    U  H  u  po1TU-   U4v   M     g 7f)N )r   r   r   r   ns       r   r   *from_biadjacency_matrix.<locals>.<genexpr>   s     J.I!1q5!}.Is   )ir   c              3   Z   ^^#    U  H  u  mmnUU4S  j[        U5       5       v   M!     g7f)c              3   .   >#    U  H
  nTTS 4v   M     g7fr
   r:   )r   r   r   r   s     r   r   4from_biadjacency_matrix.<locals>.<genexpr>.<genexpr>   s     5Hq!QHs   N)range)r   wr   r   s     @@r   r   r<      s$     Ow)1a5E!H55ws   '+)r   )r   empty_graphr   add_nodes_fromrA   r   r   kindis_multigraphr!   chainfrom_iterableadd_weighted_edges_from)r3   create_usingedge_attributer)   mtriplesrG   r;   s          @r   r   r   r   s    J 	q,'A77DAqU1X+U1a!e_2 K.Fq.IJG 	ww||z!aoo&7&7--OwOOg=H    )NNr   csr)Nr   )
__doc__r!   networkxr   networkx.convert_matrixr   __all___dispatchabler   r   r:   rN   r   <module>rU      sh      <!:
; X&IN_R '_RD T25 35rN   