Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:58:32+00:00 2026-05-16T22:58:32+00:00

I coded a greedy search algorithm , but it goes to an infinite loop

  • 0

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 !

    while(!f1.eof() )
        {
            f1>>temp1>>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>>temp1>>temp2>>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<int>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<A[start].loc.size();u++)
                    {
                        l=A[start].loc[u];
                        if(A[l].time_taken < 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<<A[l].loc_name<<min<<endl;
                }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:58:32+00:00Added an answer on May 16, 2026 at 10:58 pm
    int algo(int value, int stopValue, list<int> & answer)
    {
        if(value == stopValue)
        {
            return 0;
        }
        if(value > stopValue)
        {
            return 1;
        }
    
        if(algo(value+2, stopValue) == 0)
        {
            answer.push_back(2);
            return 0;
        }
        if(algo(value+1, stopValue) == 0)
        {
            answer.push_back(1);
            return 0;
        }
        return 1;
    }
    

    Here’s a trivial greedy recursive algorithm that finds the number of 2s and 1s a number is composed of. Greedy algorithms are only good for some problems and produce suboptimal results in most.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is coded in pseudo-PHP, but I really don't mind what language I
I coded something using Date statement in Access VBA. It was working fine until
I coded up 4 .java files. The thing is that I can only execute
I have never hand-coded object creation code for SQL Server and foreign key decleration
I have custom coded several enterprise applications for mid to large organizations to use
I've coded some smaller projects using Silverlight 2.0, which is fairly impressive. Does Silverlight
A designer coded an html template using CSS best practices. I want to use
I have coded some JavaScript to perform an ajax call in an asp.net application.
I have just coded up a web based flv file download utility. When I
I've coded a program in C that sends messages to the stdout using printf

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.