
    \Mhs(                         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dS )z
Min-heaps.
    )heappopheappush)countN)MinHeapPairingHeap
BinaryHeapc                   f    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dS )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dS )MinHeap._Itemz2Used by subclassess to represent a key-value pair.keyvaluec                 "    || _         || _        d S Nr   )selfr   r   s      T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/networkx/utils/heaps.py__init__zMinHeap._Item.__init__   s    DHDJJJ    c                 8    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=        @@$		 	 		0 	0 	0 	0 	0r   r   c                     i | _         dS )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   Fc                     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       DJ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     dj  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 
0 
0 
0 
0 
0 
0  " " "" " "" " " "&" " " ".            ! ! ! ! !r   r   c                   v     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 | _        d S r   )superr   rB   rC   rD   rE   )r   r   r   	__class__s      r   r   zPairingHeap._Node.__init__   s=    GGS%(((DIDIDIDKKKr   )r   r   r   r   r   r   __classcell__rH   s   @r   _NoderA      sI        	 	 7			 		 		 		 		 		 		 		 		r   rK   c                 V    t                                                       d| _        dS )zInitialize a pairing heap.N)rG   r   _rootr   rH   s    r   r   zPairingHeap.__init__   s$    


r   c                 h    | 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   s0    :"#3444

 011r   c                     | j         t          j        d          | j         }|                     | j                   | _         | j        |j        = |j        |j        fS rP   )rM   rQ   rR   _merge_childrenr"   r   r   )r   min_nodes     r   r)   zPairingHeap.pop   sU    :"#3444:))$*55
Jx|$hn--r   Nc                 L    | j                             |          }||j        n|S r   )r"   r-   r   )r   r   r,   nodes       r   r-   zPairingHeap.get   s&    z~~c""!-tzz7:r   Fc                    | j                             |          }| j        }|||j        k     rM||_        ||ur@||j        j        k     r0|                     |           |                     ||          | _        dS |rI||j        k    r>||_        |                     |          }| |                     | j        |          | _        dS |                     ||          }|| j         |<   ||                     ||          n|| _        dS )NTF)	r"   r-   rM   r   rE   _cut_linkrT   rK   )r   r   r   r/   rW   rootchilds          r   r0   zPairingHeap.insert   s   z~~c""ztz!!"
t##0A(A(AIIdOOO!%D$!7!7DJt ?EDJ$6$6"
,,T22 $!%DJ!>!>DJ 5 ::c5))D"DJsO373CD$///DJ4r   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   sQ     ;##%Dy
DI
	r   c                 
   |j         }d|_         |r| j        }d}	 |j        }|||_        n"|j        } |||          }||_        |}|n|}3|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     y	:D
 D!y< $DI I	tD$'' 	$ ! 9D" I	tD$''  "
 DIDIDKr   c                     |j         }|j        }|||_        n||j        _        d|_         |||_         d|_        d|_        dS )zCut a node from its parent.N)rD   rC   rE   rB   )r   rW   rD   rC   s       r   rY   zPairingHeap._cut
  sO    yyDII#DK	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 2 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                 |    t                                                       g | _        t                      | _        dS )zInitialize a binary heap.N)rG   r   _heapr   _countrN   s    r   r   zBinaryHeap.__init__  s/    
ggr   c                     | j         }|st          j        d          | j        }	 |d         \  }}}||v r|||         k    rnt	          |           -||fS Nzheap is emptyTr   r"   rQ   rR   rf   r   r   dictheapr   _r   s         r   r&   zBinaryHeap.min"  ss    z 	4"?333z	 GME1cd{{uS	11DMMM		
 U|r   c                     | j         }|st          j        d          | j        }	 |d         \  }}}t	          |           ||v r|||         k    rn-||= ||fS ri   rj   rk   s         r   r)   zBinaryHeap.pop0  sz    z 	4"?333z	 GME1cDMMMd{{uS	11		
 IU|r   Nc                 8    | j                             ||          S r   )r"   r-   r+   s      r   r-   zBinaryHeap.get?  s    z~~c7+++r   Fc                    | j         }||v rM||         }||k     s|r;||k    r5|||<   t          | j        |t          | j                  |f           ||k     S dS |||<   t          | j        |t          | j                  |f           dS )NFT)r"   r   rf   rC   rg   )r   r   r   r/   rl   	old_values         r   r0   zBinaryHeap.insertB  s    z$;;S	Iy  ^ 	8I8I
 "S	eT$+->->%DEEEy((5DITZ%dk):):C!@AAA4r   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      s     $ # # # # # # #          
2
2
2t! t! t! t! t! t! t! t!nR R R R R' R R Rj9 9 9 9 9 9 9 9 9 9r   