
    wg                      *   d Z ddlZddlmZ ddlmZ g dZej                  d        Z	ej                  d        Z
ej                  d        Zej                  dd	       Z ej                  d
      d        Z ej                  d
d      dd       Zy)zR
==========================
Bipartite Graph Algorithms
==========================
    N)connected_components)AmbiguousSolution)is_bipartiteis_bipartite_node_setcolorsetsdensitydegreesc                      j                         rddl fd}n j                  }i } D ]  }||v st         |         dk(  r|g}d||<   |s$|j	                         }d||   z
  } ||      D ]=  }||v r!||   ||   k(  st        j                  d      |||<   |j                  |       ? |rc |j                  t        j                  t        j                         d             |S )a  Returns a two-coloring of the graph.

    Raises an exception if the graph is not bipartite.

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

    Returns
    -------
    color : dictionary
        A dictionary keyed by node with a 1 or 0 as data for each node color.

    Raises
    ------
    NetworkXError
        If the graph is not two-colorable.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> c = bipartite.color(G)
    >>> print(c)
    {0: 1, 1: 0, 2: 1, 3: 0}

    You can use this to set a node attribute indicating the bipartite set:

    >>> nx.set_node_attributes(G, c, "bipartite")
    >>> print(G.nodes[0]["bipartite"])
    1
    >>> print(G.nodes[1]["bipartite"])
    0
    r   Nc                 z    j                   j                  j                  |       j                  |       g      S N)chainfrom_iterablepredecessors
successors)vG	itertoolss    h/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/bipartite/basic.py	neighborszcolor.<locals>.neighbors<   s/    ??00!..2CQ\\RS_1UVV       zGraph is not bipartite.)is_directedr   r   lenpopnxNetworkXErrorappendupdatedictfromkeysisolates)	r   r   r   nqueuer   cwr   s	   `       @r   r   r      s    H 	}}	W KK	E $:QqTaa		AE!HAq\ $:Qx58+ ../HII E!HLLO$ $  
LLr{{1~q12Lr   c                 N    	 t        |        y# t        j                  $ r Y yw xY w)aF  Returns True if graph G is bipartite, False if not.

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

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> print(bipartite.is_bipartite(G))
    True

    See Also
    --------
    color, is_bipartite_node_set
    TF)r   r   r   )r   s    r   r   r   W   s)    &a s    $$c                 <    t        |      }t        |      t        |      k  rt        d       fdt               D        D ]W  }t	        |      \  }}|j                  |      r|j                  |      r4|j                  |      r|j                  |      rW y y)a  Returns True if nodes and G/nodes are a bipartition of G.

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

    nodes: list or container
      Check if nodes are a one of a bipartite set.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> X = set([1, 3])
    >>> bipartite.is_bipartite_node_set(G, X)
    True

    Notes
    -----
    An exception is raised if the input nodes are not distinct, because in this
    case some bipartite algorithms will yield incorrect results.
    For connected graphs the bipartite sets are unique.  This function handles
    disconnected graphs.
    zThe input node set contains duplicates.
This may lead to incorrect results when using it in bipartite algorithms.
Consider using set(nodes) as the inputc              3   \   K   | ]#  }j                  |      j                          % y wr   )subgraphcopy).0r%   r   s     r   	<genexpr>z(is_bipartite_node_set.<locals>.<genexpr>   s"     Eqzz!}!!#Es   ),FT)setr   r   r   r   issubset
isdisjoint)r   nodesSCCXYs   `     r   r   r   q   s    4 	E
A
1vE
5
 	
 F-A!-DE Bx1ZZ]q||AAJJqMallSTo r   c                    | j                         rt        j                  }nt        j                  }|t	        |      }t	        |       |z
  }||fS  ||       sd}t        j
                  |      t        |       }|j                         D ch c]
  \  }}|s	| }}}|j                         D ch c]
  \  }}|r	| }}}||fS c c}}w c c}}w )a;  Returns bipartite node sets of graph G.

    Raises an exception if the graph is not bipartite or if the input
    graph is disconnected and thus more than one valid solution exists.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

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

    top_nodes : container, optional
      Container with all nodes in one bipartite node set. If not supplied
      it will be computed. But if more than one solution exists an exception
      will be raised.

    Returns
    -------
    X : set
      Nodes from one side of the bipartite graph.
    Y : set
      Nodes from the other side.

    Raises
    ------
    AmbiguousSolution
      Raised if the input bipartite graph is disconnected and no container
      with all nodes in one bipartite set is provided. When determining
      the nodes in each bipartite set more than one valid solution is
      possible if the input graph is disconnected.
    NetworkXError
      Raised if the input graph is not bipartite.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.path_graph(4)
    >>> X, Y = bipartite.sets(G)
    >>> list(X)
    [0, 2]
    >>> list(Y)
    [1, 3]

    See Also
    --------
    color

    z:Disconnected graph: Ambiguous solution for bipartite sets.)r   r   is_weakly_connectedis_connectedr.   r   r   items)	r   	top_nodesr8   r4   r5   msgr%   r#   is_tops	            r   r   r      s    d 	}}--	NFQJ q6M ANC&&s++!H !	491fVQ44 !	891fQ88q6M 58s   
C	C	3
C>CB)graphsc                     t        |       }t        j                  |       }t        |      }||z
  }|dk(  rd}|S | j                         r|d|z  |z  z  }|S |||z  z  }|S )ah  Returns density of bipartite graph B.

    Parameters
    ----------
    B : NetworkX graph

    nodes: list or container
      Nodes in one node set of the bipartite graph.

    Returns
    -------
    d : float
       The bipartite density

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.complete_bipartite_graph(3, 2)
    >>> X = set([0, 1, 2])
    >>> bipartite.density(G, X)
    1.0
    >>> Y = set([3, 4])
    >>> bipartite.density(G, Y)
    1.0

    Notes
    -----
    The container of nodes passed as argument must contain all nodes
    in one of the two bipartite node sets to avoid ambiguity in the
    case of disconnected graphs.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------
    color
    r   g           )r   r   number_of_edgesr   )r=   r1   r#   mnbntds          r   r	   r	      s{    N 	AA
1A	UB	
RBAv H	 ==?QVb[!A H R"WAHr   weight)r>   
edge_attrsc                 |    t        |      }t        |       |z
  }| j                  ||      | j                  ||      fS )a  Returns the degrees of the two node sets in the bipartite graph B.

    Parameters
    ----------
    B : NetworkX graph

    nodes: list or container
      Nodes in one node set of the bipartite graph.

    weight : string or None, optional (default=None)
       The edge attribute that holds the numerical value used as a weight.
       If None, then each edge has weight 1.
       The degree is the sum of the edge weights adjacent to the node.

    Returns
    -------
    (degX,degY) : tuple of dictionaries
       The degrees of the two bipartite sets as dictionaries keyed by node.

    Examples
    --------
    >>> from networkx.algorithms import bipartite
    >>> G = nx.complete_bipartite_graph(3, 2)
    >>> Y = set([3, 4])
    >>> degX, degY = bipartite.degrees(G, Y)
    >>> dict(degX)
    {0: 2, 1: 2, 2: 2}

    Notes
    -----
    The container of nodes passed as argument must contain all nodes
    in one of the two bipartite node sets to avoid ambiguity in the
    case of disconnected graphs.
    See :mod:`bipartite documentation <networkx.algorithms.bipartite>`
    for further details on how bipartite graphs are handled in NetworkX.

    See Also
    --------
    color, density
    )r.   degree)r=   r1   rF   bottomtops        r   r
   r
     s<    T ZF
a&6/CHHS&!188FF#;<<r   r   )__doc__networkxr   networkx.algorithms.componentsr   networkx.exceptionr   __all___dispatchabler   r   r   r   r	   r
    r   r   <module>rS      s     ? 0 > >B  2 ) )X ? ?D 1 1h 2+= 3+=r   