40
CS221 Week 13 - Monday

Week 13 - Monday. What did we talk about last time? B-trees Hamiltonian tour Traveling Salesman Problem

Embed Size (px)

Citation preview

Page 1: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

CS221Week 13 - Monday

Page 2: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Last time

What did we talk about last time? B-trees Hamiltonian tour Traveling Salesman Problem

Page 3: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Questions?

Page 4: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Project 4

Page 5: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Assignment 6

Page 6: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

TSP

Page 7: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Traveling Salesman Problem:Hamiltonian Cycle Meets Shortest Path

Given a graph, find the shortest tour that visits every node and comes back to the starting point

Like a lazy traveling salesman who wants to visit all possible clients and then return to his home

Page 8: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

What’s the Shortest Tour? Find the shortest TSP tour:

Starts anywhere Visits all nodes Has the shortest length of such a tour

E

A

DB

FC

56

44

35

27

3

3

Page 9: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

How Can We Always Find the Shortest Tour? Strategies:

Always add the nearest neighbor Randomized approaches Try every possible combination

Why are we only listing strategies?

Page 10: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Best Solution to TSP

So far we haven’t given a foolproof solution to TSP

Let’s look at two possibilities: Greedy Solution: Pick the closest

neighbor Brute Force: Try all possibilities

Page 11: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Greedy Doesn’t Work

We are tempted to always take the closest neighbor, but there are pitfalls Greedy Optimal

Page 12: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Brute Force is Brutal

In a completely connected graph, we can try any sequence of nodes

If there are n nodes, there are (n – 1)! tours

For 30 cities, 29! = 8841761993739701954543616000000

If we can check 1,000,000,000 tours in one second, it will only take about 20,000 times the age of the universe to check them all

We will (eventually) get the best answer!

Page 13: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

What’s the Best Time for TSP? No one knows how to find the best

solution to TSP in an efficient amount of time

For a general graph, no good approximation even exists

For a graph with the triangle inequality, there is an approximation that yields a tour no more than 3/2 the optimal

Some variations on the problem are easier than others

Page 14: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Hard Problems

Is TSP the hardest problem there is? Are there other problems equally

hard? How do we compare the difficulty of

one problem to another?

Page 15: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

NP-Completeness

TSP is one of many problems which are called NP-complete

All of these problems share the characteristic that the only way we know to find best solution uses brute force

All NP-complete problems are reducible to all other NP-complete problems

An efficient solution to one would guarantee an efficient solution to all

Page 16: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

NP-complete Problems on Graphs Traveling Salesman Problem Hamiltonian Cycle (and Path) Let’s see just a couple more…

Page 17: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Graph Coloring

Find the smallest number of colors for coloring nodes such that no two adjacent nodes have the same color

E

A

DB

FC

E

A

DB

FC

Page 18: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Graph Coloring

What about this graph?

Page 19: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Graph Coloring

Like TSP and Hamiltonian Cycle, large graphs get really hard to find the smallest coloring for

It might not be obvious, but graph coloring has practical applications

Perhaps the most common example is register allocation inside of a compiler

Page 20: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Maximum Clique

Find the largest complete subgraph in a given graph

This graph has a clique of size 3

E

A

DB

FC

Page 21: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Maximum Clique

What about this graph? This graph has a clique of size 4 As before, a large graph can be very

difficult

E

A

DB

FC

Page 22: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Knapsack problem Not all NP-complete problems are graph

problems The knapsack problem is the following:

Imagine that you are Indiana Jones You are the first to open the tomb of some long-lost

pharaoh You have a knapsack that can hold m pounds of loot,

but there's way more than that in the tomb Because you're Indiana Jones, you can instantly tell

how much everything weighs and how valuable it is You want to find the most valuable loot that weighs

less than or equal to m pounds

Page 23: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Subset sum

This one is a little bit mathematical Say you have a set of numbers Somebody gives you a number k

Is there any subset of the numbers in your set that add up to exactly k?

Example: Set: { 3, 9, 15, 22, 35, 52, 78, 141} Is there a subset that adds up to exactly

100? What about 101?

Page 24: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Fabulous Cash Prizes

These NP-complete problems are very hard

Many of them are really useful Especially if you are a lazy traveling

salesman Clay Mathematics Institute has

offered a $1,000,000 prize You can do one of two things to

collect it: Find an efficient solution to any of the

problems Prove that one cannot have an efficient

solution

Page 25: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

A Few Finer Points…

Page 26: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Turing machine A Turing machine is a mathematical model

for computation It consists of a head, an infinitely long tape,

a set of possible states, and an alphabet of characters that can be written on the tape

A list of rules saying what it should write and should it move left or right given the current symbol and state

1 0 1 1 1 1 0 0 0 0

A

Page 27: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Turing machine example

3 state, 2 symbol “busy beaver” Turing machine:

Starting state A

Tape Symbo

l

State A State B State C

Write Move Next Write Move Next Write Move Next

0 1 R B 0 R C 1 L C

1 1 R HALT 1 R B 1 L A

Page 28: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Church-Turing thesis

If an algorithm exists, a Turing machine can perform that algorithm

In essence, a Turing machine is the most powerful model we have of computation

Power, in this sense, means the ability to compute some function, not the speed associated with its computation

Page 29: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

NP

NP is actually a class of problem Problems in NP can be solved in

polynomial time on a non-deterministic computer

A deterministic computer is the kind you know: First it has to consider possibility A, then,

it can consider possibility B

Page 30: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Deterministic vs. non-deterministic

A non-deterministic computer (which, as far as we know, only exists in our imagination) can consider both possibility A and possibility B at the same timeDeterministi

c

A B C D E F

A

B

C

D

E

F

Non-Deterministi

c

Page 31: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

P P is the class of decision problems that can

be solved in polynomial time by a deterministic computer

Lots of great problems are in P: Is this list sorted? Is this number prime? Is the largest number in this B-tree equal to 38?

Many problems are unknown: Does this number have exactly two factors? Is this graph equivalent to this other graph?

Page 32: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

NP-complete

Everything in P is also in NP Some problems are the “hardest”

problems in NP This means that any problem in NP

can be converted into one of these problem in polynomial time

These problems make up the class NP-complete

Page 33: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Decisions, decisions

Notice that P, NP, and NP-complete are all decision problems

So, the TSP we stated is not technically NP-complete

The NP-complete version is: Is there a tour of length less than or

equal to 24 in this graph? The optimization versions of NP-

complete problems are called NP-hard

Page 34: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Complexity hierarchy EXPSPACE

Exponential space EXP

Exponential time on a deterministic Turing machine

PSPACE Polynomial space

NP Polynomial time on a non-

deterministic Turing machine P

Polynomial time on a deterministic Turing machine

L Logarithmic space on a

deterministic Turing machine

EXPSPACE

P

L

Page 35: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

RE Co-RE

But wait it gets worse!

There are also problems that cannot be solved by any computer (or algorithm) in a finite amount of time

Problems in R can be solved by an algorithm in a finite amount of time Problems in RE can reach a "yes" answer in a finite amount of time, but no

algorithm is guaranteed to reach completion if the answer is "no" Problems in Co-RE can reach a "no" answer in a finite amount of time, but

no algorithm is guaranteed to reach completion if the answer is "yes"

R

EXPSPACE

R is the set of recursive decision problems

RE is the set of recursively enumerable decision problems

Co-RE is the complement of set RE

Page 36: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Halting problem

Given a Turing machine and input x, does it reach the halt state?

It turns out that this problem is undecidable

That means that there is no algorithm that can be to determine if any Turing machine will go into an infinite loop

Consequently, there is no algorithm that can take any program and check to see if it goes into an infinite loop

Page 37: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Post correspondence problem Given two finite lists of words A and B, can you pick k words

(repetitions allowed) from A and k words (repetitions allowed) from B so that the words from A concatenated are exactly the same string as the words from B concatenated

Example:

Solution: aa + bb + bb = aabbbb = a + a + bbbb

The Post correspondence problem (PCP) is undecidable (there is no algorithm that can solve all instances of it)

A Baa bbbbaba abb abba

Page 38: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Upcoming

Page 39: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Next time…

Elementary sorts Merge sort Quicksort Priority queues Heapsort

Page 40: Week 13 - Monday.  What did we talk about last time?  B-trees  Hamiltonian tour  Traveling Salesman Problem

Reminders

Work on Project 4Work on Assignment 6 Read sections 2.1 through 2.4