So this is driving me crazy. I'm coding the Dijskra algorithm to find the shortest path on a graph from a vertix v to a vertix nv and It's just not working, I hope someone can explain to me whats wrong and guide me on how to solve it. I know where the error is originating but i can't think of how to fix it.
This is the function that returns a pointer to an object of type Vertex.
Vertex* Dijkstra(Graph& g, int v, int nv)
{
if(!g.is_in(v)) throw 2; //Check if v is inside the graph
if(!g.is_in(nv)) throw 2;
int i=0,vlabel=0;
int ulabel=9999;
Vertex* holder =new Vertex(v); //Holder will be returned and will contain the shortests path
PriorityQueue<Vertex*> pq; //Priority Queue
while(i<g.get_list().size()) //Fill PQ with Vertices in g
{
if(g.get_vertex(i)->getID() == v) pq.insertItem(vlabel,g.get_vertex(i)); //When v is found insert in pq with key =0
else pq.insertItem(ulabel,g.get_vertex(i));
i++;
}
while(!pq.isEmpty())
{
Vertex* cloud = pq.min_element(); //Set a vertex cloud to hold vertex with key 0
vlabel = pq.min_key(); //Set label to key
pq.remove_min();
for(int m = 0; m < cloud->getOutEdges().size(); m++) //While we havent looked at all the outward edges of cloud.
{
int templ = pq.find(cloud->get_out(m)->geteVertP());
int tempk = pq.get_key(pq.find(cloud->get_out(m)->geteVertP()));
if(cloud->get_out(m)->geteVertP()->getID() == nv ) // if we find nv is an outward edge, get its weight, insert it
{ // to the outward edges vector of cloud and return
Edge* edge = new Edge(holder,pq.min_element(),pq.min_key());
holder->getOutEdges().push_back(edge);
return holder;
}
elseif(vlabel + cloud->get_out(m)->getWeight() < tempk)
{
pq.decrease_key(templ, tempk-cloud->get_out(m)->getWeight());
}
}
Edge* edge = new Edge(holder,pq.min_element(),pq.min_key());
holder->getOutEdges().push_back(edge);
}
return holder;
}
Now i know the error originates at this point
cloud->getOutEdges().size()
Also, the error that im getting is an Access Violation reading location.
Could it have something to do with the priority Queue i'm using? Or is my syntax just wrong?
> Or is my syntax just wrong?
Then it would not compile.
However, things like if(cloud->get_out(m)->geteVertP()->getID() == nv ) obfuscate the code.
By the way
_ if we find nv is an outward edge,
___ get its weight,
___ insert it to the outward edges vector of cloud
_ and return
is incorrect. You found one path, no the minimum path
I see what you mean on the path statement. I didn't think of it like that.
And I though about that, so I tested it using this
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int main()
{
PriorityQueue<Vertex*> pq;
int x =32;
Vertex* cloud= new Vertex(x);
pq.insertItem(x,cloud);
Vertex* c = pq.min_element();
cout << c->getID();
{
I still get the same error with this. Any idea what causes this? Would it help to look at the PriorityQueue class, or at least the functions that it uses in this case?
Just to clarify: The Vertex class takes an int which becomes the vertex id
and getID() just returns the id.
After filling some holes to make your code compile, I did not observe the crash.
However, ElemType& UnsortedArray<ElemType, Comp>::min() returns a reference to a temporary.
1 2 3 4 5 6 7 8 9 10 11
int main()
{
PriorityQueue<Vertex*> pq;
int x =32;
Vertex* cloud= new Vertex(x);
pq.insertItem(x,cloud);
Vertex* c = pq.min_element();
cout << c->getID();
}
`c' should be equal to `cloud', do a step-by-step run and find the error.
That is curious, When i ran it several times before it gave the error, but now its not... I'm just confused as to what happened because i did not change anything mayor at all;
Anyways I'm now having another error with this function:
1 2 3 4 5
int find(ElemType& e)
{
return T.find_p(Item(0,e));
}
I suppose that the problem is with const correctness.
You can't have a reference to a temporary, as you intend with T.find_p(Item(0,e));
Pass it by value, or by const-reference