创建一个包含对的最小堆<int,custom =“”class =“”>

时间:2017-05-24 01:08:09

标签: c++ algorithm graph priority-queue shortest-path

我正在设计一个Actor和Movie对象的二分图,我想执行djikstra算法,以便找到两个Actor对象之间的最短路径。

我已经设计并构建了我的图表。如果你有兴趣,这是我的设计......

actor.h:

/* (Vertex) Object Class to represent actors */
 class ActorNode {
         friend class Movie;
         friend class ActorGraph;
     public:
         /*Member Variables*/
         std::string name;
         std::set<Movie*> movies;
         /*Constructor*/
         ActorNode(std::string name) : name(name) {}
         /*Destructor*/
         ~ActorNode();

         /*Getters and Setters*/
         std::string getName();
         void setName(std::string actor);

         /*Member Functions*/

 };

movie.h:

 class Movie {
     public:
         friend class ActorNode;
         friend class ActorGraph;

         std::string name;
         int year;
         int weight;
         std::set<ActorNode*> cast;
         /*Constructor*/
         Movie(std::string name, int year) : name(name), year(year), weight(1) {}
         /*Destructor*/
         ~Movie();
         /*Getters and Setters*/
         std::string getMovie();
         void setMovie(std::string movie);

         int getYear();
         void setYear(int yr);

         int getWeight();
         void setWeight(int wt);

         /*Member Functions*/
 };

ActorGraph.h:

  class ActorGraph {
       public:
         unordered_map<std::string,ActorNode*> actorMap;
         unordered_map<std::string,Movie*> movieMap;

         ActorGraph(void);

         bool loadFromFile(const char* in_filename, bool use_weighted_edges);

     };

函数loadFromFile从一个文件中读取,其中每一行都是一个演员和电影,他们与它一起出演了这一年。 只是让我可以更轻松地从文本文件中创建图表。

无论如何,我的问题是试图用我的数据结构来实现djikstra。

我想使用一对整数的优先级队列,ActorNode *,其中int将表示我与源的距离。默认情况下,优先级队列是max_heap,但我想将其设为最小堆。这将在其他一些主题中介绍......

std::priority_queue<int, std::vector<int>, std::greater<int> > my_min_heap;

所以我试着按照这个例子来实现我的目的......

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>,std::greater<int>> min_heap;

但是它说参数2和3是无效的。 有没有办法按我想要的方式建立优先级队列?谢谢!

更新

好的,所以我写了比较课......

/* comparison class */
 class pairCompare{
     public:
         typedef std::pair<int, ActorNode*> p;

         struct compare{
             bool operator()(const p& a, const p& b) {
                 if (a.first > b.first) return true;
                 else return false;
             }
         };

     private:
         std::priority_queue<p,std::vector<std::pair<int,ActorNode*>>,pairCompare> pq; };

但现在得到一个不完整的类型错误...

In file included from /usr/include/c++/5/queue:64:0,
                 from movie.h:6,
                 from actor.h:8,
                 from ActorGraph.h:14,
                 from ActorGraph.cpp:15:
/usr/include/c++/5/bits/stl_queue.h: In instantiation of ‘class std::priority_queue<std::pair<int, ActorNode*>, std::vector<std::pair<int, ActorNode*> >, pairCompare>’:
ActorGraph.h:61:84:   required from here
/usr/include/c++/5/bits/stl_queue.h:391:18: error: ‘std::priority_queue<_Tp, _Sequence, _Compare>::comp’ has incomplete type
       _Compare   comp;

我研究了这个,我找到的解决方案是我应该在我的比较类中转发声明我的ActorNode类,但是没有修复错误。我是否正确创建了比较类?试图让它产生一个最小堆的优先级队列。

其他问题:假设我可以让这个比较类工作......我是否通过创建comparePairs对象来使用成员变量pq?也许只是更容易创建比较类分隔将使用它的pq。实际上创建一个comparePairs对象与仅创建一个优先级队列并使用comparePairs作为第三个参数来进行比较似乎很奇怪。

1 个答案:

答案 0 :(得分:0)

在这一行中,

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>,std::greater<int>> min_heap;

在第二个论点中,

std::vector<std::pair<int,ActorNode*>。你不要关闭角撑。试试这个:

std::priority_queue<std::pair<int,ActorNode*>,std::vector<std::pair<int,ActorNode*>>,std::greater<int>> min_heap;

另一方面,当我以前做过Djikstra或A *时,我发现std::map表现得比std::priority_queue好。虽然可能是我身边的实施问题。如果您担心表现,可能需要查看std::map

相关问题