• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 849
  • Last Modified:

How do I fix this infinite loop in Dijkstra's algorithm?

I have the following cpp file to compute dijkstra algorithm . I get a infinite loop at the condition check while(cur != V)
Please help me resolving  this. The attached dij.in file has the following input format
v v w
where V is # of vertices, E is # of edges, followed by E lines where v are vertex numbers and w is the associated edge weight.
  • 2
1 Solution
>>>>                  if (visited[i] == 0 && priolist[i] < tempmin) {
>>>>                        cur = i;

The cur is changed only wnen a specific condition becomes true. You need a while condition which becomes true for all cases.

for example, define the 'int i = 0;' before the while loop 'while (i <= V)'. And change the for loop to

      for (i = 1; i <= V; i++)
>>>> You need a while condition which becomes true for all cases.
Of course the while condition should become false to prevent from infinite loop. Or, you need a break in the loop.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now