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

Posted on 2008-11-09
Last Modified: 2012-05-05
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 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.
Question by:Beebutter
    LVL 39

    Expert Comment

    >>>>                  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++)
    LVL 39

    Accepted Solution

    >>>> 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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

      Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
    IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now