Advertisement
cacodemon665

Branch&Bound

Dec 4th, 2019
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <ctime>
  4.  
  5. typedef std::vector<std::vector<int>> Matrix;
  6.  
  7. void branch_and_bound_helper(const Matrix& distances, std::vector<int>& path, std::vector<bool>& visited, int distance, int& lower_bound, std::vector<int>& final_path)
  8. {
  9.     const auto depth = path.size();
  10.     const auto last_node = path[depth - 1];
  11.  
  12.     if (depth == distances.size())
  13.     {
  14.         lower_bound = distance;
  15.         final_path = path;
  16.         //std::cout << distance << std::endl;
  17.     }
  18.    
  19.     for (size_t i = 0; i < distances.size(); i++)
  20.     {
  21.         if (visited[i])
  22.             continue;
  23.  
  24.         auto temp_distance = distance + distances[last_node][i];
  25.  
  26.         if (depth + 1 == distances.size())
  27.             temp_distance += distances[i][path[0]];
  28.  
  29.         if (lower_bound > temp_distance)
  30.         {
  31.             visited[i] = true;
  32.             path.push_back(i);
  33.  
  34.             branch_and_bound_helper(distances, path, visited, temp_distance, lower_bound, final_path);
  35.            
  36.             visited[i] = false;
  37.             path.pop_back();
  38.         }
  39.     }
  40. }
  41.  
  42. std::pair<int, std::vector<int>> branch_and_bound(const Matrix& distances)
  43. {
  44.     auto path = std::vector<int>(1, 0);
  45.     path.reserve(distances.size());
  46.  
  47.     auto visited = std::vector<bool>(distances.size(), false);
  48.     visited[0] = true;
  49.    
  50.     int lower_bound = int(1e9);
  51.    
  52.     std::vector<int> final_path;
  53.    
  54.     branch_and_bound_helper(distances, path, visited, 0, lower_bound, final_path);
  55.  
  56.     return std::make_pair(lower_bound, final_path);
  57. }
  58.  
  59.  
  60. int main()
  61. {
  62.     Matrix distances = { {0,56,131,85,123,154,156,198,218,169,235,262,209,262,272,141},
  63.         {56,0,119,65,90,123,149,186,198,150,192,219,157,209,220,87},
  64.         {131,119,0,50,88,81,30,67,115,80,139,184,176,224,239,132},
  65.         {85,65,50,0,40,71,80,124,166,86,151,179,70,122,132,82},
  66.         {123,90,88,40,0,31,106,137,140,77,106,135,112,164,174,45},
  67.         {154,123,81,71,31,0,87,118,103,54,76,101,95,144,54,133},
  68.         {156,149,30,80,106,87,0,35,102,62,120,169,181,230,244,151},
  69.         {198,186,67,124,137,118,35,0,73,73,132,182,212,248,275,182},
  70.         {218,198,115,166,140,103,102,73,0,49,68,147,197,222,260,171},
  71.         {169,150,80,86,77,54,62,73,49,0,54,109,149,175,212,132},
  72.         {235,192,139,151,106,76,120,132,68,54,0,73,166,137,228,149},
  73.         {262,219,184,179,135,101,169,182,147,109,73,0,93,67,156,143},
  74.         {209,157,176,70,112,95,181,212,197,149,166,93,0,57,63,70},
  75.         {262,209,224,122,164,144,230,248,222,175,137,67,57,0,49,122},
  76.         {272,220,239,132,174,54,244,275,260,212,228,156,63,49,0,132},
  77.         {141,87,132,82,45,133,151,182,171,132,149,143,70,122,132,0}
  78.     };
  79.     unsigned int start_time = clock();
  80.     auto result = branch_and_bound(distances);
  81.    
  82.     std::cout << "Min distance: " << result.first << std::endl;
  83.     std::cout << "Path: ";
  84.  
  85.     for (auto v : result.second)
  86.     {
  87.         std::cout << v << " ";
  88.     }
  89.  
  90.     std::cout << std::endl;
  91.  
  92.     unsigned int end_time = clock(); // конечное время
  93.     unsigned int search_time = end_time - start_time; // искомое время
  94.  
  95.     std::cout << "Elapsed time:" << search_time / 1000.f;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement