Advertisement
Guest User

bnb

a guest
Nov 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <utility>
  5. #include <cstring>
  6. #include <climits>
  7. using namespace std;
  8.  
  9. // N is number of total nodes on the graph or the cities in the map
  10. #define N 5
  11.  
  12. // Sentinal value for representing infinity
  13. #define INF INT_MAX
  14.  
  15. // State Space Tree nodes
  16. struct Node
  17. {
  18.     // stores edges of state space tree
  19.     // helps in tracing path when answer is found
  20.     vector<pair<int, int>> path;
  21.  
  22.     // stores the reduced matrix
  23.     int reducedMatrix[N][N];
  24.  
  25.     // stores the lower bound
  26.     int cost;
  27.  
  28.     //stores current city number
  29.     int vertex;
  30.  
  31.     // stores number of cities visited so far
  32.     int level;
  33. };
  34.  
  35. // Function to allocate a new node (i, j) corresponds to visiting
  36. // city j from city i
  37. Node* newNode(int parentMatrix[N][N], vector<pair<int, int>> const &path,
  38.             int level, int i, int j)
  39. {
  40.     Node* node = new Node;
  41.  
  42.     // stores ancestors edges of state space tree
  43.     node->path = path;
  44.     // skip for root node
  45.     if (level != 0)
  46.         // add current edge to path
  47.         node->path.push_back(make_pair(i, j));
  48.  
  49.     // copy data from parent node to current node
  50.     memcpy(node->reducedMatrix, parentMatrix,
  51.         sizeof node->reducedMatrix);
  52.  
  53.     // Change all entries of row i and column j to infinity
  54.     // skip for root node
  55.     for (int k = 0; level != 0 && k < N; k++)
  56.     {
  57.         // set outgoing edges for city i to infinity
  58.         node->reducedMatrix[i][k] = INF;
  59.  
  60.         // set incoming edges to city j to infinity
  61.         node->reducedMatrix[k][j] = INF;
  62.     }
  63.  
  64.     // Set (j, 0) to infinity
  65.     // here start node is 0
  66.     node->reducedMatrix[j][0] = INF;
  67.  
  68.     // set number of cities visited so far
  69.     node->level = level;
  70.  
  71.     // assign current city number
  72.     node->vertex = j;
  73.  
  74.     // return node
  75.     return node;
  76. }
  77.  
  78. // Function to reduce each row in such a way that
  79. // there must be at least one zero in each row
  80. int rowReduction(int reducedMatrix[N][N], int row[N])
  81. {
  82.     // initialize row array to INF
  83.     fill_n(row, N, INF);
  84.  
  85.     // row[i] contains minimum in row i
  86.     for (int i = 0; i < N; i++)
  87.         for (int j = 0; j < N; j++)
  88.             if (reducedMatrix[i][j] < row[i])
  89.                 row[i] = reducedMatrix[i][j];
  90.  
  91.     // reduce the minimum value from each element in each row
  92.     for (int i = 0; i < N; i++)
  93.         for (int j = 0; j < N; j++)
  94.             if (reducedMatrix[i][j] != INF && row[i] != INF)
  95.                 reducedMatrix[i][j] -= row[i];
  96. }
  97.  
  98. // Function to reduce each column in such a way that
  99. // there must be at least one zero in each column
  100. int columnReduction(int reducedMatrix[N][N], int col[N])
  101. {
  102.     // initialize col array to INF
  103.     fill_n(col, N, INF);
  104.  
  105.     // col[j] contains minimum in col j
  106.     for (int i = 0; i < N; i++)
  107.         for (int j = 0; j < N; j++)
  108.             if (reducedMatrix[i][j] < col[j])
  109.                 col[j] = reducedMatrix[i][j];
  110.  
  111.     // reduce the minimum value from each element in each column
  112.     for (int i = 0; i < N; i++)
  113.         for (int j = 0; j < N; j++)
  114.             if (reducedMatrix[i][j] != INF && col[j] != INF)
  115.                 reducedMatrix[i][j] -= col[j];
  116. }
  117.  
  118. // Function to get the lower bound on
  119. // on the path starting at current min node
  120. int calculateCost(int reducedMatrix[N][N])
  121. {
  122.     // initialize cost to 0
  123.     int cost = 0;
  124.  
  125.     // Row Reduction
  126.     int row[N];
  127.     rowReduction(reducedMatrix, row);
  128.  
  129.     // Column Reduction
  130.     int col[N];
  131.     columnReduction(reducedMatrix, col);
  132.  
  133.     // the total expected cost
  134.     // is the sum of all reductions
  135.     for (int i = 0; i < N; i++)
  136.         cost += (row[i] != INT_MAX) ? row[i] : 0,
  137.             cost += (col[i] != INT_MAX) ? col[i] : 0;
  138.  
  139.     return cost;
  140. }
  141.  
  142. // print list of cities visited following least cost
  143. void printPath(vector<pair<int, int>> const &list)
  144. {
  145.     for (int i = 0; i < list.size(); i++)
  146.         cout << list[i].first + 1 << " -> "
  147.              << list[i].second + 1 << endl;
  148. }
  149.  
  150. // Comparison object to be used to order the heap
  151. struct comp {
  152.     bool operator()(const Node* lhs, const Node* rhs) const
  153.     {
  154.         return lhs->cost > rhs->cost;
  155.     }
  156. };
  157.  
  158. // Function to solve Traveling Salesman Problem using Branch and Bound
  159. int solve(int costMatrix[N][N])
  160. {
  161.     // Create a priority queue to store live nodes of search tree;
  162.     priority_queue<Node*, std::vector<Node*>, comp> pq;
  163.  
  164.     vector<pair<int, int>> v;
  165.  
  166.     // create a root node and calculate its cost
  167.     // The TSP starts from first city i.e. node 0
  168.     Node* root = newNode(costMatrix, v, 0, -1, 0);
  169.  
  170.     // get the lower bound of the path starting at node 0
  171.     root->cost = calculateCost(root->reducedMatrix);
  172.  
  173.     // Add root to list of live nodes;
  174.     pq.push(root);
  175.  
  176.     // Finds a live node with least cost, add its children to list of
  177.     // live nodes and finally deletes it from the list
  178.     while (!pq.empty())
  179.     {
  180.         // Find a live node with least estimated cost
  181.         Node* min = pq.top();
  182.  
  183.         // The found node is deleted from the list of live nodes
  184.         pq.pop();
  185.  
  186.         // i stores current city number
  187.         int i = min->vertex;
  188.  
  189.         // if all cities are visited
  190.         if (min->level == N - 1)
  191.         {
  192.             // return to starting city
  193.             min->path.push_back(make_pair(i, 0));
  194.  
  195.             // print list of cities visited;
  196.             printPath(min->path);
  197.  
  198.             // return optimal cost
  199.             return min->cost;
  200.         }
  201.  
  202.         // do for each child of min
  203.         // (i, j) forms an edge in space tree
  204.         for (int j = 0; j < N; j++)
  205.         {
  206.             if (min->reducedMatrix[i][j] != INF)
  207.             {
  208.                 // create a child node and calculate its cost
  209.                 Node* child = newNode(min->reducedMatrix, min->path,
  210.                     min->level + 1, i, j);
  211.  
  212.                 /* Cost of the child =
  213.                     cost of parent node +
  214.                     cost of the edge(i, j) +
  215.                     lower bound of the path starting at node j
  216.                 */
  217.                 child->cost = min->cost + min->reducedMatrix[i][j]
  218.                             + calculateCost(child->reducedMatrix);
  219.  
  220.                 // Add child to list of live nodes
  221.                 pq.push(child);
  222.             }
  223.         }
  224.  
  225.         // free node as we have already stored edges (i, j) in vector.
  226.         // So no need for parent node while printing solution.
  227.         delete min;
  228.     }
  229. }
  230.  
  231. // main function
  232. int main()
  233. {
  234.     // cost matrix for traveling salesman problem.
  235.     /*
  236.     int costMatrix[N][N] =
  237.     {
  238.         {INF, 5,   INF, 6,   5,   4},
  239.         {5,   INF, 2,   4,   3,   INF},
  240.         {INF, 2,   INF, 1,   INF, INF},
  241.         {6,   4,   1,   INF, 7,   INF},
  242.         {5,   3,   INF, 7,   INF, 3},
  243.         {4,   INF, INF, INF, 3,   INF}
  244.     };
  245.     */
  246.     // cost 34
  247.     int costMatrix[N][N] =
  248.     {
  249.         { INF, 10,  8,   9,   7 },
  250.         { 10,  INF, 10,  5,   6 },
  251.         { 8,   10,  INF, 8,   9 },
  252.         { 9,   5,   8,   INF, 6 },
  253.         { 7,   6,   9,   6,   INF }
  254.     };
  255.  
  256.     /*
  257.     // cost 16
  258.     int costMatrix[N][N] =
  259.     {
  260.         {INF, 3,   1,   5,   8},
  261.         {3,   INF, 6,   7,   9},
  262.         {1,   6,   INF, 4,   2},
  263.         {5,   7,   4,   INF, 3},
  264.         {8,   9,   2,   3,   INF}
  265.     };
  266.     */
  267.  
  268.     /*
  269.     // cost 8
  270.     int costMatrix[N][N] =
  271.     {
  272.         {INF, 2,   1,   INF},
  273.         {2,   INF, 4,   3},
  274.         {1,   4,   INF, 2},
  275.         {INF, 3,   2,   INF}
  276.     };
  277.     */
  278.  
  279.     /*
  280.     // cost 12
  281.     int costMatrix[N][N] =
  282.     {
  283.         {INF, 5,   4,   3},
  284.         {3,   INF, 8,   2},
  285.         {5,   3,   INF, 9},
  286.         {6,   4,   3,   INF}
  287.     };
  288.     */
  289.  
  290.     cout << "\n\nTotal Cost is " << solve(costMatrix);
  291.  
  292.     return 0;
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement