# Computational Complexity

PGSS Computer Science Core Slides

Are there problem classes that have no good algorithms? How can ew prove it?

These are questions for complexity researchers.

### Good Algorithms

Bob: What makes an algorithm ``good''?

Graph input size against computation time; if it can be bounded above by a polynomial (any polynomial will do), the algorithm is good.

```       ^
|          _                        _
|          _ a polynomial          _
|         _                       _
|         _                    _-~
time   |        _                   _- algorithm
req'd  |       _                   -   performance
|      _                   -
|    _~                __-~
|__-~        _------~~~
|__---------~
+----------------------------------------------------->
bits required for input -->
```

### Complaints

Bob: Polynomials can be much worse than 2^n for reasonable values of n!
Alice: Why not define good algorithms to be bounded by degree-6 polynomials?
• Natural problem classes required high-degree polynomials are rare and usually short-lived.
• Composability is important. A good algorithm used a good number of times should take a good about of time. But n^6 * n^6 = n^12 is not degree-6.
• It often works.

### Easy Problems

A problem class is easy if a good algorithm exists for it. The set of easy problem classes is called P.

There is a much larger set of problem classes called NP.

```  +-----------------------------------------------------------------+
|   ___________________________________________________________   |
|  /   ________________                                        \  |
|  |  /                \                                   NP  |  |
|  |  |             P  |                                       |  |
|  |  |                |        .                              |  |
|  |  | .ADDITION      |    PRIMALITY                          |  |
|  |  |                |                                       |  |
|  |  |                |                                       |  |
|  |  |           .    |                                .      |  |
|  |  | ALL-PAIRS-PATHS|                             HAM-PATH  |  |
|  |  |                |                                       |  |
|  |  |                |                                       |  |
|  |  |                |                                       |  |
|  |  |                |                                       |  |
|  |  \________________/                                       |  |
|  \___________________________________________________________/  |
|                                                                 |
+-----------------------------------------------------------------+

Problem class topology
```

### The \$1,000,000 Question

Does P=NP?

Are there problem classes in NP which are not in P?

(We know P is a subset of NP.)

This is the biggest open question is computer science (and probably mathematics too).

### The \$1,000 Answer

We think not: P != NP. No proof yet, but we have evidence.

We do know this:

If HAM-PATH is in P, then P = NP.
Problem classes with this property are called NP-hard. NP-hard problems in NP are called NP-complete.

```  +-----------------------------------------------------------------+
|   ________________________________________________________      |
|  /   ________________                                     \     |
|  |  /                \                                NP  |     |
|  |  |             P  |                     _______________|___  |
|  |  |                |        .           /               |   \ |
|  |  | .ADDITION      |    PRIMALITY       |           NP-hard | |
|  |  |                |                    |               |   | |
|  |  |                |                    |               |   | |
|  |  |           .    |                    |        .      |   | |
|  |  | ALL-PAIRS-PATHS|                    |     HAM-PATH  |   | |
|  |  |                |                    |               |   | |
|  |  |                |                    |               |   | |
|  |  |                |                    |               |   | |
|  |  |                |                    \_______________|___/ |
|  |  \________________/                                    |     |
|  \________________________________________________________/     |
|                                                                 |
+-----------------------------------------------------------------+

Revised problem class topology
```

Many very important problem classes are NP-complete, so people have nonetheless worken on them very hard.

After 30 years, we haven't found that any are in P. Hence our confidence in the P != NP conjecture.

### Proving NP-hardness

Alice: How can I show that my favorite problem class is NP-hard?

To show A is NP-hard, we reduce a known NP-hard problem class B to A. That is, we show taht a good algorithm for A implies a good algorithm for B. Since this would imply P = NP, it follows that A is NP-hard.

(This only works when we know of other NP-hard problems. Take my word they exist.)

### A First Reduction

We begin with the known NP-hard problem class VERTEX-COVER.

Input: a graph (V, E)
Output: a minimal subset V' of V so that every vertex in V is adjacent to a vertex in V'.

example:

```      *---o---o---*---o
\_  \_ | _/| _/
\   \|/  |/
o---o---o
\_____/
* represents the output set
```

We show that SET-COVER is NP-hard.

Input: a base set S, a collection C of subsets of S.
Output: a minimal subcollection of C whose union is S.

example:

```  ______
/      \
|  ____|_______
| / o  |     o \
| \____|_______/ <---\
|  ____|_______       > The optimal set cover
| / o  |     o \ <---/
| \____|_______/
\______/
```

The NP-hardness proof:

```      a---b---c---d---e
\_  \_ | _/| _/
\   \|/  |/
f---g---h
\_____/
```
2. Show how we can use a SET-COVER algorithm to solve it.
```    S = { a, b, c, d, e, f, g, h }
C = { {a, b, f}, {b, a, c, g}, {c, b, d, g}, {d, c, e, g, h},
{e, d, h}, {f, a, g, h}, {g, b, c, d, f, h}, {h, d, e, g} }
```
3. So VERTEX-COVER is NP-hard implies SET-COVER is NP-hard.

### Another Reduction

Problem class FACILITY-LOCATION

Input: a graph (V, E) with distances, a radius r, and a subset H of V.
Output: the smallest subset of V so that each element of H is within r of something in the subset.

example:

```      x--1--o--1--o--1--x--1--*--2--x
\___  \_1_       |
1 \     \      | 1
o--1--*--1--o
\1
\
x
x represents the houses (elements of H)
* represents the optimal facility placement (the output)
```

The proof:

```  ______
/   C  \
|  ____|_______
| / b  | A   a \
| \____|_______/ <---\
|  ____|_______       > The optimal set cover
| / c  | B   d \ <---/
| \____|_______/
\______/
```
2. Show how we can use a FACILITY-LOCATION algorithm to solve it.
```
A   C   B
/ \ / \ / \
a   b   c   d

All edges have length 1.
H = {a, b, c, d}
r = 1
```
Any solution can be changed to a SET-COVER solution (just covering the set-vertices) by removing any house in the optimal cover and selecting a non-house adjacent to it. A facility only covers that house, and that house remains covered.
3. So SET-COVER is NP-hard implies FACILITY-LOCATION is NP-hard.