0

I used the nearest neighbor algorithm to solve traveling sales man problem but it provides a sub-optimal tour . I tried to implement the 2-opt algorithm but i was lost , i hope some one can help to implement that algorithm . Here is the nearest neighbor algorithm that i implemented

void matrix::algo()
{
    deque <float> temp ;
    vector <float> v ;
    for (int i=1 ; i<n ; i++)
    {
        v.push_back(i) ;
    }
    int u=0 ;
    int k=0 ;
    temp.push_back(u) ;
    while (!v.empty())
    {
        float minm=5000000 ;

        for (int i=0 ; i<n ; i++)
        {

            if(find(temp.begin(), temp.end(), i)==temp.end())
            {
                if (mat[u][i]<=minm)
                {
                    minm=mat[u][i] ;
                    k=i ;

                }
            }
        }
        temp.push_back(k);
        u=k ;
        v.erase(remove(v.begin(), v.end(), u), v.end());
    }

    temp.push_back(0) ;


    while (! temp.empty())
    {
        cout<<temp.front()<<"->" ;

        temp.pop_front() ;
    }

}
Komp
  • 1
  • 1
  • Is there an actual question, or are you looking for reference-implementation of 2-opt? Right now, there is a high chance that there want be any answers or the question is closed. – Jens Aug 23 '15 at 20:57
  • as i said i need help to implement the 2-opt algorithm , so a good reference would be great – Komp Aug 23 '15 at 21:15

0 Answers0