
    9i                          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SS.S jj5       r	S 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 {'dense', '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       d/var/www/html/land-doc-ocr/venv/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_graph)r*   r+   c                  ^
 [         R                  " SU5      nU R                  u  m
n[        XU5      u  p4U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  [        [        R                  " [        [        T
5      U5      [        [        T
T
U-   5      U5      5      5      n	[!        U	5      (       a  [         R"                  " XYSS	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)).

row_order : list, optional (default: range(number of rows in `A`))
    A list of the nodes represented by the rows of the matrix `A`. Will
    be represented in the graph as nodes with the `bipartite` attribute set
    to 0. Must be the same length as the number of rows in `A`.

column_order : list, optional (default: range(number of columns in `A`))
    A list of the nodes represented by the columns of the matrix `A`. Will
    be represented in the graph as nodes with the `bipartite` attribute set
    to 1. Must be the same length as the number of columns in `A`.

Returns
-------
G : NetworkX graph
    A bipartite graph with edges from the biadjacency matrix `A`, and
    nodes from `row_order` and `column_order`.

Raises
------
ValueError
    If `row_order` or `column_order` are provided and are not the same
    length as the number of rows or columns in `A`, respectively.

Notes
-----
The nodes are labeled with the attribute `bipartite` set to an integer
0 or 1 representing membership in the `top` set (`bipartite=0`) or `bottom`
set (`bipartite=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   F)copy)r   empty_graphr   (_validate_initialize_bipartite_nodelistsadd_nodes_fromrA   r   r   kindis_multigraphr!   chainfrom_iterableadd_weighted_edges_fromr   r    r   relabel_nodes)r3   create_usingedge_attributer*   r+   r)   mtriplesrI   mappingr;   s             @r   r   r   r   s"   F 	q,'A77DAqF	lI
 U1X+U1a!e_2 K.Fq.IJG 	ww||z!aoo&7&7--OwOOg= E!Hi0#eAq1uo|2TUG 7||
%0H    c                     U R                   u  p4Ub  [        U5      U:w  a  [        S5      eO/ nUb  [        U5      U:w  a  [        S5      e X4$ / nX4$ )Nz<Length of row_order does not match number of rows in A ({n})zBLength of column_order does not match number of columns in A ({m}))r   r   r(   )r3   r*   r+   r;   rO   s        r   rE   rE      s~    77DAy>QN  
 	|!T  " "" ""rR   )NNr   csr)Nr   )__doc__r!   networkxr   networkx.convert_matrixr   __all___dispatchabler   r   rE   r:   rR   r   <module>rZ      s      <!:
; X&IN_R '_RD T2 _
 _ 3_D#rR   