You are using a version without Ads of this website. Please, consider donating:

### Dijkstra's algorithm issue

After numerous Rewrites, and many different versions of this, I am stumped and frustrated.

It won't continue onto the next node in the sequence, it just stays at the start (0).

prevNode holds the weight, current node index, previous node index, and whether it's been visited or not.

Any help?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273`` ``````prevNode* graph::search(int start, int **adjmat) // using dijkstra's algorithm { // setup prevNode *ret; ret = new prevNode[size]; // create node data for( int i = 0; i < size; ++i) { ret[i].weight = 1.0/0.0; ret[i].self = i; ret[i].prev = -1; ret[i].visited = false; } ret[start].weight = 0; bool cont = true; int u = 0; // until set is empty, run while(cont) { // pull out the closest unvisited node for( int i = 0; i < size; ++i) { //std::cout << i << ' ' << u << std::endl; if( ret[u].visited) { u = i; //std::cout << "change in u" << std::endl; } else if( ret[u].weight > ret[i].weight) { u = i; std::cout << "change in u" << std::endl; } } // if the next closest to the begining is infinity break if( ret[u].weight == 1.0/0.0) return ret; for( int v = 0; v < size; ++v) { if( adjmat[u][v] != 0) { double alt = ret[u].weight + adjmat[u][v]; //std::cout << alt << std::endl; if( alt < ret[v].weight) { //std::cout << "change" << alt << ":" << ret[v].weight << std::endl; ret[v].weight = alt; ret[v].prev = u; //std::cout << alt << ":" << ret[v].weight << std::endl; } } } // check for unvisited nodes cont = false; for( int i = 0; i < size; ++i) { if( !ret[i].visited) cont = true; } } return ret; }``````
Last edited on
¿where do you visit your nodes?
I visit it when it is the node I'm connecting from, when it is u, and I just realized the problem. I feel stupid.
I never included the

`ret[u].visited = true;`

, huff.

Thank you. );
Topic archived. No new replies allowed.