Traveling Salesman Problem (TSP) - Visit every city and...

33
Traveling Salesman Problem (TSP) - Visit every city and then go home.

Transcript of Traveling Salesman Problem (TSP) - Visit every city and...

Page 1: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Traveling Salesman Problem (TSP)

- Visit every city and then go home.

Page 2: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Seattle

L. A.

Boston

Detroit

Toledo Cleveland

Columbus

Atlanta

Orlando

New Orleans

Page 3: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A Hamiltonian Path is a path that goes through each vertex exactly once.

Note: Not all edges have to be used.

A B

C D

E F

Page 4: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Does this graph have a Hamiltonian path?A

B C

D E

F

Page 5: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A Hamiltonian Circuit is a Hamiltonian path that starts and ends at the same vertex.

This is the traveling salesman problem.

AB C

D E

AB C

D E

Page 6: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

For any given graph, finding an Euler path will be easier than finding a Hamiltonian path.

Page 7: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Pop Quiz!

An Euler path visits every ____ once

1) vertex2) edge3) something else

Page 8: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Euler path/circuit = cross each edge once

Hamiltonian path/circuit = cross each vertex once

Page 9: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 9

• Examples:

Page 10: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Let's find K6

Page 11: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

Since every vertex is visited, it doesn't matter where you start.

Let's start from A.

Page 12: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

Page 13: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

Page 14: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

For K5 we have 5 verticies A,B,C,D,E

Since we visit every vertex, we can start at A.

There are 4 verticies left. All can be reached.

From there 3 are left (all reachable).Then 2 are left, then one.Then we take the edge back to A.

So there are 4x3x2x1=24 possible paths.

Page 15: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 15

Finding Hamilton Circuits

Page 16: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Which Hamiltonian circuit is better?

S.F.

L.A.

Detroit

Toledo

S.F.

L.A.

Detroit

Toledo

Page 17: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

In a Traveling Salesman Problem, not only do you want to visit each city exactly once, but you also want to do so as efficiently as possible.

This will be measured by the total distance for the trip.

Page 18: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

The weight of an edge is a number assigned to the edge. (Think distance between cities.)

A graph is a weighted graph if all of its edges have weights.

AB C

D E

What is the weight of a) A,B b) C,B c) C,D - no edge, no weight

1

74

2

83

5

Page 19: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

The weight of a path is the sum of the weights of the edges along the path.

AB C

D E

1

74

2

83

5

What is the weight of the path B,A,D,E ?

Page 20: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Q: What is the relation between the weight of a path and the weight of its reverse path? Why?

A:

Page 21: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

Q: What is the relation between the weight of a path and the weight of its reverse path? Why?

A: The are the same.

The path and its reverse path use the same edges.

(So direction doesn't matter for finding the weight of a path.)

Page 22: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

AB C

D E

1

74

2

83

5

AB C

D E

1

74

2

83

5

Which is the circuit with the smaller weight?

That one is the solution to the Traveling Salesman Problem.

Page 23: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 23

Solving the TSP by Brute Force

• Brute Force means trying all possible outcomes and then picking the best one.

• In solving a TSP problem by brute force, we consider all possible Hamiltonian circuits.

Page 24: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 24

Solving the TSP by Brute Force• Example: Use the weighted graph to find the

sequence of cities for Danielle to visit that will minimize her total travel cost.

Page 25: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 25

Solving the TSP by Brute Force• Solution: Use brute force to explore all possible

circuits:

Page 26: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 26

Solving the TSP by Brute Force

Page 27: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

2

4

3 7

5

6

Paths are:

ABCDA (reverse ADCBA)

ACBDA (reverse ADBCA)

ABDCA (reverse ACDBA)

Page 28: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 28

The Nearest Neighbor Algorithm• There are algorithms that give good

approximations to the TSP.

Page 29: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

2

4

3 7

5

6

Start from A

B,C,D remain, B is closest.

C,D remain, C is closest.

D remains.

Lastly go back to A.

Page 30: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 30

The Nearest Neighbor Algorithm• Example: Use the nearest neighbor algorithm

to schedule Danielle’s trip.• Solution:

Page 31: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 31

The Best Edge Algorithm

Page 32: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

A B

C D

2

4

3 7

5

6

2 is the smallest weight, add it.

3 is the next smallest, add it.

4 is the next smallest, add it.

5 is next, can't add, triple at C.

6 is next, can't add, triple at A.

7 is next, add it and get the circuit!

Page 33: Traveling Salesman Problem (TSP) - Visit every city and ...math.utoledo.edu/~dgajews/1180_old/4.2-TSP.pdf · In a Traveling Salesman Problem, not only do you want to visit each city

© 2010 Pearson Education, Inc. All rights reserved. Section 4.2, Slide 33

The Best Edge Algorithm• Example: Use the best edge algorithm to

schedule Danielle’s trip.• Solution:

210 + 230 + 170 + 240 + 350 = 1200Notice that this circuit has a weight of 1,200, which also makes it the best solution to Danielle’s problem.