Nadar saraswathi college of arts & science,theni.
Department of cs & it
data structures and algorithms
PRESENTED BY
G.KAVIYA
M.SC(IT)
TOPIC:TRAVELLING SALESMAN
PROBLEM.
TRAVELLING SALESMAN
PROBLEM
Travelling Salesman Problem using Branch and
Bound
Travelling salesman problem:
 A set of some cities.
 Distance between every pair of cities.
Travelling salesman problem states-
 A salesman has to visit every city exactly once.
 He has to come back to the city from where he starts his journey.
 What is the shortest possible route that the salesman must
follow to complete his tour?
EXAMPLE:
The following graph shows a set of cities and distances between every pair of
cities-
If salesman starting city is A, then a TSP tour in the given is-
A  B  D  C  A
Cost of the tour
= 10 + 25 + 30 + 15
=80 units.
PRACTICE PROBLEM BASED ON TRAVELLING
SALESMAN PROBLEM USING BRANCH AND BOUND
APPROACH-
Problem-
Solve Travelling Salesman Problem using Branch and Bound Algorithm in the
following graph-
Solution-
Step-01:
Write the initial cost matrix and reduce it-
Row Reduction-
Consider the rows of above matrix one by one.
If the row already contains an entry ‘0’, then-
 There is no need to reduce that row.
If the row does not contains an entry ‘0’, then-
 Reduce that particular row.
 Select the least value element from that row.
 Subtract that element from each element of that row.
 This will create an entry ‘0’ in the row, thus reducing that row.
Following this, we have-
• Reduce the elements of row-1 by 4.
• Reduce the elements of row-2 by 5.
• Reduce the elements of row-3 by 6.
• Reduce the elements of row-4 by 2.
Cont;
Cont;
Performing this, we obtain the following row-reduced matrix-
Column Reduction-
Consider the columns of above row-reduced matrix one by one.
If the contains already contains an entry ‘0’, then-
 There is no need to reduce that column.
Cont;
If the column does not contains an entry ’0’, then-
 Reduce that particular column.
 Select the least value element from that column.
 Subtract that element from each element of that column.
 This will create an entry ‘0’ in the row, thus reducing that column.
Following this, we have-
• There is no need to reduce column-1.
• There is no need to reduce column-2.
• Reduce the elements of column-3 by -1.
• There is no need to reduce column-4.
Cont;
Performing this, we obtain the following column-reduced matrix-
Finally, the initial distance matrix is completely reduced.
Now, we calculate the cost of node-1 by adding all the reduction elements.
Cost(1)
=sum of all reduction elements
=4 + 5 + 6 + 2 + 1
=18
Cont;
Step-02:
o We consider all other vertices one by one.
o We select the best vertex where we can land upon to minimize the tour cost.
Choosing To Go To Vertex-B: Node-2(Path A  B)
• From the reduced matrix of step-1, M[A,B] = 0
• Set row-A and column-B to ∞
• Set M[B,A] = ∞
Cont;
Now, resulting cost matrix is-
Now,
• We reduce this matrix.
• Then, we find out the cost of node-02.
Cont;
Row Reduction-
 We can not reduce row-1 as all its elements are ∞.
 Reduce all the elements of row-2 by 13.
 There is no need to reduce row-3.
 There is no need to reduce row-4.
Performing this, we obtain the following row-reduced matrix-
Cont;
Column Reduction-
 Reduction the elements of column-1 by -5.
 We can not reduce column-2 as all its elements are ∞.
 There is no need to reduce column-3.
 There is no need to reduce column-4.
Performing this, we obtain the following column-reduced matrix-
Cont;
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-2.
Cost(2)
=Cost(1) + Sum of reduction elements + M[A,B]
=18 + (13 + 5) +0
=36
Choosing To Go To Vertex-C: Node-3 (Path A  C)
 From the reduced matrix of step-01,M[A,C]=7
 Set row-A and column-C to ∞.
 Set M[C,A] = ∞.
Cont;
Now, resulting cost matrix is-
Now,
• We reduce this matrix.
• Then, we find out the cost of node-03.
Cont;
Row Reduction-
 We can not reduce row-1 as all its elements are ∞.
 There is no need to reduce row-2.
 There is no need to reduce row-3.
 There is no need to reduce row-4.
Thus, the matrix is already row-reduced.
Column Reduction-
 There is no need to reduce column-1.
 There is no need to reduce column-2.
 We can not reduce column-3 as all its elements are ∞.
 There is no need to reduce column-4.
Thus, the matrix is already column-reduced.
Cont;
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-3.
Cost(3)
=Cost(1) + Sum of reduction elements + M[A,C]
=18 + 0 + 7
=25
Choosing To Go To Vertex-D: Node-4 (Path A  D)
• From the reduced matrix of step-01,M[A,D] = 3
• Set row-A and column-D to ∞.
• Set M[D,A] = ∞.
Cont;
Now, resulting cost matrix is-
Now,
 We reduce this matrix.
 Then, we find out the cost of node-04.
Cont;
Row Reduction-
• We can not reduce row-1 as all its elements are ∞.
• There is no need to reduce row-2.
• Reduce all the elements of row-3 by 5.
• There is no need to reduce row-4.
Performing this, we obtain the following row-reduced matrix-
Cont;
Column Reduction-
• There is no need to reduce column-1.
• There is no need to reduce column-2.
• There is no need to reduce column-3.
• We can not reduce column-4 as all its elements are ∞.
Thus, the matrix is already column-reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-4.
Cost(4)
=Cost(1) + Sum of reduction elements + M[A,D]
=18 + 5 + 3
=26
Cont;
Thus, we have-
 Cost (2) = 36 (for path A  B)
 Cost (3) = 25 (for path A  C)
 Cost (4) = 26 (for path A  D)
We choose the node with the lowest cost.
Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e, path A  C
Cont;
Step-03:
We explore the vertices B and D from node-3.
We now start from the cost matrix at node-3 which is-
Cost(3) = 25
Cont;
Choosing To Go To Vertex-B: Node-5 (Path A  C  B)
• From the reduce matrix of step-02,M[C,B] = ∞.
• Set row-C and column-B to ∞.
• Set M[B,A] = ∞.
Now, resulting cost matrix is-
Cont;
Now,
 We reduce this matrix.
 Then, we find out the cost of node-5.
Row Reduction:
• We can not reduce row-1 as all its elements are ∞.
• Reduce all the elements of row-2 by 13.
• We can not reduce row-3 as all its elements are ∞.
• Reduce all the elements of row-4 by 8.
Cont;
Performing this, we obtain the following row-reduced matrix-
Column Reduction:
• There is no need to reduce column-1.
• We can not reduce column-2 as all its elements are ∞.
• We can not reduce column-3 as all its elements are ∞.
Cont;
• There is no need to reduce column-4.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-5.
Cost(5)
=cost(3) + Sum of reduction elements + M[C,B]
=25 + (13 + 8) + ∞
= ∞.
Cont;
Choosing To Go To Vertex-D: Node-6 (Path A  C  D)
• From the reduced matrix of step-02,M[C,D] = ∞.
• Set row-C and column-D to ∞.
• Set M[D,A] = ∞.
Now, resulting cost matrix is-
Cont;
Now,
 We reduce this matrix.
 Then, we find out the cost of node-6.
Row Reduction:
• We can not reduce row-1 as all its elements are ∞.
• There is no need to reduce row-2.
• We can not reduce row-3 as all its elements are ∞.
• We can not reduce row-4 as all its elements are ∞.
Thus, the matrix is already row reduced.
Cont;
Column Reduction:
• There is no need to reduce column-1.
• We can not reduce column-2 as all its elements are ∞.
• We can not reduce column-3 as all its elements are ∞.
• We can not reduce column-4 as all its elements are ∞.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
Now, we calculate the cost of node-6.
Cont;
Cos(6)
=cost(3) + Sum of reduction elements + M[C,D]
=25 + 0 + 0
=25
Thus, we have-
 Cost(5) = ∞ (for path A  C  B)
 Cost(6) = 25 (for path A  C  D)
We choose the node with the lowest cost.
Since cost for node-6 is lowest, so we prefer to visit node-6.
Thus, we choose node-6 i.e, path C  D
Cont;
Step-04:
We explore vertex B from node-6.
We start will the cost matrix at node-6 which is-
Cost(6) = 25
Cont;
Choosing To Go To Vertex-B: Node-7 (Path A  C  D  B)
• From the reduced matrix of step-03, M[D,B] = 0.
• Set row-D and column-B to ∞.
• Set M[B,A] = ∞.
Now, resulting cost matrix is-
Cont;
Now,
 We reduce the matrix.
 Then, we find out the cost of node-7.
Row Reduction:
• We can not reduce row reduce row-1 as all its elements are ∞.
• We can not reduce row reduce row-2 as all its elements are ∞.
• We can not reduce row reduce row-3 as all its elements are ∞.
• We can not reduce row reduce row-4 as all its elements are ∞.
Cont;
Column Reduction:
• We can not reduce column-1 as all its elements are ∞.
• We can not reduce column-2 as all its elements are ∞.
• We can not reduce column-3 as all its elements are ∞.
• We can not reduce column-4 as all its elements are ∞.
Thus, the matrix is already column reduce.
Finally, the matrix is completely reduced.
All the entries have become ∞.
Cont;
Now, we calculate the cost node-7
Cost(7)
=cost(6) + Sum of reduction elements + M[D,B]
=25 + 0 + 0
=25
Thus,
 Optimal path is: A C  D  B  A
 Cost of Optimal path is = 25 units.
EFFICIENCY
CONSIDERATIONS
EFFICIENCY CONSIDERATION:
 When doing branch and bound, efficiency considerations usually
demand that a priority queue be used to hold the nodes that have
been generated but not yet processed.
 The heap is often an ideal structure for implementing a large priority
queue.
 Branch and bound algorithms may be less than straightforward to
program. Also, it may not be tractable to calculate precise estimates of
the big -0 or big -0 of a branch and bound algorithm.
 Nevertheless, branch and bound techniques are well worth
considering and using in many applications, because they have the
potential to greatly reduce the amount of work involved in computing.
 A good bounding function is one that can be calculated quickly and
which has a high probability of diverting the search toward the
better solutions.
 It is something of an “art” to design a good bounding function.
Sometimes it requires considerable expertise. Consider the problem
of evaluating the worth of a board position in the game of chess, or of
evaluating the standing of a nation in the world’s economy.
Data structures and algorithms

Data structures and algorithms

  • 1.
    Nadar saraswathi collegeof arts & science,theni. Department of cs & it data structures and algorithms PRESENTED BY G.KAVIYA M.SC(IT) TOPIC:TRAVELLING SALESMAN PROBLEM.
  • 2.
  • 3.
    Travelling Salesman Problemusing Branch and Bound Travelling salesman problem:  A set of some cities.  Distance between every pair of cities. Travelling salesman problem states-  A salesman has to visit every city exactly once.  He has to come back to the city from where he starts his journey.  What is the shortest possible route that the salesman must follow to complete his tour?
  • 4.
    EXAMPLE: The following graphshows a set of cities and distances between every pair of cities- If salesman starting city is A, then a TSP tour in the given is- A  B  D  C  A Cost of the tour = 10 + 25 + 30 + 15 =80 units.
  • 5.
    PRACTICE PROBLEM BASEDON TRAVELLING SALESMAN PROBLEM USING BRANCH AND BOUND APPROACH- Problem- Solve Travelling Salesman Problem using Branch and Bound Algorithm in the following graph-
  • 6.
    Solution- Step-01: Write the initialcost matrix and reduce it- Row Reduction- Consider the rows of above matrix one by one. If the row already contains an entry ‘0’, then-  There is no need to reduce that row.
  • 7.
    If the rowdoes not contains an entry ‘0’, then-  Reduce that particular row.  Select the least value element from that row.  Subtract that element from each element of that row.  This will create an entry ‘0’ in the row, thus reducing that row. Following this, we have- • Reduce the elements of row-1 by 4. • Reduce the elements of row-2 by 5. • Reduce the elements of row-3 by 6. • Reduce the elements of row-4 by 2. Cont;
  • 8.
    Cont; Performing this, weobtain the following row-reduced matrix- Column Reduction- Consider the columns of above row-reduced matrix one by one. If the contains already contains an entry ‘0’, then-  There is no need to reduce that column.
  • 9.
    Cont; If the columndoes not contains an entry ’0’, then-  Reduce that particular column.  Select the least value element from that column.  Subtract that element from each element of that column.  This will create an entry ‘0’ in the row, thus reducing that column. Following this, we have- • There is no need to reduce column-1. • There is no need to reduce column-2. • Reduce the elements of column-3 by -1. • There is no need to reduce column-4.
  • 10.
    Cont; Performing this, weobtain the following column-reduced matrix- Finally, the initial distance matrix is completely reduced. Now, we calculate the cost of node-1 by adding all the reduction elements. Cost(1) =sum of all reduction elements =4 + 5 + 6 + 2 + 1 =18
  • 11.
    Cont; Step-02: o We considerall other vertices one by one. o We select the best vertex where we can land upon to minimize the tour cost. Choosing To Go To Vertex-B: Node-2(Path A  B) • From the reduced matrix of step-1, M[A,B] = 0 • Set row-A and column-B to ∞ • Set M[B,A] = ∞
  • 12.
    Cont; Now, resulting costmatrix is- Now, • We reduce this matrix. • Then, we find out the cost of node-02.
  • 13.
    Cont; Row Reduction-  Wecan not reduce row-1 as all its elements are ∞.  Reduce all the elements of row-2 by 13.  There is no need to reduce row-3.  There is no need to reduce row-4. Performing this, we obtain the following row-reduced matrix-
  • 14.
    Cont; Column Reduction-  Reductionthe elements of column-1 by -5.  We can not reduce column-2 as all its elements are ∞.  There is no need to reduce column-3.  There is no need to reduce column-4. Performing this, we obtain the following column-reduced matrix-
  • 15.
    Cont; Finally, the matrixis completely reduced. Now, we calculate the cost of node-2. Cost(2) =Cost(1) + Sum of reduction elements + M[A,B] =18 + (13 + 5) +0 =36 Choosing To Go To Vertex-C: Node-3 (Path A  C)  From the reduced matrix of step-01,M[A,C]=7  Set row-A and column-C to ∞.  Set M[C,A] = ∞.
  • 16.
    Cont; Now, resulting costmatrix is- Now, • We reduce this matrix. • Then, we find out the cost of node-03.
  • 17.
    Cont; Row Reduction-  Wecan not reduce row-1 as all its elements are ∞.  There is no need to reduce row-2.  There is no need to reduce row-3.  There is no need to reduce row-4. Thus, the matrix is already row-reduced. Column Reduction-  There is no need to reduce column-1.  There is no need to reduce column-2.  We can not reduce column-3 as all its elements are ∞.  There is no need to reduce column-4. Thus, the matrix is already column-reduced.
  • 18.
    Cont; Finally, the matrixis completely reduced. Now, we calculate the cost of node-3. Cost(3) =Cost(1) + Sum of reduction elements + M[A,C] =18 + 0 + 7 =25 Choosing To Go To Vertex-D: Node-4 (Path A  D) • From the reduced matrix of step-01,M[A,D] = 3 • Set row-A and column-D to ∞. • Set M[D,A] = ∞.
  • 19.
    Cont; Now, resulting costmatrix is- Now,  We reduce this matrix.  Then, we find out the cost of node-04.
  • 20.
    Cont; Row Reduction- • Wecan not reduce row-1 as all its elements are ∞. • There is no need to reduce row-2. • Reduce all the elements of row-3 by 5. • There is no need to reduce row-4. Performing this, we obtain the following row-reduced matrix-
  • 21.
    Cont; Column Reduction- • Thereis no need to reduce column-1. • There is no need to reduce column-2. • There is no need to reduce column-3. • We can not reduce column-4 as all its elements are ∞. Thus, the matrix is already column-reduced. Finally, the matrix is completely reduced. Now, we calculate the cost of node-4. Cost(4) =Cost(1) + Sum of reduction elements + M[A,D] =18 + 5 + 3 =26
  • 22.
    Cont; Thus, we have- Cost (2) = 36 (for path A  B)  Cost (3) = 25 (for path A  C)  Cost (4) = 26 (for path A  D) We choose the node with the lowest cost. Since cost for node-3 is lowest, so we prefer to visit node-3. Thus, we choose node-3 i.e, path A  C
  • 23.
    Cont; Step-03: We explore thevertices B and D from node-3. We now start from the cost matrix at node-3 which is- Cost(3) = 25
  • 24.
    Cont; Choosing To GoTo Vertex-B: Node-5 (Path A  C  B) • From the reduce matrix of step-02,M[C,B] = ∞. • Set row-C and column-B to ∞. • Set M[B,A] = ∞. Now, resulting cost matrix is-
  • 25.
    Cont; Now,  We reducethis matrix.  Then, we find out the cost of node-5. Row Reduction: • We can not reduce row-1 as all its elements are ∞. • Reduce all the elements of row-2 by 13. • We can not reduce row-3 as all its elements are ∞. • Reduce all the elements of row-4 by 8.
  • 26.
    Cont; Performing this, weobtain the following row-reduced matrix- Column Reduction: • There is no need to reduce column-1. • We can not reduce column-2 as all its elements are ∞. • We can not reduce column-3 as all its elements are ∞.
  • 27.
    Cont; • There isno need to reduce column-4. Thus, the matrix is already column reduced. Finally, the matrix is completely reduced. Now, we calculate the cost of node-5. Cost(5) =cost(3) + Sum of reduction elements + M[C,B] =25 + (13 + 8) + ∞ = ∞.
  • 28.
    Cont; Choosing To GoTo Vertex-D: Node-6 (Path A  C  D) • From the reduced matrix of step-02,M[C,D] = ∞. • Set row-C and column-D to ∞. • Set M[D,A] = ∞. Now, resulting cost matrix is-
  • 29.
    Cont; Now,  We reducethis matrix.  Then, we find out the cost of node-6. Row Reduction: • We can not reduce row-1 as all its elements are ∞. • There is no need to reduce row-2. • We can not reduce row-3 as all its elements are ∞. • We can not reduce row-4 as all its elements are ∞. Thus, the matrix is already row reduced.
  • 30.
    Cont; Column Reduction: • Thereis no need to reduce column-1. • We can not reduce column-2 as all its elements are ∞. • We can not reduce column-3 as all its elements are ∞. • We can not reduce column-4 as all its elements are ∞. Thus, the matrix is already column reduced. Finally, the matrix is completely reduced. Now, we calculate the cost of node-6.
  • 31.
    Cont; Cos(6) =cost(3) + Sumof reduction elements + M[C,D] =25 + 0 + 0 =25 Thus, we have-  Cost(5) = ∞ (for path A  C  B)  Cost(6) = 25 (for path A  C  D) We choose the node with the lowest cost. Since cost for node-6 is lowest, so we prefer to visit node-6. Thus, we choose node-6 i.e, path C  D
  • 32.
    Cont; Step-04: We explore vertexB from node-6. We start will the cost matrix at node-6 which is- Cost(6) = 25
  • 33.
    Cont; Choosing To GoTo Vertex-B: Node-7 (Path A  C  D  B) • From the reduced matrix of step-03, M[D,B] = 0. • Set row-D and column-B to ∞. • Set M[B,A] = ∞. Now, resulting cost matrix is-
  • 34.
    Cont; Now,  We reducethe matrix.  Then, we find out the cost of node-7. Row Reduction: • We can not reduce row reduce row-1 as all its elements are ∞. • We can not reduce row reduce row-2 as all its elements are ∞. • We can not reduce row reduce row-3 as all its elements are ∞. • We can not reduce row reduce row-4 as all its elements are ∞.
  • 35.
    Cont; Column Reduction: • Wecan not reduce column-1 as all its elements are ∞. • We can not reduce column-2 as all its elements are ∞. • We can not reduce column-3 as all its elements are ∞. • We can not reduce column-4 as all its elements are ∞. Thus, the matrix is already column reduce. Finally, the matrix is completely reduced. All the entries have become ∞.
  • 36.
    Cont; Now, we calculatethe cost node-7 Cost(7) =cost(6) + Sum of reduction elements + M[D,B] =25 + 0 + 0 =25 Thus,  Optimal path is: A C  D  B  A  Cost of Optimal path is = 25 units.
  • 37.
  • 38.
    EFFICIENCY CONSIDERATION:  Whendoing branch and bound, efficiency considerations usually demand that a priority queue be used to hold the nodes that have been generated but not yet processed.  The heap is often an ideal structure for implementing a large priority queue.  Branch and bound algorithms may be less than straightforward to program. Also, it may not be tractable to calculate precise estimates of the big -0 or big -0 of a branch and bound algorithm.  Nevertheless, branch and bound techniques are well worth considering and using in many applications, because they have the potential to greatly reduce the amount of work involved in computing.
  • 39.
     A goodbounding function is one that can be calculated quickly and which has a high probability of diverting the search toward the better solutions.  It is something of an “art” to design a good bounding function. Sometimes it requires considerable expertise. Consider the problem of evaluating the worth of a board position in the game of chess, or of evaluating the standing of a nation in the world’s economy.