Python calculates the least-priority queue code share

Source: Internet
Author: User
Copy CodeThe code is as follows:


Class Heap (object):

def parent (CLS, i):
"" "Parent Node Subscript" ""
return int ((i-1) >> 1);

Def left (CLS, i):
"" "Left son subscript" ""
Return (I << 1) + 1;

def right (CLS, i):
"" "Right son subscript" ""
Return (I << 1) + 2;

Class Minpriorityqueue (list, Heap):

Def min_heapify (CLS, A, I, heap_size):
"" "Minimum heap a[i] is the subtree of the root" "
L, R = Cls.left (i), cls.right (i)
If L < heap_size and a[l] < A[i]:
least = L
least = I
If r < Heap_size and A[r] < A[least]:
least = R
If least! = I:
A[i], a[least] = A[least], a[i]
Cls.min_heapify (A, least, Heap_size)

def minimum (self):
"" Returns the minimum element with the pseudo code as follows:
Heap-minimum (A)
1 return a[1]

T (n) = O (1)
return self[0]

def extract_min (self):
"" To remove and return the minimum element, the pseudo code is as follows:
Heap-extract-min (A)
1 if heap-size[a] < 1
2 then Error "Heap Underflow"
3 Min←a[1]
4 A[1]←a[heap-size[a]]//tail element put to first position
5 Heap-size[a]←heap-size[a]-1//decrease Heap-size[a]
6 Min-heapify (A, 1)//Keep minimum heap properties
7 return min

T (n) =θ (LGN)
Heap_size = Len (self)
Assert heap_size > 0, "Heap underflow"
val = self[0]
Tail = heap_size-1
Self[0] = Self[tail]
Self.min_heapify (self, 0, tail)
Self.pop (tail)
Return Val

def decrease_key (self, I, key):
"" To reduce the value of I to key, the pseudo code is as follows:
Heap-decrease-key (A, I, KEY)
1 if key > A[i]
2 then error "new key was larger than current key"
3 A[i]←key
4 while I > 1 and a[parent (i)] > A[i]//is not a root node and the parent node is larger.
5 Do Exchange a[i]↔a[parent (i)]//Swap two elements
6 i←parent (i)//point to parent node location

T (n) =θ (LGN)
val = self[i]
Assert key <= Val, "New key is larger than current key"
Self[i] = key
Parent = Self.parent
While I > 0 and Self[parent (i)] > Self[i]:
Self[i], self[parent (i)] = Self[parent (i)], Self[i]
i = parent (i)

def insert (self, key):
"" Insert key into a, pseudo code as follows:
Min-heap-insert (A, key)
1 Heap-size[a]←heap-size[a] + 1//increase in number of elements
2 a[heap-size[a]]←+∞//Initial new added element is +∞
3 Heap-decrease-key (A, Heap-size[a], key)//reduce new elements to KEY

T (n) =θ (LGN)
Self.append (float (' inf '))
Self.decrease_key (Len (self)-1, key)

if __name__ = = ' __main__ ':
Import Random

Keys = Range (10)
Random.shuffle (keys)
Print (keys)

Queue = Minpriorityqueue () # Insert method to build minimum heap
For I in Keys:
Queue.insert (i)
Print (queue)

Print (' * ' * 30)

For I in range (len):
val = i% 3
If val = = 0:
val = queue.extract_min () # removes and returns the smallest element
Elif val = = 1:
val = queue.minimum () # returns the smallest element
val = queue[1]-10
Queue.decrease_key (1, Val) # Queue[1] reduced by 10
Print (queue, Val)

Print ([Queue.extract_min () for I in range (len (queue))])

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.