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() ;
}
}