Advertisement
naimularif

Travelling salesman bnd do not work properly

May 22nd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 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();
  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.     cout<<basic<<endl;;
  56.     for(int i=0;i<optimal.size();i++)
  57.     {
  58.         cout<<optimal[i]<<' ';
  59.     }cout<<endl;
  60. }
  61.  
  62. int bnbfirst(void)
  63. {
  64.     int cost=0;
  65.     for(int i=0;i<n;i++)
  66.     {
  67.         int low=infinity;
  68.         for(int j=0;j<n;j++) ///lowest determined
  69.         {
  70.             if(mat[i][j]<low)
  71.             {
  72.                 low=mat[i][j];
  73.             }
  74.         }
  75.         for(int j=0;j<n;j++) ///low is subtracted from all elements of the row
  76.         {
  77.             if(mat[i][j]==infinity);
  78.             else
  79.             {
  80.                 mat[i][j]-=low;
  81.             }
  82.         }
  83.         cost+=low;
  84.     }
  85.  
  86.     for(int j=0;j<n;j++)
  87.     {
  88.         int low=infinity;
  89.         for(int i=0;i<n;i++) ///lowest determined
  90.         {
  91.             if(mat[i][j]<low)
  92.             {
  93.                 low=mat[i][j];
  94.             }
  95.         }
  96.         for(int i=0;i<n;i++) ///low is subtracted from all elements of the column
  97.         {
  98.             if(mat[i][j]==infinity);
  99.             else
  100.             {
  101.                 mat[i][j]-=low;
  102.             }
  103.         }
  104.         cost+=low;
  105.         optimal.push_back(0);
  106.     }
  107.     return cost;
  108. }
  109.  
  110. void bnb(int x)
  111. {
  112.     int localcost=infinity,next;
  113.     for(int i=0;i<n;i++)
  114.     {
  115.         int flag=0;
  116.         for(int j=0;j<optimal.size();j++)
  117.         {
  118.             if(i==optimal[j])
  119.             {
  120.                 flag=1;
  121.                 break;
  122.             }
  123.         }
  124.         if(flag==0)
  125.         {
  126.             if(localcost>calculateLocalCost(x,i))
  127.             {
  128.                 localcost=calculateLocalCost(x,i);
  129.                 next=i;
  130.             }
  131.         }
  132.     }
  133.     basic+=localcost;
  134.     optimal.push_back(next);
  135.  
  136.     for(int i=0;i<n;i++)
  137.     {
  138.         mat[x][i]=infinity;
  139.     }
  140.     for(int i=0;i<n;i++)
  141.     {
  142.         mat[i][next]=infinity;
  143.     }
  144.     mat[x][next]=mat[next][x]=infinity;
  145.  
  146.     int low=infinity;
  147.     for(int i=0;i<n;i++)
  148.     {
  149.         if(low>mat[0][i])
  150.         {
  151.             low=mat[0][i];
  152.         }
  153.     }
  154.     for(int i=0;i<n;i++)
  155.     {
  156.         mat[0][i]-=low;
  157.     }
  158.  
  159.     bnb(next);
  160. }
  161.  
  162. int calculateLocalCost(int x,int y)
  163. {
  164.     int temp[n][n];
  165.     int low=infinity;
  166.     for(int i=0;i<n;i++)
  167.     {
  168.         for(int j=0;j<n;j++)
  169.         {
  170.             temp[i][j]=mat[i][j];
  171.         }
  172.     }
  173.  
  174.     int localcost=temp[x][y];
  175.     for(int i=0;i<n;i++)
  176.     {
  177.         if(temp[x][i]<low)
  178.         {
  179.             low=temp[x][i];
  180.         }
  181.     }
  182.     localcost+=low;
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement