The Classical Traveling Salesman Problem is a very simple algorithm to implement. There are two conditions that a graph must meet in order to apply this algorithm:

1.) the graph is complete
2.) the graph has the Euclidean property (the direct edge between two vertices has the smallest possible weight).

Following with the IBDP Math HL curriculum on graph theory, we "solve" this problem by computing upper and lower bounds for the optimal solution. For the upper bound, we simply apply the nearest-neighbor algorithm. For the lower bound, we delete a vertex (and all incident edges), then apply Kruskal's algorithm to find a minimum spanning tree. Then we add back the two deleted edges with the least weight. The sum of the weights of these edges gives us a lower bound for the TSP.

Working on this algorithm has made me appreciate the concept of encapsulation. All of the hard work that has gone into designing the characteristics of vertices, edges, and graphs, as well as minimum spanning tree algorithms, was already done. All that was left was to bring all of these separate pieces together, without having to worry about their underlying mechanisms. When coding works, it sure is sweet.

#swift #swiftui #ios #macos #app #appdevelopment #mobileapp #coding #algorithm #math #graphtheory #ibdp #mathhl