0

I realized I can't post answers to my own questions because of my low rep or whatever so i deleted my old question and am reasking it. i changed some things and still can't get what i'm looking for.

Here is most of the code I left out some of the simpler implementations such as parts of the pathFinder class because I know for sure they work, which is why you'll see playerVertex and time just randomly there. In the example they used a decreaseKey function, I'm not sure if THAT'S what I'm missing? I'm a beginner here, so constructive criticism is welcome. (hopefully as polite as possible) lol. My problem is printing the path, I get a looop of the same two values over and over again.

class Heap 
{
public: Heap();
    ~Heap();
    void insert(double element);
    double  deletemin();
    void print();
    int size(){return heap.size();}


private:
 int currentIndex;
 int left(int parent);
 int right(int parent);
 int parent(int child);
 void heapifyup(int index);
 void heapifydown(int index);
private:
 vector<double> heap;
};

Heap::Heap()
{
 currentIndex = 0;
}
Heap::~Heap()
{}

void Heap::insert(double element)
{
heap.push_back(element);
currentIndex++;
heapifyup(heap.size() - 1);
}

double Heap::deletemin()
{
double min = heap.front();
heap[0] = heap.at(heap.size()-1);
heap.pop_back();
heapifydown(0);
currentIndex--;
return min;
}
void Heap::print()
{
vector<double>::iterator pos = heap.begin();
cout << "Heap = ";
while ( pos != heap.end() ) 
{
    cout << *pos;
    ++pos;
    cout << endl;
 }
}
void Heap::heapifyup(int index)
{


while((index>0) && (parent(index) >=0) && (heap[parent(index)] > heap[index]))
{
 double tmp = heap[parent(index)];
 heap[parent(index)] = heap[index];
 heap[index] = tmp;
 index = parent(index);


}
}

void Heap::heapifydown(int index)
{



int child = left(index);

if((child > 0) && (right(index) > 0) && (heap[child]>heap[right(index)]))
{
 child = right(index);

}
if(child > 0)
{
double tmp = heap[index];
heap[index] = heap[child];
heap[child] = tmp;
heapifydown(child);
}
}

int Heap::left(int parent)
{
int i = ( parent <<1) + 1; 
return(i<heap.size()) ? i : - 1;
}

int Heap::right(int parent)
{
int i = ( parent <<1) + 2; 
return(i<heap.size()) ? i : - 1;
}

int Heap::parent(int child)
{
if(child != 0)
{
 int i = (child - 1) >>1;
 return i;
}
return -1;
}



class pathFinder : public weightedGraph
{

private:

vertex* playerVertex;
double time;


public:
string source;
pathFinder()
{
    playerVertex = NULL;
    time = 0;

}


  void Dijkstra(int s,int t)
{
    vertex *verts = findVertex(grid[s][t]);
    Heap H;
    for each(vertex *v in vertexList)
    {

        if(v->data == verts->data)
        {
            verts->distance = 0;
            verts->pred = NULL;
        }
        v->distance = INFINITY;
        v->pred = NULL;
        H.insert(v->data);
    }
    while(H.size() != 0)
    {

        vertex *x = findVertex(H.deletemin());

        for each(edge *v in x->adjacencyList)
        {

            if(v->end->visited != true)
            {    
            relax(x,v->end);
            v->end->visited = true;
            }
            else
                break;

        }

    }
}








void relax(vertex *a, vertex *b)
{

    if(a->distance + weightFrom(a,b) > b->distance)
        {
            b->distance = a->distance + weightFrom(a,b);
            b->pred = a;
        }


}


void printPath(double dest,double dest1)
{
    vertex *verta = findVertex(dest);
    while(verta->pred->data != dest1)
    {
    cout<<verta->data<<endl;
    verta = verta->pred;
    }
}

and i'm not sure about the print path being that. i just used the print path from the BFS algorithm i've implemented before.

user2283704
  • 113
  • 1
  • 1
  • 6
  • just need anything please! – user2283704 Apr 15 '13 at 23:04
  • 2
    `for each` is not standard c++, is there a macro `each` defined somewhere? – David Brown Apr 15 '13 at 23:12
  • 1
    @DavidBrown I don't think so. It must be a part of my Visual Studio, a compiler extension or something. – user2283704 Apr 15 '13 at 23:15
  • @DavidBrown The OP is right, it's [specific to the MSVC compiler](http://stackoverflow.com/questions/197375/visual-c-for-each-portability). I recommend using [BOOST_FOREACH](http://www.boost.org/doc/libs/1_53_0/doc/html/foreach.html) instead for better portability, or if you are using C++11 (and only need your code to be portable to other C++11 compilers), then [use this](http://stackoverflow.com/questions/6963894/c11-how-to-use-range-based-for-loop-with-stdmap). – JBentley Apr 15 '13 at 23:59
  • I don't think you're ensuring the heap condition is being maintained. After you relax the nodes, you need to ensure the heap is re-ordered to respect the new costs. Not doing that probably means that you are generating loops in your path. – Keith Apr 16 '13 at 02:48

1 Answers1

1

Where in your printPath function are you looking for the end of the list?

You keep going verta = verta->pred until the data is not equal to some value.

By the way, don't compare doubles for equality, as it ain't going to happen. See What Every Computer Scientist Should Know About Floating Point.

What happens when you single step with your debugger?
(Try drawing the links and how you traverse them.)

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • yea like i said i used the printPath function from BFS so i'm not sure if it's right lol. i'm trying to do Dijkstra's on let's say [0][0] in a grid. then i do print path from [3][3] going from pred to pred until it reaches [0][0]. this is the BFS print out. i'm not sure if that's how you print out in Dijkstra's. – user2283704 Apr 15 '13 at 23:52