0

I'm a Python newbie since I've been coding in C++ and JAVA all along. I was implementing Dijkstra's algorithm which needed me to implement a Priority Queue/Set which stores a Tuple(a,b) where 'a' is a vertex and 'b' is the weight. I the ordering to be according to the 'b' parameter so that when i a.pop() where 'a' is my set, the Tuple with the least weight is popped. How do I got about implementing this? All I know is:

a=set()

I have no idea how to implement the comparator with this. Any help will be appreciated

gabbar0x
  • 4,046
  • 5
  • 31
  • 51
  • 1
    You can use the [`heapq`](https://docs.python.org/dev/library/heapq.html) module. – thefourtheye Dec 10 '15 at 10:12
  • I was looking for a custom comparator with a set. A sample implementation in your answer would help atleast :) – gabbar0x Dec 10 '15 at 10:13
  • 4
    http://stackoverflow.com/questions/3954530/how-to-make-heapq-evaluate-the-heap-off-of-a-specific-attribute – pvg Dec 10 '15 at 10:20
  • You can just add that as an answer (the 2nd answer on that page). I'll accept it. I don't want to delete the question as it could be useful for someone else – gabbar0x Dec 10 '15 at 10:22

0 Answers0