0

This is the two classes I have

    class Graph
    {
        private:
            unordered_map<int, Vertex> _vertices;
    };

    class Vertex
    {
        private: 
            unordered_map<Vertex *, int> _edges;//key: pointer to a vertex, data: edge weight from this vertex to a certain vertex
            int _load_factor = 0;
            int _id = 0;
    };

If I want to use Dijkstra's algorithm to compute the shortest path from a certain pass, I need to use a priority queue. I have no Idea how can I write a comparator class for:

priority_queue<Vertex, vector<Vertex>, PathWeightComparer> dijkstra_queue{};

This is what I found online:

class PathWeightComparer
{
public:
    bool operator()(Vertex lhs, Vertex rhs)
    {
        return (lhs.getPathWeight() > rhs.getPathWeight());

    }
};

What is lhs and rhs refer to? Is it the vertices from the priority_queue? If so to get a path weight for this comparison, I need to know where from to factors. Can I just create a new variable name _path_weight and only use it for findShortestPath()? The main thing I don't understand is the notion of lhs and rhs, what are they? One more thing, my current comparator class will result in max heap or min heap?

Thank you so much

1 Answers1

0

The point of std::priority_queue is to give you a constant access time of the element of the highest priority that is chosen by the comparator function (the largest, the smallest, etc.).

What is lhs and rhs refer to?

That's how you compare. Two elements are passed to the comparator function to determine which one is "superior" over the other (or has greater priority). Comparison is what we call a binary operation (two operands), right?

my current comparator class will result in max heap or min heap?

You're comparing with >, thereby max heap.

Lastly, as I commented, the signature should be:

bool operator()(Vertex const& lhs, Vertex const& rhs);
LogicStuff
  • 19,397
  • 6
  • 54
  • 74