
    wg                         d Z ddlZddlZddlmZ ddgZ ej                  d      	 dd       Z ej                  dd	
      dd       Z	y)z@
====================
Biadjacency matrices
====================
    N)_generate_weighted_edgesbiadjacency_matrixfrom_biadjacency_matrixweight)
edge_attrsc                 p   ddl }t        |      }|dk(  rt        j                  d      t        |      t        t	        |            k7  rd}t        j                  |      | t        t	        |       t	        |      z
        }t        |      }	t        |      t        t	        |            k7  rd}t        j                  |      t        t        |t        j                                     t        t        |t        j                                     | j                         dk(  rg g g }}}
n(t        fd| j                  |d      D         \  }
}}|j                  j                  ||
|ff||	f|	      }	 |j                  |      S # t        $ r}t        j                  d
|       |d}~ww xY w)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   l   K   | ]+  \  }}}|v r!|v r|   |   |j                  d       f - yw   N)get).0uvd	col_index	row_indexr   s       i/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/matrix.py	<genexpr>z%biadjacency_matrix.<locals>.<genexpr>e   sF      Aq!	>a9n 1y|QUU61-=>s   14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$S wwytw<
S$ 			TC:.tTl%PARzz&!! R!>vhGHcQRs   =F 	F5F00F5T)graphsreturns_graphc                    t        j                  d|      }| j                  \  }|j                  t	              d       |j                  t	        |z         d       fdt        |       D        }| j                  j                  dv r9|j                         r)t        j                  j                  } |d |D              }|j                  ||       |S )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   6   K   | ]  \  }}}||z   |f  y w)N )r   r   r   r   ns       r   r   z*from_biadjacency_matrix.<locals>.<genexpr>   s!     J!Q1q5!}Js   )ir   c              3   R   K   | ]  \  }fd t        |      D          yw)c              3   (   K   | ]	  }d f  ywr
   r9   )r   r   r   r   s     r   r   z4from_biadjacency_matrix.<locals>.<genexpr>.<genexpr>   s     5q!Q5s   N)range)r   wr   r   s     @@r   r   z*from_biadjacency_matrix.<locals>.<genexpr>   s$     O)1a5E!H55Os   #')r   )r   empty_graphr   add_nodes_fromr>   r   r   kindis_multigraphr    chainfrom_iterableadd_weighted_edges_from)r2   create_usingedge_attributer(   mtriplesrD   r:   s          @r   r   r   r   s    J 	q,'A77DAqU1X+U1a!e_2 K.Fq.IJG 	ww||z!aoo&7--OwOOgn=H    )NNr   csr)Nr   )
__doc__r    networkxr   networkx.convert_matrixr   __all___dispatchabler   r   r9   rK   r   <module>rR      sl      <!:
; X&IN_R '_RD T25 35rK   