<<

CS 2230 CS II: Data structures Graphs: From shortest to shortest round trip, NP-completeness Brandon Myers University of Iowa Today’s big ideas

• Spanning trees • Minimum spanning trees (MST)

MST is a very important problem with many applications. but we’re going to talk about another problem instead...

Traveling salesman problem and other HARD problems Motivation for today’s topic A couple classes ago, we asked “can you think of a real- life situation where solving the shortest paths problem would be useful?” several students mentioned “traveling salesman” or “finding the shortest route that visits all destinations” Different problems shortest path problem: for each vertex, u, find the shortest path from a starting vertex to u

For their express service, UPS might want to know... “for each pickup location what is the quickest way to get there from the facility?” traveling salesman problem: find the shortest route from a starting vertex that visits all vertices

For efficiency purposes, UPS might want to know... “what is the quickest route for one truck to make pickups at all the pickup locations?” Brain experiment

1. pair up: one person is “computer” the other “timer” 2a. Timer times how long it takes computer to do each task 2b. Computer does two tasks in a specified order left half of the room: do task X then task Y

right half of the room: do task Y then task X

Timer will write down the observed time for task X and time for task Y task X. For every vertex, find the shortest path from A to that vertex task Y. Find the shortest route starting at A that visits every vertex A 10 B left side of room: 1. time task X 4 2. time task Y 7 3 right side of room: 4 1. time task Y 2 9 2. time task X C 8 E

9 20

D Submit your data

https://b.socrative.com/login/student/ CS2230A ids 1000-4999 CS2230B ids 5000+ Compute the shortest path from A to every other vertex

A 10 B

Using Djikstra’s algorithm 4 7 3 O(|E|+|V|log|V|) 4 for a complete graph like 2 9 C the one here, |E|=|V|2 8 E so in that case O(|V|2) 9 20

D

(each color denotes the shortest path from A to another vertex) Compute the shortest route from A that visits every vertex

A 10 B : A 10 B always take the 4 cheapest edge that 4 7 7 3 discovers a new 3 4 4 vertex 2 2 9 9 C C 8 8 E E 9 20 9 20 D D cost=32 A 10 B

4 7 3 here’s a cheaper 4 route that the 2 9 C greedy algorithm 8 would not have E as we traverse the graph, how found 20 do we know what the right next 9 cost=24 choice is? Do we have to try D every possible route? Different difficulty

shortest path problem: find the shortest path from a starting vertex to each other vertex

can be solved in “polynomial time”, that is O(nc) for some constant c

traveling salesman problem: find the shortest route from a starting vertex that visits all vertices

no known “polynomial time” solution A slightly easier version of TSP

The optimization version of TSP: find the shortest route from a starting vertex that visits all vertices

The “probably-easier” decision version of TSP (let’s call it dTSP): given a length L, determine whether a route of length less than L that visits all vertices exists Decision problems

• e.g., in dTSP • The decision problem is “does a route of length < L exist that visits all vertices?” • has a yes/no answer • the proof for a “yes” answer: a route with length

• e.g., in block puzzle solver, • The decision problem is “does a sequence of moves exist that transforms the initial Tray into the goal state?” • has a yes/no answer • the proof for a “yes” answer: a sequence of moves Decision problems A complexity class is a set of problems with known solutions bounded by some running time

The class P is the set of problems that can be solved in polynomial time, that is O(nc)

The class NP is the set of decision problems whose proof can be verified in polynomial time Decision problems The class NP is the set of decision problems whose proof can be verified in polynomial time

proof of “yes” algorithm for the decision if verifier running time ∈ � � then verifier problem (e.g., the decision problem is in NP TSP)

e.g., in block puzzle solver, the proof is the sequence of moves Difficulty

Many problems in NP do not have known polynomial time solutions, that is, they do not seem to be in P

decision TSP

NP

shortest paths

P Other problems

• decision TSP • 3-SAT • longest path NP • max independent set • • ...

P • shortest paths • sorting • inserts/deletes/find in all the data structures we’ve discussed • • ...

Problems that are in NP and known to be at least as hard as anything else in NP are called NP-complete NP-complete problems in general, solutions to NP-complete problems have exponential running time! O(2n)

This basically means you may be checking every possible configuration to find the answer how many configurations of dTSP in a complete graph? pick the first edge, pick the second edge, ..., pick edge that finishes the route |E| * |E| * ... * |E| = |E||V| Moore’s law to the rescue...?

Moore’s law predicted the number of transistors on a chip will double every ~2 years A implication of Moore’s law was 2x speed every ~2years (exponential improvement!!!) So if in 2000 you could solve TSP on a graph of size 30 in 100 seconds... • in 2002 you could solve TSP on a graph of size 30 in 50 seconds • or, you could solve TSP on a graph of size 31 in 100 seconds... LLL Even with exponential increases in performance, we can only increase the problem size by 1 every 2 years.

(speed increases no longer continue anyway...which only further hurts the case for Moore’s law addressing hard problems) NP-completeness in practice?

So does UPS give up on “what is the quickest route for one truck to make pickups at all the pickup locations?”

• No, for specific variants of a problem, algorithms exist that are likely to solve a reasonably sized problem in a reasonable amount of time

• Also a class of algorithms called randomized algorithms that can find fraction-of-optimal solutions in reasonable amount of time The P vs. NP question • No one has proven to date P=NP or P!=NP • In fact, there are many results that say producing such a proof is, itself, a difficult problem

NP P is in NP but has never been P proven whether or not P ≠ �� • Most computer scientists expect that P!=NP • But, if it turned out P=NP, the world would get interesting very quickly • No significant difference in difficulty between discovering and verifying a solution to a decision problem