# Lecture 8 - TSP and Intro to Divide & Conquer

## Traveling Salesperson Problem

The traveling salesperson problem (TSP) is as follows:

Given a number of cities and the distances between any pair of cities, find the shortest round tour, that starts in one city and visits each other city once, bringing you back to the start

Here’s an example:

We could try iterating over every city, but that gives us an NP-Hard complexity of $O(n!)$. We can use a *dynamic programming algorithm* to solve this problem. We will look at a problem that runs in $2^n \times n^2$ time complexity, which isn’t great, but is a lot better than $n!$.

The TSP is a very good benchmark for testing compute power, since it’s a very difficult problem to solve quickly. We can also approximate the TSP

### Recurrence Equation

Let $C = {1,2,…,n}$ be the set of cities to be visited. Assume the tour starts in city 1. Consider a set $S$ as a subset of the cities $C$, where $1 \in S$ and $c \in S$. Let $l(S,c)$ be the length of a shortest trip from 1 to $c$ visiting all cities in $S$.

A minimum tour through all cities in ${1,2,..,n}$ has length $\min{l(C,c) + d[c,1] \mid c \in C {1}}$

### Correctness & Running Time

If the recurrence is correct, then the algorithm is correct.

Since $\sum^n_{k=0}(n,k) = 2^n$ the overall running time is $O(2^n n^2)$. We can ignore the $n^2$ by rules of big O, so the time complexity is $O(2^n)$. The polynomial factor is absorbed by a more exponential part in the running time.

### A visual example

Wow thanks Haiko!

## Divide & Conquer

D&C leads to recursive algorithms. It is comprised of 3 parts:

**Divide**the current instance into smaller sub-instances that can be solved in the same way**Apply**the same technique to the subproblems. If the problem is too small, solve by a simple method**Combine**the solutions together into a single solution

Examples of D&C algorithms:

- Binary search
- Merge sort
- Quick sort

### Recurrences of Divide & Conquer

The running time is bounded by the function $T : \N \rightarrow \N$ given by a recurrence. Each recurrence consists of *initial conditions*, and a *recursive equation* that describes how the value is passed to a later recurrence.

Some example recurrences:

### Methods for solving recurrences

**Direct Method:**Always applies to D&C algorithms, but might be tricky in others**Guessing the Solution:**Applies to recurrences of the form $T(n) = a_1T(n-1)+a_2(n-2)+…+a_kT(n-k)$**Master Method:**Applies to recurrences of the form $T(n)=aT(n\div b) + f(n)$