-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Min head with decrease key functionality #1202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2f86237
Min head with decrease key functionality
Raj1998 67cfb52
doctest added
Raj1998 a871188
__str__ changed as per Python convention
Raj1998 853ca2c
edits in doctest
Raj1998 bfebbf5
get_value by key added
Raj1998 6741f49
__getitem__ added
Raj1998 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# Min head data structure | ||
# with decrease key functionality - in O(log(n)) time | ||
|
||
|
||
class MinHeap: | ||
def __init__(self, array): | ||
self.idx_of_element = {} | ||
self.heap = self.buildHeap(array) | ||
|
||
def getParentIdx(self, idx): | ||
return (idx - 1) // 2 | ||
|
||
def getLeftChildIdx(self, idx): | ||
return idx * 2 + 1 | ||
|
||
def getRightChildIdx(self, idx): | ||
return idx * 2 + 2 | ||
|
||
def buildHeap(self, array): | ||
lastIdx = len(array) - 1 | ||
startFrom = self.getParentIdx(lastIdx) | ||
|
||
for idx, i in enumerate(array): | ||
self.idx_of_element[i] = idx | ||
|
||
for i in range(startFrom, -1, -1): | ||
self.siftDown(i, array) | ||
return array | ||
|
||
# this is min-heapify method | ||
def siftDown(self, idx, array): | ||
while True: | ||
l = self.getLeftChildIdx(idx) | ||
r = self.getRightChildIdx(idx) | ||
|
||
smallest = idx | ||
if l < len(array) and array[l] < array[idx]: | ||
smallest = l | ||
if r < len(array) and array[r] < array[smallest]: | ||
smallest = r | ||
|
||
if smallest != idx: | ||
array[idx], array[smallest] = array[smallest], array[idx] | ||
self.idx_of_element[array[idx]], self.idx_of_element[ | ||
array[smallest] | ||
] = ( | ||
self.idx_of_element[array[smallest]], | ||
self.idx_of_element[array[idx]], | ||
) | ||
idx = smallest | ||
else: | ||
break | ||
|
||
def siftUp(self, idx): | ||
p = self.getParentIdx(idx) | ||
while p >= 0 and self.heap[p] > self.heap[idx]: | ||
self.heap[p], self.heap[idx] = self.heap[idx], self.heap[p] | ||
self.idx_of_element[self.heap[p]], self.idx_of_element[self.heap[idx]] = ( | ||
self.idx_of_element[self.heap[idx]], | ||
self.idx_of_element[self.heap[p]], | ||
) | ||
idx = p | ||
p = self.getParentIdx(idx) | ||
|
||
def peek(self): | ||
return self.heap[0] | ||
|
||
def remove(self): | ||
self.heap[0], self.heap[-1] = self.heap[-1], self.heap[0] | ||
self.idx_of_element[self.heap[0]], self.idx_of_element[self.heap[-1]] = ( | ||
self.idx_of_element[self.heap[-1]], | ||
self.idx_of_element[self.heap[0]], | ||
) | ||
|
||
x = self.heap.pop() | ||
del self.idx_of_element[x] | ||
self.siftDown(0, self.heap) | ||
return x | ||
|
||
def insert(self, value): | ||
self.heap.append(value) | ||
self.idx_of_element[value] = len(self.heap) - 1 | ||
self.siftUp(len(self.heap) - 1) | ||
|
||
def isEmpty(self): | ||
return True if len(self.heap) == 0 else False | ||
|
||
def decreaseKey(self, key, newValue): | ||
assert ( | ||
self.heap[self.idx_of_element[key]].val > newValue | ||
), "newValue must be less that current value" | ||
key.val = newValue | ||
self.siftUp(self.idx_of_element[key]) | ||
|
||
|
||
class Node: | ||
def __init__(self, val, name): | ||
self.val = val | ||
self.name = name | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
def __lt__(self, other): | ||
return self.val < other.val | ||
|
||
|
||
## USAGE | ||
|
||
r = Node(-1, "R") | ||
b = Node(6, "B") | ||
a = Node(3, "A") | ||
x = Node(1, "X") | ||
e = Node(4, "E") | ||
|
||
arr = [r, b, a, x, e] | ||
|
||
# Use one of these two ways to generate Min-Heap | ||
|
||
# Generating Min-Heap from array | ||
myMinHeap = MinHeap(arr) | ||
|
||
# Generating Min-Heap by Insert method | ||
# myMinHeap.insert(a) | ||
# myMinHeap.insert(b) | ||
# myMinHeap.insert(x) | ||
# myMinHeap.insert(r) | ||
# myMinHeap.insert(e) | ||
|
||
# Before | ||
print("Min Heap - before decrease key") | ||
for i in myMinHeap.heap: | ||
print(i, i.val) | ||
|
||
print("Min Heap - After decrease key of node [B -> -17]") | ||
myMinHeap.decreaseKey(b, -17) | ||
|
||
# After | ||
for i in myMinHeap.heap: | ||
print(i, i.val) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.