0

How does priority queue works ? I started to study Dijkstra's algorithm, googled it and got a lot of code where different coders used different versions of priority queues. In one code I have noticed that he uses this declaration

priority_queue <pii, vector <pii>, comp> Q;
//pii means pair <int,int>
// And comp is compare structure  which I also cannot understand 

Comp goes like this

struct comp {
  bool operator() (const pii &a, const pii &b) {
    return a.second > b.second;
  }
};

Can anyone please explain me what is going on here? Also how many versions of priority_queue declarations are there in c++?

Dan
  • 2,701
  • 1
  • 29
  • 34
run_time_error
  • 697
  • 1
  • 8
  • 22

3 Answers3

1

The C++ priority queue is:

template <class T, class Container = vector<T>,
  class Compare = less<typename Container::value_type> > class priority_queue;

In this:

T is the type to be stored in the queue; hopefully this is plain enough.

Container is the underlying storage within the queue. As the declaration shows, this defaults to the std::vector<T>, and normally one can ignore it.

Compare' is the method for determining the ordering in the queue. Again, it has a default and can often be ignored.Compare` is a type supporting a function call which can compare two elements in the queue and determine their ordering.

less is the default and simply applies the normal < operator.

When one wants an ordering other than that defined by <, one defines a type which provides the needed comparison. As you have:

struct comp {
  bool operator() (const pii &a, const pii &b) {
    return a.second > b.second;
  }
};

Note how this takes two pii and compares using the > operator; this gives the reverse of the default ordering in the queue.

This comp type is then specified as the third parameter to the template.

Having specified a third parameter then, with templates as with functions, the second parameter must also be specified even though we only want the same as the default, std::vector<pii>.

Why the specification of the underlying container? This is the container adaptor pattern. The idea is that the semantics of Priority Queues do not necessarily imply the underlying way in which the data is to be stored. Also the standard provides a number if data structures specifically focused on storage. Therefore, why not have the priority_queue allow a choice of the underlying storage? This is the adaptor concept.

Keith
  • 6,756
  • 19
  • 23
0

Check this

and also check this dijkstra's algorithm - in c++?

Community
  • 1
  • 1
Shyam
  • 6,376
  • 1
  • 24
  • 38
  • Actually, i sought for help from first site previously. Could you please tell me what is the use of vector inside the declaration? This vector troubles me most here. Thanks again – run_time_error Dec 26 '13 at 06:25
0

If you read the reference for the priority_queue you can see that the first argument is the type and the second argument is the container and the third is the comparison class. Also as far as I know there is only one priority queue in the C++ standard, I've implemented my own custom queue using a binary search tree which is a very useful exercise that I recommend.

In the example of Dijkstra's algorithm at the zobayer blog this is all a bit obfuscated due to the use of Macros in the code. It's a shame that this code is so obfuscated as it makes the code more difficult to follow.

The comp class (a struct is a class with all members public by default) operator () simply takes the second item in the pair of pii and compares it against another pii. You would customize this to whatever your type is in your priority queue.

JHagdahl
  • 164
  • 5