0

Consider the weighted graph given from the table below. Find the nearest neighbor solution to the traveling salesperson problem starting from vertex A.

enter image description here

1 Answers1

1

Directly employing this answer:

m1 = {{1, 2} -> 12, {1, 3} -> 14, {1, 4} -> 5, {1, 5} -> 4, {2, 3} -> 
    10, {2, 4} -> 3, {2, 5} -> 8, {3, 4} -> 7, {3, 5} -> 6, {4, 5} -> 
    9};
m2 = Reverse@#[[1]] -> #[[2]] & /@ m1;
m = m1~Join~m2;
d = SparseArray[m, {5, 5}, Infinity];
{len, tour} = 
 FindShortestTour[Range@5, DistanceFunction -> (d[[#1, #2]] &)]

{28, {1, 4, 2, 3, 5, 1}}

HighlightGraph[
 WeightedAdjacencyGraph[d, GraphStyle -> "SmallNetwork", 
  EdgeLabels -> "EdgeWeight", 
  EdgeLabelStyle -> Directive[Black, Bold, 15]], 
 Style[UndirectedEdge[#1, #2], Thickness[.007], Red] & @@@ 
  Partition[tour, 2, 1, 1]]

enter image description here

corey979
  • 23,947
  • 7
  • 58
  • 101