Advertisement
naimularif

Travelling salesman bnb

May 23rd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.02 KB | None | 0 0
  1. #include<iostream>
  2. #include<time.h>
  3. #include<cstdlib>
  4. #include<vector>
  5. #include<fstream>
  6. #define infinity 10000
  7. #define node int
  8. using namespace std;
  9.  
  10. void bnb(int n);
  11. int bnbfirst(void);
  12. int calculateLocalCost(int x,int y);
  13.  
  14. int mat[1000][1000];
  15. int n;
  16. int basic;
  17. int cost=infinity;
  18. vector<node> optimal;
  19.  
  20. int main(void)
  21. {
  22.     ifstream myfile2;
  23.     myfile2.open("input6.txt");
  24.     cout<<"Input the size of graph: "<<endl;
  25.     myfile2>>n;
  26.  
  27.     for(int i=0;i<n;i++)
  28.     {
  29.         for(int j=0;j<n;j++)
  30.         {
  31.             myfile2>>mat[i][j];
  32.         }
  33.     }
  34.  
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         for(int j=0;j<n;j++)
  38.         {
  39.             cout<<mat[i][j]<<'\t';
  40.         }
  41.         cout<<endl;
  42.     }
  43.  
  44.     basic=bnbfirst(); ///calculate initial node cost
  45.     cout<<basic<<endl;
  46.  
  47.     for(int i=0;i<n;i++)
  48.     {
  49.         for(int j=0;j<n;j++)
  50.         {
  51.             cout<<mat[i][j]<<'\t';
  52.         }
  53.         cout<<endl;
  54.     }
  55.  
  56.     optimal.push_back(0);
  57.     bnb(0);
  58.     for(int i=0;i<optimal.size();i++)
  59.     {
  60.         cout<<optimal[i]<<' ';
  61.     }cout<<endl;
  62. }
  63.  
  64. int bnbfirst(void)
  65. {
  66.     int cost=0;
  67.     for(int i=0;i<n;i++)
  68.     {
  69.         int low=infinity;
  70.         for(int j=0;j<n;j++) ///lowest determined
  71.         {
  72.             if(mat[i][j]<low)
  73.             {
  74.                 low=mat[i][j];
  75.             }
  76.         }
  77.         for(int j=0;j<n;j++) ///low is subtracted from all elements of the row
  78.         {
  79.             if(mat[i][j]==infinity);
  80.             else
  81.             {
  82.                 mat[i][j]-=low;
  83.             }
  84.         }
  85.         cost+=low;
  86.     }
  87.  
  88.     for(int j=0;j<n;j++)
  89.     {
  90.         int low=infinity;
  91.         for(int i=0;i<n;i++) ///lowest determined
  92.         {
  93.             if(mat[i][j]<low)
  94.             {
  95.                 low=mat[i][j];
  96.             }
  97.         }
  98.         for(int i=0;i<n;i++) ///low is subtracted from all elements of the column
  99.         {
  100.             if(mat[i][j]==infinity);
  101.             else
  102.             {
  103.                 mat[i][j]-=low;
  104.             }
  105.         }
  106.         cost+=low;
  107.     }
  108.     return cost;
  109. }
  110.  
  111. void bnb(int x)
  112. {
  113.     int localcost=infinity,next;
  114.     for(int i=0;i<n;i++)
  115.     {
  116.         int flag=0;
  117.         for(int j=0;j<optimal.size();j++) ///check if the node is already in the path
  118.         {
  119.             if(i==optimal[j])
  120.             {
  121.                 flag=1;
  122.                 break;
  123.             }
  124.         }
  125.         if(flag==0)
  126.         {int cc=calculateLocalCost(x,i);
  127.         cout<<"cc "<<cc<<endl;
  128.             if(localcost>cc)
  129.             {
  130.                 localcost=cc;
  131.                 cout<<"local "<<localcost<<endl;
  132.                 next=i;
  133.                 cout<<"next "<<next<<endl;
  134.             }
  135.         }
  136.     }
  137.     basic+=localcost;
  138.     cout<<"basic "<<basic<<endl;
  139.     optimal.push_back(next);
  140.  
  141.     cout<<"next push to vector "<<next<<endl;
  142.  
  143.     for(int i=0;i<n;i++) ///row of x turned infinity
  144.     {
  145.         mat[x][i]=infinity;
  146.     }
  147.     for(int i=0;i<n;i++) ///column of next turned infinity
  148.     {
  149.         mat[i][next]=infinity;
  150.     }
  151.     mat[x][next]=mat[next][x]=infinity;
  152.  
  153.     int low=infinity;
  154.     for(int i=0;i<n;i++)
  155.     {
  156.         if(low>mat[0][i])
  157.         {
  158.             low=mat[0][i];
  159.         }
  160.     }
  161.     for(int i=0;i<n;i++)
  162.     {
  163.         mat[0][i]-=low;
  164.     }
  165.     cout<<"next "<<next<<endl;
  166.     if(optimal.size()==n) return;
  167.     bnb(next);
  168. }
  169.  
  170. int calculateLocalCost(int x,int y)
  171. {
  172.     int temp[n][n];
  173.     int low=infinity;
  174.     for(int i=0;i<n;i++)
  175.     {
  176.         for(int j=0;j<n;j++)
  177.         {
  178.             temp[i][j]=mat[i][j];
  179.         }
  180.     }
  181. //
  182. //    cout<<"<<inside>>\n";
  183. //    cout<<x<<' '<<y<<endl;
  184. //    for(int i=0;i<n;i++)
  185. //    {
  186. //        for(int j=0;j<n;j++)
  187. //        {
  188. //            cout<<mat[i][j]<<'\t';
  189. //        }cout<<endl;
  190. //    }
  191. //    cout<<"<<inside>>\n";
  192.  
  193.     int localcost=temp[x][y];
  194.     for(int i=0;i<n;i++)
  195.     {
  196.         if(temp[x][i]<low)
  197.         {
  198.             low=temp[x][i];
  199.         }
  200.     }
  201.     localcost+=low;
  202.     return localcost;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement