
    h}7                     H   S r SSKJr  SSKrSSKJrJr  / SQr\R                  S 5       r
\R                  S 5       rS	 rS
 rS r\R                  SS j5       r\R                  SS j5       r\R                  SS j5       r\" S5      \R                  " SS9S 5       5       rg)z
Eulerian circuits and graphs.
    )combinationsN   )arbitrary_elementnot_implemented_for)is_eulerianeulerian_circuiteulerizeis_semieulerianhas_eulerian_patheulerian_pathc                   ^  T R                  5       (       a2  [        U 4S jT  5       5      =(       a    [        R                  " T 5      $ [        S T R	                  5        5       5      =(       a    [        R
                  " T 5      $ )a  Returns True if and only if `G` is Eulerian.

A graph is *Eulerian* if it has an Eulerian circuit. An *Eulerian
circuit* is a closed walk that includes each edge of a graph exactly
once.

Graphs with isolated vertices (i.e. vertices with zero degree) are not
considered to have Eulerian circuits. Therefore, if the graph is not
connected (or not strongly connected, for directed graphs), this function
returns False.

Parameters
----------
G : NetworkX graph
   A graph, either directed or undirected.

Examples
--------
>>> nx.is_eulerian(nx.DiGraph({0: [3], 1: [2], 2: [3], 3: [0, 1]}))
True
>>> nx.is_eulerian(nx.complete_graph(5))
True
>>> nx.is_eulerian(nx.petersen_graph())
False

If you prefer to allow graphs with isolated vertices to have Eulerian circuits,
you can first remove such vertices and then call `is_eulerian` as below example shows.

>>> G = nx.Graph([(0, 1), (1, 2), (0, 2)])
>>> G.add_node(3)
>>> nx.is_eulerian(G)
False

>>> G.remove_nodes_from(list(nx.isolates(G)))
>>> nx.is_eulerian(G)
True


c              3   j   >#    U  H(  nTR                  U5      TR                  U5      :H  v   M*     g 7fN	in_degree
out_degree).0nGs     K/var/www/html/env/lib/python3.13/site-packages/networkx/algorithms/euler.py	<genexpr>is_eulerian.<locals>.<genexpr>A   s)      
78!AKKNall1o-qs   03c              3   6   #    U  H  u  pUS -  S:H  v   M     g7f)r   r   N r   vds      r   r   r   F   s     1jdaq1uzj   )is_directedallnxis_strongly_connecteddegreeis_connectedr   s   `r   r   r      sg    R 	}}  
78
 
 *&&q)	*
 1ahhj11Hbooa6HH    c                 F    [        U 5      =(       a    [        U 5      (       + $ )zReturn True iff `G` is semi-Eulerian.

G is semi-Eulerian if it has an Eulerian path but no Eulerian circuit.

See Also
--------
has_eulerian_path
is_eulerian
)r   r   r%   s    r   r
   r
   I   s     Q6A$66r&   c                 `  ^  [        T 5      (       d  g[        T 5      (       a  [        T 5      $ T R                  5       (       a6  U 4S jT  5       u  pT R	                  U5      T R                  U5      :  a  U$ U$ T  Vs/ s H  nT R                  U5      S-  S:w  d  M  UPM!     snS   nU$ s  snf )zYReturn a suitable starting vertex for an Eulerian path.

If no path exists, return None.
Nc              3   t   >#    U  H-  nTR                  U5      TR                  U5      :w  d  M)  Uv   M/     g 7fr   r   )r   r   r   s     r   r   #_find_path_start.<locals>.<genexpr>c   s*     DQ!++a.ALLO"C!!Qs   (8	8r   r   )r   r   r   r   r   r   r#   )r   v1v2r   starts   `    r   _find_path_startr.   W   s    
 Q1~~ ##}}DQD<<akk"o-II 6Aq!qA!5A6q9 7s   =B+B+c              #     #    U R                  5       (       a  U R                  nU R                  nOU R                  nU R                  nU/nS nU(       ak  US   nU" U5      S:X  a  Ub  XV4v   UnUR                  5         O5[        U" U5      5      u  pxUR                  U5        U R                  Xh5        U(       a  Mj  g g 7f)Nr   	r   r   	out_edgesr#   edgespopr   appendremove_edge)	r   sourcer#   r3   vertex_stacklast_vertexcurrent_vertex_next_vertexs	            r   _simplegraph_eulerian_circuitr=   p   s     }}8LK
%b).!Q&&"33(K.u^/DENA,MM.6 ,s   B;C?Cc              #     #    U R                  5       (       a  U R                  nU R                  nOU R                  nU R                  nUS 4/nS nS nU(       ar  US   u  pxU" U5      S:X  a  Ub  XWU4v   XxpeUR                  5         O9[        U" USS95      n	U	u  pnUR                  X45        U R                  X{U5        U(       a  Mq  g g 7f)Nr0   r   T)keysr1   )r   r7   r#   r3   r8   r9   last_keyr:   current_keytripler;   r<   next_keys                r   _multigraph_eulerian_circuitrD      s     }}TN#LKH
&22&6#.!Q&&"H==$2&u^$'GHF'-$AH 78MM.x@ ,s   CC
Cc              #     #    [        U 5      (       d  [        R                  " S5      eU R                  5       (       a  U R	                  5       n OU R                  5       n Uc  [        U 5      nU R                  5       (       a*  [        X5       H  u  p4nU(       a  X4U4v   M  X44v   M     g[        X5       Sh  vN   g N7f)az  Returns an iterator over the edges of an Eulerian circuit in `G`.

An *Eulerian circuit* is a closed walk that includes each edge of a
graph exactly once.

Parameters
----------
G : NetworkX graph
   A graph, either directed or undirected.

source : node, optional
   Starting node for circuit.

keys : bool
   If False, edges generated by this function will be of the form
   ``(u, v)``. Otherwise, edges will be of the form ``(u, v, k)``.
   This option is ignored unless `G` is a multigraph.

Returns
-------
edges : iterator
   An iterator over edges in the Eulerian circuit.

Raises
------
NetworkXError
   If the graph is not Eulerian.

See Also
--------
is_eulerian

Notes
-----
This is a linear time implementation of an algorithm adapted from [1]_.

For general information about Euler tours, see [2]_.

References
----------
.. [1] J. Edmonds, E. L. Johnson.
   Matching, Euler tours and the Chinese postman.
   Mathematical programming, Volume 5, Issue 1 (1973), 111-114.
.. [2] https://en.wikipedia.org/wiki/Eulerian_path

Examples
--------
To get an Eulerian circuit in an undirected graph::

    >>> G = nx.complete_graph(3)
    >>> list(nx.eulerian_circuit(G))
    [(0, 2), (2, 1), (1, 0)]
    >>> list(nx.eulerian_circuit(G, source=1))
    [(1, 2), (2, 0), (0, 1)]

To get the sequence of vertices in an Eulerian circuit::

    >>> [u for u, v in nx.eulerian_circuit(G)]
    [0, 2, 1]

zG is not Eulerian.N)
r   r!   NetworkXErrorr   reversecopyr   is_multigraphrD   r=   r   r7   r?   ur   ks         r   r   r      s     ~ q>>344}}IIKFFH~"1%3A>GA!Agd
	 ? 1;;;s   B8C:C ;Cc                 H   [         R                  " U 5      (       a  gU R                  5       (       a  U R                  nU R                  nUb  X1   X!   -
  S:w  a  gSnSnU  H7  nX&   X6   -
  S:X  a  US-  nM  X6   X&   -
  S:X  a  US-  nM+  X&   X6   :w  d  M7    g   US:*  =(       a"    US:*  =(       a    [         R
                  " U 5      $ Ub  U R                  U   S-  S:w  a  g[        S U R                  5        5       5      S:H  =(       a    [         R                  " U 5      $ )a  Return True iff `G` has an Eulerian path.

An Eulerian path is a path in a graph which uses each edge of a graph
exactly once. If `source` is specified, then this function checks
whether an Eulerian path that starts at node `source` exists.

A directed graph has an Eulerian path iff:
    - at most one vertex has out_degree - in_degree = 1,
    - at most one vertex has in_degree - out_degree = 1,
    - every other vertex has equal in_degree and out_degree,
    - and all of its vertices belong to a single connected
      component of the underlying undirected graph.

If `source` is not None, an Eulerian path starting at `source` exists if no
other node has out_degree - in_degree = 1. This is equivalent to either
there exists an Eulerian circuit or `source` has out_degree - in_degree = 1
and the conditions above hold.

An undirected graph has an Eulerian path iff:
    - exactly zero or two vertices have odd degree,
    - and all of its vertices belong to a single connected component.

If `source` is not None, an Eulerian path starting at `source` exists if
either there exists an Eulerian circuit or `source` has an odd degree and the
conditions above hold.

Graphs with isolated vertices (i.e. vertices with zero degree) are not considered
to have an Eulerian path. Therefore, if the graph is not connected (or not strongly
connected, for directed graphs), this function returns False.

Parameters
----------
G : NetworkX Graph
    The graph to find an euler path in.

source : node, optional
    Starting node for path.

Returns
-------
Bool : True if G has an Eulerian path.

Examples
--------
If you prefer to allow graphs with isolated vertices to have Eulerian path,
you can first remove such vertices and then call `has_eulerian_path` as below example shows.

>>> G = nx.Graph([(0, 1), (1, 2), (0, 2)])
>>> G.add_node(3)
>>> nx.has_eulerian_path(G)
False

>>> G.remove_nodes_from(list(nx.isolates(G)))
>>> nx.has_eulerian_path(G)
True

See Also
--------
is_eulerian
eulerian_path
T   Fr   r   c              3   6   #    U  H  u  pUS -  S:H  v   M     g7f)r   rN   Nr   r   s      r   r   $has_eulerian_path.<locals>.<genexpr>K  s     5*$!1q5A:*r   )	r!   r   r   r   r   is_weakly_connectedr#   sumr$   )r   r7   insoutsunbalanced_insunbalanced_outsr   s          r   r   r      s   ~ 
~~a}}kk||$,"<"AAv1$!#36!Q&1$47"  aVOq$8VR=S=STU=V	

 !((6"2Q"6!"; 5!((*55:Qrq?QQr&   c           
   #   ~  #    [        X5      (       d  [        R                  " S5      eU R                  5       (       a  U R	                  5       n Ub  [        R
                  " U 5      SL a  [        U 5      nU R                  5       (       a*  [        X5       H  u  p4nU(       a  X4U4v   M  X44v   M     g[        X5       Sh  vN   gU R                  5       n Uc  [        U 5      nU R                  5       (       as  U(       a6  [        [        X5       VVVs/ s H	  u  p4oTX54PM     snnn5       Sh  vN   g[        [        X5       VVVs/ s H	  u  p4oTU4PM     snnn5       Sh  vN   g[        [        X5       VVs/ s H  u  p4XC4PM
     snn5       Sh  vN   g Ns  snnnf  Nws  snnnf  NJs  snnf  N7f)aG  Return an iterator over the edges of an Eulerian path in `G`.

Parameters
----------
G : NetworkX Graph
    The graph in which to look for an eulerian path.
source : node or None (default: None)
    The node at which to start the search. None means search over all
    starting nodes.
keys : Bool (default: False)
    Indicates whether to yield edge 3-tuples (u, v, edge_key).
    The default yields edge 2-tuples

Yields
------
Edge tuples along the eulerian path.

Warning: If `source` provided is not the start node of an Euler path
will raise error even if an Euler Path exists.
zGraph has no Eulerian paths.NF)r   r!   rF   r   rG   r   r.   rI   rD   r=   rH   reversedrJ   s         r   r   r   N  s    , Q''=>>}}IIK>R^^A.%7%a(F??7Ba'M$J	 C 5Q???FFH>%a(F??#.J1.UV.U71Y.UV   $+G+RS+RaV+RS    $A!$LM$LDA!$LM   @ W
 T
 Nsm   C F=F!AF=F#&F=2F*3F=F,F=(F3)F=F5
F=F;F=#F=,F=5F=directedT)returns_graphc                    U R                  5       S:X  a  [        R                  " S5      e[        R                  " U 5      (       d  [        R                  " S5      eU R                  5        VVs/ s H  u  pUS-  S:X  d  M  UPM     nnn[        R                  " U 5      n [        U5      S:X  a  U $ [        US5       VVs/ s H  u  pAXA[        R                  " XUS904PM     nnn[        U 5      S-   n[        R                  " 5       nU HB  u  pUR                  5        H)  u  pIX:w  d  M  UR                  XAU[        U	5      -
  U	S9  M+     MD     [        R                  " [        [        R                  " U5      5      5      n
U
R                  5        H=  u  pAXt   U   S   nU R!                  [        R"                  R%                  U5      5        M?     U $ s  snnf s  snnf )	a@  Transforms a graph into an Eulerian graph.

If `G` is Eulerian the result is `G` as a MultiGraph, otherwise the result is a smallest
(in terms of the number of edges) multigraph whose underlying simple graph is `G`.

Parameters
----------
G : NetworkX graph
   An undirected graph

Returns
-------
G : NetworkX multigraph

Raises
------
NetworkXError
   If the graph is not connected.

See Also
--------
is_eulerian
eulerian_circuit

References
----------
.. [1] J. Edmonds, E. L. Johnson.
   Matching, Euler tours and the Chinese postman.
   Mathematical programming, Volume 5, Issue 1 (1973), 111-114.
.. [2] https://en.wikipedia.org/wiki/Eulerian_path
.. [3] http://web.math.princeton.edu/math_alive/5/Notes1.pdf

Examples
--------
    >>> G = nx.complete_graph(10)
    >>> H = nx.eulerize(G)
    >>> nx.is_eulerian(H)
    True

r   zCannot Eulerize null graphzG is not connectedr   rN   )r7   target)weightpathr^   )orderr!   NetworkXPointlessConceptr$   rF   r#   
MultiGraphlenr   shortest_pathGraphitemsadd_edgelistmax_weight_matchingr3   add_edges_fromutilspairwise)r   r   r   odd_degree_nodesmodd_deg_pairs_pathsupper_bound_on_max_path_lengthGpPsPbest_matchingr^   s               r   r	   r	     s   V 	wwyA~))*FGG??1344&'hhj?jdaAEQJj?
aA
!
 !!1155DA 
  Q7895   &)VaZ" 
B$HHJDAv!?#a&!Hq    % HHT""8"8"<=>M ##%uQx	**401 & HE @s   /GG$G)NFr   )__doc__	itertoolsr   networkxr!   rj   r   r   __all___dispatchabler   r
   r.   r=   rD   r   r   r   r	   r   r&   r   <module>ry      s    #  : 0I 0If 
7 
727,A0 M< M<` [R [R| 3 3l Z %O & !Or&   