-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Add priority heap implementation using lists. #271
Conversation
Pull Request Test Coverage Report for Build 220
💛 - Coveralls |
I already did it: https://github.com/keon/algorithms/blob/master/heap/binary_heap.py |
That implementation is with a heap which is a much more efficient way of implementing a priority queue however I think It's always a good idea to have different ways of implementing the same data structure. It just opens up your mind to so many different solutions. Just my two cents. @danghai |
I thought it is the same algorithm. Priority queue require 2 popular method |
Yep, mine is bubble down. I am adding context to this comment cuz danghai took it literally. When I said bubble down, what I meant was that I add items based on the lowest priority and for that I am doing comparison to get to appropriate spot (similar to how you'd do a find min in array) |
So it looks the same file that I give you. If you can use linkedlist ( different data structure). It could be a different approach because the file I gave you use a array or a list |
No, your implementation is based off of heap (binary structure). This one is on lists which is not the best in terms of running time but everything has its tradeoffs. |
Could you point out the difference my way and your way? The data structure is the same ( array , and you use list). The algorithm for perceDown is same too. |
The difference is what you use for implementation. Yours is heap which is a tree structure, this one is list even though you're using lists but the implementation is of a tree structure. Here is a much more detailed explanation of implementation of PQ using heaps. |
It is the same! Binary tree structure explain the list/array store these number. Look your file, does it has binary tree structure? |
The implementation that I have doesn't have binary tree structure. It is just a simple but terrible implementation of PQ using lists. The idea is to have as many ways of implementing the same data structure. If you click on the link that I gave you, you should be able to see the difference. |
Could you point out my binary tree structure in my code? :) |
First of all, you have |
So you give me this link: http://pages.cs.wisc.edu/~vernon/cs367/notes/11.PRIORITY-Q.html. And you implement base on it. Do you see and read these example for |
That link contains the implementation of PQ using heaps. That was for your understanding. My implementation is not based on that as I am not doing it that way. |
Furthermore, your method delete is wrong because it misses |
A classic heap as it is typically referred to is usually a min heap. An implementation that has good time complexity (O(log n) on push and pop) and no memory overhead. So my implementation is based on lists/array. |
You mean your priority queue always stores increasing order (MiN priority queue) |
Correct. |
@danghai I think you got a little bit messed up between priority queues and max/min heaps :) Anyone can get confused here because priority queues almost everytime get implemented using heaps (for efficiency). However, the priority queue is just a data structure that stores a group of items and we can extract the min/max item based on the priority. |
So I guess we can merge now? |
Excellent!! Merging it now. |
@goswami-rahul @SaadBenn I am not sure why you merge it, while he does not provide a |
A node usually contains some data. If you look at the |
@SaadBenn I read these book for a long time ago. Return true only for |
@goswami-rahul Can you please do another review? I'll make the adjustments. I'm sure you'll do an objective review. :) |
@danghai Let's leave the review to goswami. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def size(self): | ||
return len(self.priority_queue_list) | ||
|
||
def insert(self, node): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SaadBenn This entire insert
method method can be rewritten as:
def insert(self, node):
for index, current in enumerate(self.priority_queue_list):
if current.priority > node.priority:
self.priority_queue_list.insert(index, node)
return
# when traversed complete queue
self.priority_queue_list.append(node)
I think its better and more clear this way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yeah, there is no point of returning True
here, because it would return True
every time, and there is no case for False
. So, its better to return None
.
Returning True
and False
is more logical if there was, for example, a maxsize restriction in the queue. Then we would have returned True
for every successful insertion, while returned False
if the queue was already full :)
|
||
def delete(self): | ||
# remove the first node from the queue | ||
return self.priority_queue_list.pop(0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can use deque for O(1) popleft
.
I know this is not an efficient way to do it but our repo was missing a priority queue implementation and I thought I might just just give it a try.