VHISTlib  1.84.0.3018
/daten/ahuesgen/projects/vhist/code/vhistlib/include/vhistdifflib/dijkstra.h
00001 /* $HEADERS
00002  $Id: dijkstra.h 2179 2011-06-24 09:03:24Z ahuesgen $
00003  */
00004 
00005 #ifndef DIJKSTRA_H
00006 #define DIJKSTRA_H
00007 
00008 #include <QSharedPointer>
00009 #include <QList>
00010 
00011 #include "../shared/misc_defines.h"
00012 
00013 #include "priorityqueue.h"
00014 #include <QtDebug>
00015 
00016 
00017 namespace vhist
00018 {
00019 
00020 // T = Node/Path Type. Must provide methods:
00021 //   bool reachedGoal() // true if node is a goal node
00022 //   void expand(PriorityQueue<QSharedPointer<T> >& m_queue) // expands the node
00023 template<class T>
00024 class VHIST_EXTERN Dijkstra
00025 {
00026     private:
00027         PriorityQueue<QSharedPointer<T> > m_queue;
00028 
00029     public:
00030         Dijkstra();
00031         ~Dijkstra();
00032 
00033     public:
00034         QSharedPointer<T> findPath(QSharedPointer<T> startNode);
00035         QList<QSharedPointer<T> > findPaths(QSharedPointer<T> startNode,
00036                 int numPaths);
00037 };
00038 
00039 } // namespace vhist
00040 
00041 
00042 
00043 template<class T>
00044 vhist::Dijkstra<T>::Dijkstra()
00045 {
00046 }
00047 
00048 
00049 template<class T>
00050 vhist::Dijkstra<T>::~Dijkstra()
00051 {
00052 }
00053 
00054 
00055 template<class T>
00056 QSharedPointer<T> vhist::Dijkstra<T>::findPath(QSharedPointer<T> startNode)
00057 {
00058     QList<QSharedPointer<T> > goals = findPaths(startNode, 1);
00059     if (goals.size() > 0)
00060         return goals.at(0);
00061     else
00062         return QSharedPointer<T>(NULL);
00063 }
00064 
00065 
00066 template<class T>
00067 QList<QSharedPointer<T> > vhist::Dijkstra<T>::findPaths(
00068         QSharedPointer<T> startNode, int numPaths)
00069 {
00070     QList<QSharedPointer<T> > goals;
00071 
00072     m_queue.push(startNode, 0);
00073     int i = 0;
00074     while (!m_queue.isEmpty())
00075     {
00076         ++i;
00077         QSharedPointer<T> curNode = m_queue.pop();
00078         if (curNode->reachedGoal())
00079         {
00080             qDebug() << "FINISHED";
00081             qDebug() << i << "nodes expanded";
00082             curNode->printMe();
00083 
00084             goals.append(curNode);
00085             if (goals.size() >= numPaths)
00086                 return goals;
00087         }
00088         else
00089             curNode->expand(m_queue);
00090     }
00091 
00092     return goals;
00093 }
00094 
00095 #endif // DIJKSTRA_H

VHISTlib 1.84.0.3018 of Jun 28 2013, generated by doxygen.