Priority queue

I was working through Dijkstra's Shortest Path algorithm in which priority queue is used. In that a comparison Function object is used. I want to know if the ordering ensures min priority of the priority queue. If so, isn't it suppose to return true for maintaining the weak ordering(lowest value at the top of priority queue)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
struct Edge {
    int tail;
    int head;
    double cost;
};

...
...

 class edgeSort {
    bool reverse;
public:
    edgeSort(const bool& revparam = false){
        reverse = revparam;
    }
    inline bool operator () ( Edge &lhs, Edge &rhs) const { 
        return reverse ? (lhs.cost < rhs.cost) : (lhs.cost > rhs.cost); 
    }
};


double Graph::shortestPath(int num){

    std::vector<double> distance; //stores distance of each vertex from source
    std::vector<bool> visited;//stores information if a particular vertex is visited from source vertex
    std::priority_queue<Edge,std::vector<Edge>,edgeSort> myPQ (edgeSort(false)); 
    
Last edited on
Any inputs on this is highly appreciated
Looks fine.

Consider:

1
2
3
4
5
6
7
8
9
template < bool MAXHEAP = false > struct edge_compare
{
    inline bool operator() ( const edge& lhs, const edge& rhs ) const { return lhs.cost > rhs.cost ; }
};

template <> struct edge_compare<true>
{
    inline bool operator() ( const edge& lhs, const edge& rhs ) const { return lhs.cost < rhs.cost ; }
};


And then: std::priority_queue< edge, std::vector<edge>, edge_compare<false> > myPQ ;
Topic archived. No new replies allowed.