
    h(                     t    S r SSKJrJr  SSKJr  SSKr/ SQr " S S5      r	 " S S	\	5      r
 " S
 S\	5      rg)z
Min-heaps.
    )heappopheappush)countN)MinHeapPairingHeap
BinaryHeapc                   j    \ rS rSrSr " S S5      rS rS rS rSS	 jr	SS
 jr
S rS rS rS rSrg)r      zBase class for min-heaps.

A MinHeap stores a collection of key-value pairs ordered by their values.
It supports querying the minimum pair, inserting a new pair, decreasing the
value in an existing pair and deleting the minimum pair.
c                   (    \ rS rSrSrSrS rS rSrg)MinHeap._Item   z2Used by subclassess to represent a key-value pair.keyvaluec                     Xl         X l        g Nr   )selfr   r   s      F/var/www/html/env/lib/python3.13/site-packages/networkx/utils/heaps.py__init__MinHeap._Item.__init__   s    HJ    c                 D    [        U R                  U R                  45      $ r   )reprr   r   r   s    r   __repr__MinHeap._Item.__repr__   s    4::.//r   N)	__name__
__module____qualname____firstlineno____doc__	__slots__r   r   __static_attributes__ r   r   _Itemr      s    @$			0r   r%   c                     0 U l         g)zInitialize a new min-heap.N_dictr   s    r   r   MinHeap.__init__!   s	    
r   c                     [         e)zQuery the minimum key-value pair.

Returns
-------
key, value : tuple
    The key-value pair with the minimum value in the heap.

Raises
------
NetworkXError
    If the heap is empty.
NotImplementedErrorr   s    r   minMinHeap.min%   
     "!r   c                     [         e)zDelete the minimum pair in the heap.

Returns
-------
key, value : tuple
    The key-value pair with the minimum value in the heap.

Raises
------
NetworkXError
    If the heap is empty.
r+   r   s    r   popMinHeap.pop4   r/   r   Nc                     [         e)a)  Returns the value associated with a key.

Parameters
----------
key : hashable object
    The key to be looked up.

default : object
    Default value to return if the key is not present in the heap.
    Default value: None.

Returns
-------
value : object.
    The value associated with the key.
r+   r   r   defaults      r   getMinHeap.getC   s
    " "!r   c                     [         e)a  Insert a new key-value pair or modify the value in an existing
pair.

Parameters
----------
key : hashable object
    The key.

value : object comparable with existing values.
    The value.

allow_increase : bool
    Whether the value is allowed to increase. If False, attempts to
    increase an existing value have no effect. Default value: False.

Returns
-------
decreased : bool
    True if a pair is inserted or the existing value is decreased.
r+   )r   r   r   allow_increases       r   insertMinHeap.insertV   s
    * "!r   c                 ,    [        U R                  5      $ z"Returns whether the heap if empty.boolr(   r   s    r   __nonzero__MinHeap.__nonzero__m       DJJr   c                 ,    [        U R                  5      $ r=   r>   r   s    r   __bool__MinHeap.__bool__q   rB   r   c                 ,    [        U R                  5      $ )z2Returns the number of key-value pairs in the heap.)lenr(   r   s    r   __len__MinHeap.__len__u   s    4::r   c                     XR                   ;   $ )zyReturns whether a key exists in the heap.

Parameters
----------
key : any hashable object.
    The key to be looked up.
r'   )r   r   s     r   __contains__MinHeap.__contains__y   s     jj  r   r'   r   F)r   r   r   r    r!   r%   r   r-   r1   r6   r:   r@   rD   rH   rK   r#   r$   r   r   r   r      s>    
0 
0"""&".  !r   r   c                      ^  \ rS rSrSr " S S\R                  5      rU 4S jrS r	S r
SS jrSS	 jrS
 rS rS rSrU =r$ )r      zA pairing heap.c                   0   ^  \ rS rSrSrSrU 4S jrSrU =r$ )PairingHeap._Node   zrA node in a pairing heap.

A tree in a pairing heap is stored using the left-child, right-sibling
representation.
)leftnextprevparentc                 \   > [         TU ]  X5        S U l        S U l        S U l        S U l        g r   )superr   rS   rT   rU   rV   )r   r   r   	__class__s      r   r   PairingHeap._Node.__init__   s,    GS(DIDIDIDKr   )rS   rT   rV   rU   )	r   r   r   r    r!   r"   r   r#   __classcell__rY   s   @r   _NoderQ      s    	 7			 		r   r]   c                 0   > [         TU ]  5         SU l        g)zInitialize a pairing heap.N)rX   r   _rootr   rY   s    r   r   PairingHeap.__init__   s    
r   c                     U R                   c  [        R                  " S5      eU R                   R                  U R                   R                  4$ Nzheap is empty.)r_   nxNetworkXErrorr   r   r   s    r   r-   PairingHeap.min   s;    ::""#344



 0 011r   c                     U R                   c  [        R                  " S5      eU R                   nU R                  U R                   5      U l         U R                  UR
                  	 UR
                  UR                  4$ rc   )r_   rd   re   _merge_childrenr(   r   r   )r   min_nodes     r   r1   PairingHeap.pop   s`    ::""#344::))$**5
JJx||$hnn--r   c                 Z    U R                   R                  U5      nUb  UR                  $ U$ r   )r(   r6   r   )r   r   r5   nodes       r   r6   PairingHeap.get   s(    zz~~c"!-tzz:7:r   c                 $   U R                   R                  U5      nU R                  nUb  X$R                  :  aK  X$l        XELa@  X$R                  R                  :  a'  U R                  U5        U R                  XT5      U l        gU(       aJ  X$R                  :  a;  X$l        U R                  U5      nUb!  U R                  U R                  U5      U l        gU R                  X5      nX@R                   U'   Ub  U R                  XT5      OUU l        g)NTF)	r(   r6   r_   r   rV   _cut_linkrh   r]   )r   r   r   r9   rl   rootchilds          r   r:   PairingHeap.insert   s    zz~~c"zzzz!"
#0A0A(AIIdO!%D!7DJEJJ$6"
,,T2 $!%DJJ!>DJ  ::c)D"JJsO373CD/DJr   c                     UR                   UR                   :  a  X!p!UR                  nX2l        Ub  X#l        SUl        X!l        Xl        U$ )zOLink two nodes, making the one with the smaller value the parent of
the other.
N)r   rS   rT   rU   rV   )r   rq   otherrT   s       r   rp   PairingHeap._link   sH     ;;#%yy
I
	r   c                 B   UR                   nSUl         Ub  U R                  nSn UR                  nUc  XBl        O$UR                  nU" X%5      nXBl        UnUc  OUnM:  UR                  nUb  UR                  nU" XB5      nUnUb  M  SUl        SUl        SUl        U$ )ztMerge the subtrees of the root using the standard two-pass method.
The resulting subtree is detached from the root.
N)rS   rp   rT   rU   rV   )r   rq   rl   linkrU   rT   	next_next	prev_prevs           r   rh   PairingHeap._merge_children   s     yy	::D
 Dyy< $I II	D' 	$   99D" II	D'  "
 DIDIDKr   c                     UR                   nUR                  nUb  X2l        OX1R                  l        SUl         Ub  X#l         SUl        SUl        g)zCut a node from its parent.N)rU   rT   rV   rS   )r   rl   rU   rT   s       r   ro   PairingHeap._cut
  sI    yyyyI#KK	IDIr   )r_   r   rM   )r   r   r   r    r!   r   r%   r]   r   r-   r1   r6   r:   rp   rh   ro   r#   r[   r\   s   @r   r   r      sE     (
2
.;"H$L r   r   c                   L   ^  \ rS rSrSrU 4S jrS rS rS	S jrS
S jr	Sr
U =r$ )r   i  zA binary heap.c                 N   > [         TU ]  5         / U l        [        5       U l        g)zInitialize a binary heap.N)rX   r   _heapr   _countr`   s    r   r   BinaryHeap.__init__  s    
gr   c                     U R                   nU(       d  [        R                  " S5      eU R                  n[        n US   u  pEnXa;   a  XAU   :X  a   Xd4$ U" U5        M#  Nzheap is emptyr   r(   rd   re   r   r   r   dictheapr1   r   _r   s          r   r-   BinaryHeap.min"  sf    zz""?33zz  GMEc{uS	1| I	 r   c                     U R                   nU(       d  [        R                  " S5      eU R                  n[        n US   u  pEnU" U5        Xa;   a	  XAU   :X  a  OM   X	 Xd4$ r   r   r   s          r   r1   BinaryHeap.pop1  sk    zz""?33zz  GMEcI{uS	1	 
 I|r   c                 8    U R                   R                  X5      $ r   )r(   r6   r4   s      r   r6   BinaryHeap.getA  s    zz~~c++r   c                    U R                   nX;   aJ  XA   nX%:  d  U(       a9  X%:  a4  X$U'   [        U R                  U[        U R                  5      U45        X%:  $ gX$U'   [        U R                  U[        U R                  5      U45        g)NFT)r(   r   r   rT   r   )r   r   r   r9   r   	old_values         r   r:   BinaryHeap.insertD  s    zz;	I ^8I
 "S	eT$++->%DE((ITZZ%dkk):C!@Ar   )r   r   r   rM   )r   r   r   r    r!   r   r-   r1   r6   r:   r#   r[   r\   s   @r   r   r     s$     , r   r   )r!   heapqr   r   	itertoolsr   networkxrd   __all__r   r   r   r$   r   r   <module>r      sB    $  
2t! t!nR' Rj; ;r   