What is an efficient way of back-tracking in greedy best search algorithm?
<p>I coded a greedy search algorithm , but it goes to an infinite loop as its not able to back - track , and reach the final state . How can a back-tracking be done in C++ in the most optimal way ! </p>

<pre><code>    while(!f1.eof() )
        {
            f1&gt;&gt;temp1&gt;&gt;time;

            A[i].loc_name=temp1;
            A[i].time_taken=time;

                initial_start=1; // make initial to 1 and final to i

                final_finish=i;

        i=i+1;
        }

    while(!f2.eof())
        {
            f2&gt;&gt;temp1&gt;&gt;temp2&gt;&gt;time;
            int ci=0;
            while( ci != i )
                {

                    if (temp1 == A[ci].loc_name)
                        {
                            flag1=ci;
                            tf1=time;

                        }
                    if (temp2 == A[ci].loc_name)
                        {
                            flag2=ci;
                            tf2=time;
                        }
                    ci=ci+1;
                }



            A[flag1].loc.push_back(flag2);
            A[flag1].time_live.push_back(time);

            A[flag2].loc.push_back(flag1);
            A[flag2].time_live.push_back(time);         

        }


//Greedy Search algorithm   
                int min,min_time,finish,start,l;
                vector&lt;int&gt;path;
                min_time=99999;
                l=0;
                finish=1;
                start=0;
//Choosing the node with the minimum value 
            while(finish != final_finish)
                {

                for(int u=0;u&lt;A[start].loc.size();u++)
                    {
                        l=A[start].loc[u];
                        if(A[l].time_taken &lt; min_time)
                            {   
                                min_time=A[l].time_taken;
                                finish = l;

                            }

                    }
                min=min+min_time;
                path.push_back(start);
                    start=finish;   
//Printing the path found by Greedy search into an output file
            f3&lt;&lt;A[l].loc_name&lt;&lt;min&lt;&lt;endl;
                }
</code></pre>
