
    wg(                     t    d Z ddlmZmZ ddlmZ ddlZg dZ G d d      Z	 G d d	e	      Z
 G d
 de	      Zy)z
Min-heaps.
    )heappopheappush)countN)MinHeapPairingHeap
BinaryHeapc                   ^    e Zd ZdZ G d d      Zd Zd Zd ZddZdd	Z	d
 Z
d Zd Zd Zy)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                        e Zd ZdZdZd Zd Zy)MinHeap._Itemz2Used by subclassess to represent a key-value pair.keyvaluec                      || _         || _        y Nr   )selfr   r   s      Y/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/utils/heaps.py__init__zMinHeap._Item.__init__   s    DHDJ    c                 D    t        | j                  | j                  f      S r   )reprr   r   r   s    r   __repr__zMinHeap._Item.__repr__   s    4::.//r   N)__name__
__module____qualname____doc__	__slots__r   r    r   r   _Itemr      s    @$			0r   r   c                     i | _         y)zInitialize a new min-heap.N_dictr   s    r   r   zMinHeap.__init__!   s	    
r   c                     t         )a   Query 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   minzMinHeap.min%   
     "!r   c                     t         )a  Delete 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   popzMinHeap.pop4   r'   r   Nc                     t         )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   getzMinHeap.getC   s
    " "!r   c                     t         )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   insertzMinHeap.insertV   s
    * "!r   c                 ,    t        | j                        S z"Returns whether the heap if empty.boolr"   r   s    r   __nonzero__zMinHeap.__nonzero__m       DJJr   c                 ,    t        | j                        S r2   r3   r   s    r   __bool__zMinHeap.__bool__q   r6   r   c                 ,    t        | j                        S )z2Returns the number of key-value pairs in the heap.)lenr"   r   s    r   __len__zMinHeap.__len__u   s    4::r   c                     || j                   v S )zReturns whether a key exists in the heap.

        Parameters
        ----------
        key : any hashable object.
            The key to be looked up.
        r!   )r   r   s     r   __contains__zMinHeap.__contains__y   s     djj  r   r   F)r   r   r   r   r   r   r&   r)   r-   r0   r5   r8   r;   r=   r   r   r   r   r      s>    
0 
0"""&".  !r   r   c                   z     e Zd ZdZ G d dej
                        Z fdZd Zd Z	ddZ
ddZd	 Zd
 Zd Z xZS )r   zA pairing heap.c                   &     e Zd ZdZdZ fdZ xZS )PairingHeap._NodezA 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         |   ||       d | _        d | _        d | _        d | _        y r   )superr   rB   rC   rD   rE   )r   r   r   	__class__s      r   r   zPairingHeap._Node.__init__   s.    GS%(DIDIDIDKr   )r   r   r   r   r   r   __classcell__rH   s   @r   _NoderA      s    	 7			 		r   rK   c                 0    t         |           d| _        y)zInitialize a pairing heap.N)rG   r   _rootr   rH   s    r   r   zPairingHeap.__init__   s    
r   c                     | j                   t        j                  d      | j                   j                  | j                   j                  fS Nzheap is empty.)rM   nxNetworkXErrorr   r   r   s    r   r&   zPairingHeap.min   s;    ::""#344



 0 011r   c                     | j                   t        j                  d      | j                   }| j                  | j                         | _         | j                  |j
                  = |j
                  |j                  fS rP   )rM   rQ   rR   _merge_childrenr"   r   r   )r   min_nodes     r   r)   zPairingHeap.pop   s`    ::""#344::))$**5
JJx||$hnn--r   c                 X    | j                   j                  |      }||j                  S |S r   )r"   r-   r   )r   r   r,   nodes       r   r-   zPairingHeap.get   s(    zz~~c"!-tzz:7:r   c                 ,   | j                   j                  |      }| j                  }|||j                  k  rM||_        ||urA||j                  j                  k  r(| j                  |       | j                  ||      | _        y|rJ||j                  kD  r;||_        | j                  |      }|!| j                  | j                  |      | _        y| j                  ||      }|| j                   |<   || j                  ||      | _        y|| _        y)NTF)	r"   r-   rM   r   rE   _cut_linkrT   rK   )r   r   r   r/   rW   rootchilds          r   r0   zPairingHeap.insert   s    zz~~c"zztzz!"
t#0A0A(AIIdO!%D$!7DJEDJJ$6"
,,T2 $!%DJJ!>DJ  ::c5)D"DJJsO373CD$/DJ JNDJr   c                     |j                   |j                   k  r||}}|j                  }||_        |||_        d|_        ||_        ||_        |S )z_Link two nodes, making the one with the smaller value the parent of
        the other.
        N)r   rB   rC   rD   rE   )r   r[   otherrC   s       r   rZ   zPairingHeap._link   sT     ;;#%Dyy
DI
	r   c                 <   |j                   }d|_         || j                  }d}	 |j                  }|||_        n$|j                  } |||      }||_        |}|n|}:|j                  }||j                  } |||      }|}|d|_        d|_        d|_        |S )zMerge the subtrees of the root using the standard two-pass method.
        The resulting subtree is detached from the root.
        N)rB   rZ   rC   rD   rE   )r   r[   rW   linkrD   rC   	next_next	prev_prevs           r   rT   zPairingHeap._merge_children   s     yy	::D
 Dyy< $DI II	D$' 	$   99D" II	D$'  "
 DIDIDKr   c                     |j                   }|j                  }|||_        n||j                  _        d|_         |||_         d|_        d|_        y)zCut a node from its parent.N)rD   rC   rE   rB   )r   rW   rD   rC   s       r   rY   zPairingHeap._cut
  sO    yyyyDI#DKK	DIDIr   r   r>   )r   r   r   r   r   r   rK   r   r&   r)   r-   r0   rZ   rT   rY   rI   rJ   s   @r   r   r      s@     (
2
.;"H$Lr   r   c                   >     e Zd ZdZ fdZd Zd ZddZddZ xZ	S )	r   zA binary heap.c                 N    t         |           g | _        t               | _        y)zInitialize a binary heap.N)rG   r   _heapr   _countrN   s    r   r   zBinaryHeap.__init__  s    
gr   c                     | j                   }|st        j                  d      | j                  }t        }	 |d   \  }}}||v r|||   k(  r	 ||fS  ||       #Nzheap is emptyr   r"   rQ   rR   rf   r   r   dictheapr)   r   _r   s          r   r&   zBinaryHeap.min"  sn    zz""?33zz  GME1cd{uS	1U| I	 r   c                     | j                   }|st        j                  d      | j                  }t        }	 |d   \  }}} ||       ||v r	|||   k(  rn||= ||fS ri   rj   rk   s          r   r)   zBinaryHeap.pop1  su    zz""?33zz  GME1cId{uS	1	 
 IU|r   c                 :    | j                   j                  ||      S r   )r"   r-   r+   s      r   r-   zBinaryHeap.getA  s    zz~~c7++r   c                    | j                   }||v rH||   }||k  s|r;||kD  r6|||<   t        | j                  |t        | j                        |f       ||k  S y|||<   t        | j                  |t        | j                        |f       y)NFT)r"   r   rf   rC   rg   )r   r   r   r/   rl   	old_values         r   r0   zBinaryHeap.insertD  s    zz$;S	Iy ^	8I
 "S	eT$++->%DEy((DITZZ%dkk):C!@Ar   r   r>   )
r   r   r   r   r   r&   r)   r-   r0   rI   rJ   s   @r   r   r     s     ,r   r   )r   heapqr   r   	itertoolsr   networkxrQ   __all__r   r   r   r   r   r   <module>rw      sB    $  
2t! t!nR' Rj; ;r   