
    wg;-                        d Z ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZ g dZd Z e
d       e
d	      ej                  d
                      Z e
d       e
d	      ej                  d                      Z ed       ej                  dd      dd              Z e
d       e
d	      ej                  d                      Z e
d       e
d	       ej                  dddii      d                      Z e
d       e
d	      ej                  d                      Z e
d       e
d	       ej                  d      d                      Zy)a  Functions concerning tournament graphs.

A `tournament graph`_ is a complete oriented graph. In other words, it
is a directed graph in which there is exactly one directed edge joining
each pair of distinct nodes. For each function in this module that
accepts a graph as input, you must provide a tournament graph. The
responsibility is on the caller to ensure that the graph is a tournament
graph:

    >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
    >>> nx.is_tournament(G)
    True

To access the functions in this module, you must access them through the
:mod:`networkx.tournament` module::

    >>> nx.tournament.is_reachable(G, 0, 1)
    True

.. _tournament graph: https://en.wikipedia.org/wiki/Tournament_%28graph_theory%29

    )combinationsN)is_simple_path)arbitrary_elementnot_implemented_forpy_random_state)hamiltonian_pathis_reachableis_strongly_connectedis_tournamentrandom_tournamentscore_sequencec                     t        |       D ]  \  }} ||      s|c S  	 dz   S # t        $ r}t        d      |d}~ww xY w)a  Returns the index of the first element in `iterable` that
    satisfies the given condition.

    If no such element is found (that is, when the iterable is
    exhausted), this returns the length of the iterable (that is, one
    greater than the last index of the iterable).

    `iterable` must not be empty. If `iterable` is empty, this
    function raises :exc:`ValueError`.

       ziterable must be non-emptyN)	enumerate	NameError
ValueError)iterable	conditionixerrs        c/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/tournament.pyindex_satisfyingr   (   sW     (# 1Q<H@1u @56C?@s   & 	A ;A 
undirected
multigraphc                 t     t         fdt         d      D              xr t        j                         dk(  S )a  Returns True if and only if `G` is a tournament.

    A tournament is a directed graph, with neither self-loops nor
    multi-edges, in which there is exactly one directed edge joining
    each pair of distinct nodes.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    bool
        Whether the given graph is a tournament graph.

    Examples
    --------
    >>> G = nx.DiGraph([(0, 1), (1, 2), (2, 0)])
    >>> nx.is_tournament(G)
    True

    Notes
    -----
    Some definitions require a self-loop on each node, but that is not
    the convention used here.

    c              3   B   K   | ]  \  }}||   v ||   v z    y wN .0uvGs      r   	<genexpr>z is_tournament.<locals>.<genexpr>e   s*     E$!QQ!A$Y1!9%Es      r   )allr   nxnumber_of_selfloopsr$   s   `r   r   r   C   s8    D 	E,q!2DEE 	+""1%*    c                     t               dk(  rg S t               dk(  rt               gS t               t         j                  t	               hz
              }t        | fd      }|j                  |       |S )ar  Returns a Hamiltonian path in the given tournament graph.

    Each tournament has a Hamiltonian path. If furthermore, the
    tournament is strongly connected, then the returned Hamiltonian path
    is a Hamiltonian cycle (by joining the endpoints of the path).

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    path : list
        A list of nodes which form a Hamiltonian path in `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)])
    >>> nx.is_tournament(G)
    True
    >>> nx.tournament.hamiltonian_path(G)
    [0, 1, 2, 3]

    Notes
    -----
    This is a recursive implementation with an asymptotic running time
    of $O(n^2)$, ignoring multiplicative polylogarithmic factors, where
    $n$ is the number of nodes in the graph.

    r   r   c                     |    vS r   r   )r"   r$   r#   s    r   <lambda>z"hamiltonian_path.<locals>.<lambda>   s    1 r+   )lenr   r   subgraphsetr   insert)r$   hampathindexr#   s   `  @r   r   r   j   sx    F 1v{	
1v{!!$%%!Aqzz#a&A3,78G W&=>ENN5!Nr+   r   T)graphsreturns_graphc                     fdt        | | dz
  z  dz        D        }t        t        |       d      }d t        ||      D        }t        j                  |      S )a  Returns a random tournament graph on `n` nodes.

    Parameters
    ----------
    n : int
        The number of nodes in the returned graph.
    seed : integer, random_state, or None (default)
        Indicator of random number generation state.
        See :ref:`Randomness<randomness>`.

    Returns
    -------
    G : DiGraph
        A tournament on `n` nodes, with exactly one directed edge joining
        each pair of distinct nodes.

    Notes
    -----
    This algorithm adds, for each pair of distinct nodes, an edge with
    uniformly random orientation. In other words, `\binom{n}{2}` flips
    of an unbiased coin decide the orientations of the edges in the
    graph.

    c              3   >   K   | ]  }j                           y wr   )random)r!   r   seeds     r   r%   z$random_tournament.<locals>.<genexpr>   s     >qT[[]>s   r   r&   c              3   B   K   | ]  \  \  }}}|d k  r||fn||f  yw)g      ?Nr   )r!   r"   r#   rs       r   r%   z$random_tournament.<locals>.<genexpr>   s+     Nyv1qq3waVQF*Ns   )ranger   zipr(   DiGraph)nr:   coinspairsedgess    `   r   r   r      sQ    8 ?E1A;1*<$=>Eq1%ENCu<MNE::er+   c                 B    t        d | j                         D              S )a)  Returns the score sequence for the given tournament graph.

    The score sequence is the sorted list of the out-degrees of the
    nodes of the graph.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    list
        A sorted list of the out-degrees of the nodes of `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 0), (1, 3), (0, 2), (0, 3), (2, 1), (3, 2)])
    >>> nx.is_tournament(G)
    True
    >>> nx.tournament.score_sequence(G)
    [1, 1, 2, 2]

    c              3   &   K   | ]	  \  }}|  y wr   r   )r!   r#   ds      r   r%   z!score_sequence.<locals>.<genexpr>   s     /1!/s   )sorted
out_degreer*   s    r   r   r      s    8 ////r+   r$   weight)preserve_edge_attrsc                 J    t        j                  |       }||j                  z
  S )a  Returns the tournament matrix for the given tournament graph.

    This function requires SciPy.

    The *tournament matrix* of a tournament graph with edge set *E* is
    the matrix *T* defined by

    .. math::

       T_{i j} =
       \begin{cases}
       +1 & \text{if } (i, j) \in E \\
       -1 & \text{if } (j, i) \in E \\
       0 & \text{if } i == j.
       \end{cases}

    An equivalent definition is `T = A - A^T`, where *A* is the
    adjacency matrix of the graph `G`.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    SciPy sparse array
        The tournament matrix of the tournament graph `G`.

    Raises
    ------
    ImportError
        If SciPy is not available.

    )r(   adjacency_matrixT)r$   As     r   tournament_matrixrO      s"    N 	AAqss7Nr+   c                 z     d }d  D cg c]  } | |       }}t         fd|D              S c c}w )a  Decides whether there is a path from `s` to `t` in the
    tournament.

    This function is more theoretically efficient than the reachability
    checks than the shortest path algorithms in
    :mod:`networkx.algorithms.shortest_paths`.

    The given graph **must** be a tournament, otherwise this function's
    behavior is undefined.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    s : node
        A node in the graph.

    t : node
        A node in the graph.

    Returns
    -------
    bool
        Whether there is a path from `s` to `t` in `G`.

    Examples
    --------
    >>> G = nx.DiGraph([(1, 0), (1, 3), (1, 2), (2, 3), (2, 0), (3, 0)])
    >>> nx.is_tournament(G)
    True
    >>> nx.tournament.is_reachable(G, 1, 3)
    True
    >>> nx.tournament.is_reachable(G, 3, 2)
    False

    Notes
    -----
    Although this function is more theoretically efficient than the
    generic shortest path functions, a speedup requires the use of
    parallelism. Though it may in the future, the current implementation
    does not use parallelism, thus you may not see much of a speedup.

    This algorithm comes from [1].

    References
    ----------
    .. [1] Tantau, Till.
           "A note on the complexity of the reachability problem for
           tournaments."
           *Electronic Colloquium on Computational Complexity*. 2001.
           <http://eccc.hpi-web.de/report/2001/092/>
    c                 z      D ch c]'  k(  s    v st         fd D              s&) c}S c c}w )aK  Returns the set of nodes at distance at most two from `v`.

        `G` must be a graph and `v` a node in that graph.

        The returned set includes the nodes at distance zero (that is,
        the node `v` itself), the nodes at distance one (that is, the
        out-neighbors of `v`), and the nodes at distance two.

        c              3   <   K   | ]  }t        |g        y wr   )is_path)r!   zr$   r#   r   s     r   r%   z9is_reachable.<locals>.two_neighborhood.<locals>.<genexpr>K  s     6XQRwq1a)7L6Xs   )any)r$   r#   r   s   ```r   two_neighborhoodz&is_reachable.<locals>.two_neighborhood@  s@     
AFa1Q4i36XVW6X3XA
 	
 
s   '88c                 H     t         fdt               z
  D              S )zDecides whether the given set of nodes is closed.

        A set *S* of nodes is *closed* if for each node *u* in the graph
        not in *S* and for each node *v* in *S*, there is an edge from
        *u* to *v*.

        c              3   :   K   | ]  }D ]  }||   v    y wr   r   )r!   r"   r#   r$   nodess      r   r%   z2is_reachable.<locals>.is_closed.<locals>.<genexpr>V  s%     DeD1!9D9Ds   )r'   r1   )r$   rY   s   ``r   	is_closedzis_reachable.<locals>.is_closedN  s     Dc!funDDDr+   c              3   J   K   | ]  } |      xr
 |v xr |v   y wr   r   )r!   Sr$   rZ   sts     r   r%   zis_reachable.<locals>.<genexpr>Y  s,     V1IaO=Q=1A:>Vs    #r'   )r$   r]   r^   rV   r#   neighborhoodsrZ   s   ```   @r   r	   r	     sB    t
E 677%a+7M7VVVV 8s   8 tournament_is_strongly_connected)namec                 ,     t         fd D              S )a  Decides whether the given tournament is strongly connected.

    This function is more theoretically efficient than the
    :func:`~networkx.algorithms.components.is_strongly_connected`
    function.

    The given graph **must** be a tournament, otherwise this function's
    behavior is undefined.

    Parameters
    ----------
    G : NetworkX graph
        A directed graph representing a tournament.

    Returns
    -------
    bool
        Whether the tournament is strongly connected.

    Examples
    --------
    >>> G = nx.DiGraph([(0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 0)])
    >>> nx.is_tournament(G)
    True
    >>> nx.tournament.is_strongly_connected(G)
    True
    >>> G.remove_edge(3, 0)
    >>> G.add_edge(0, 3)
    >>> nx.is_tournament(G)
    True
    >>> nx.tournament.is_strongly_connected(G)
    False

    Notes
    -----
    Although this function is more theoretically efficient than the
    generic strong connectivity function, a speedup requires the use of
    parallelism. Though it may in the future, the current implementation
    does not use parallelism, thus you may not see much of a speedup.

    This algorithm comes from [1].

    References
    ----------
    .. [1] Tantau, Till.
           "A note on the complexity of the reachability problem for
           tournaments."
           *Electronic Colloquium on Computational Complexity*. 2001.
           <http://eccc.hpi-web.de/report/2001/092/>

    c              3   F   K   | ]  }D ]  }t        ||         y wr   )r	   r    s      r   r%   z(is_strongly_connected.<locals>.<genexpr>  s'     ;;A|Aq!$;$;s   !r_   r*   s   `r   r
   r
   \  s    n ;a;;;r+   r   )__doc__	itertoolsr   networkxr(    networkx.algorithms.simple_pathsr   rS   networkx.utilsr   r   r   __all__r   _dispatchabler   r   r   r   rO   r	   r
   r   r+   r   <module>rl      s  . #  F R R@6 \"\"!  # #!H \"\"*  # #*Z T2 3 @ \"\"0  # #08 \"\"sXqM&:;% < # #%P \"\"PW  # #PWf \"\"9:4< ; # #4<r+   