K is(tdZddlmZmZddlmZddlZgdZGddZ Gdd e Z Gd d e Z y) z Min-heaps. )heappopheappush)countN)MinHeap PairingHeap BinaryHeapc^eZdZdZGddZdZdZdZddZdd Z d Z d Z d Z d Z y)rzBase 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 eZdZdZdZdZdZy) MinHeap._Itemz2Used by subclassess to represent a key-value pair.keyvaluec ||_||_yNr )selfr rs Z/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/utils/heaps.py__init__zMinHeap._Item.__init__sDHDJcDt|j|jfSr)reprr rrs r__repr__zMinHeap._Item.__repr__s4::./ /rN)__name__ __module__ __qualname____doc__ __slots__rrrr_Itemr s@$   0rrci|_y)zInitialize a new min-heap.N_dictrs rrzMinHeap.__init__!s  rct)aQuery 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. NotImplementedErrorrs rminz MinHeap.min% "!rct)aDelete 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$rs rpopz MinHeap.pop4r'rNct)aReturns 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$rr defaults rgetz MinHeap.getCs ""!rct)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$)rr rallow_increases rinsertzMinHeap.insertVs *"!rc,t|jSz"Returns whether the heap if empty.boolr"rs r __nonzero__zMinHeap.__nonzero__mDJJrc,t|jSr2r3rs r__bool__zMinHeap.__bool__qr6rc,t|jS)z2Returns the number of key-value pairs in the heap.)lenr"rs r__len__zMinHeap.__len__us4::rc||jvS)zReturns whether a key exists in the heap. Parameters ---------- key : any hashable object. The key to be looked up. r!)rr s r __contains__zMinHeap.__contains__ysdjj  rrF)rrrrrrr&r)r-r0r5r8r;r=rrrrr s> 0 0 " ""&".  !rrczeZdZdZGddej ZfdZdZdZ d dZ d dZ d Z d Z d ZxZS)rzA pairing heap.c&eZdZdZdZfdZxZS)PairingHeap._NodezA node in a pairing heap. A tree in a pairing heap is stored using the left-child, right-sibling representation. )leftnextprevparentc^t|||d|_d|_d|_d|_yr)superrrBrCrDrE)rr r __class__s rrzPairingHeap._Node.__init__s. G S% (DIDIDIDKr)rrrrrr __classcell__rHs@r_NoderAs 7   rrKc0t|d|_y)zInitialize a pairing heap.N)rGr_rootrrHs rrzPairingHeap.__init__s  rc|jtjd|jj|jjfSNzheap is empty.)rMnx NetworkXErrorr rrs rr&zPairingHeap.mins; :: ""#34 4  0 011rc|jtjd|j}|j|j|_|j|j =|j |j fSrP)rMrQrR_merge_childrenr"r r)rmin_nodes rr)zPairingHeap.pops` :: ""#34 4::))$**5 JJx|| $ hnn--rcX|jj|}| |jS|Sr)r"r-r)rr r,nodes rr-zPairingHeap.gets(zz~~c"!-tzz:7:rc,|jj|}|j}|||jkrM||_||urA||jjkr(|j ||j |||_y|rJ||jkDr;||_|j|}|!|j |j||_y|j||}||j|<||j |||_y||_y)NTF) r"r-rMrrE_cut_linkrTrK)rr rr/rWrootchilds rr0zPairingHeap.insertszz~~c"zz  tzz!" t# 0A0A(AIIdO!%D$!7DJEDJJ$6" ,,T2$!%DJJ!>DJ::c5)D"DJJsO373CD$/DJJNDJrc|j|jkr||}}|j}||_|||_d|_||_||_|S)z_Link two nodes, making the one with the smaller value the parent of the other. N)rrBrCrDrE)rr[otherrCs rrZzPairingHeap._linksT ;; #%Dyy  DI    rc<|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)rBrZrCrDrE)rr[rWlinkrDrC next_next prev_prevs rrTzPairingHeap._merge_childrensyy  ::D Dyy< $DI II D$'  $ 99D" II D$' " DIDIDK rc|j}|j}|||_n||j_d|_|||_d|_d|_y)zCut a node from its parent.N)rDrCrErB)rrWrDrCs rrYzPairingHeap._cut sOyyyy  DI#DKK   DIDI rrr>)rrrrrrrKrr&r)r-r0rZrTrYrIrJs@rrrs@ ( 2 .;"H $L rrc>eZdZdZfdZdZdZddZddZxZ S) rzA binary heap.cNt|g|_t|_y)zInitialize a binary heap.N)rGr_heapr_countrNs rrzBinaryHeap.__init__s  g rc|j}|stjd|j} |d\}}}||vr |||k(r ||fSt |&Nz heap is emptyrr"rQrRrfrrdictheapr_r s rr&zBinaryHeap.min"sizz""?3 3zz GME1cd{uS 1U| DM rc|j}|stjd|j} |d\}}}t |||vr |||k(rn"||=||fSrirjrks rr)zBinaryHeap.pop0spzz""?3 3zz GME1c DMd{uS 1  IU|rc:|jj||Sr)r"r-r+s rr-zBinaryHeap.get?szz~~c7++rc|j}||vrH||}||ks|r;||kDr6|||<t|j|t|j|f||kSy|||<t|j|t|j|fy)NFT)r"rrfrCrg)rr rr/rl old_values rr0zBinaryHeap.insertBszz $;S Iy ^ 8I "S eT$++->%DEy((DI TZZ%dkk):C!@ Arrr>) rrrrrr&r)r-r0rIrJs@rrrs  ,rr) rheapqrr itertoolsrnetworkxrQ__all__rrrrrrrwsB$ 2t!t!nR'Rj99r