Chapter 6 the Traveling Salesman Problem
Transcript of Chapter 6 the Traveling Salesman Problem
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 1/62
1
Excursions in ModernMathematics
Sixth Edition
Peter Tannenbaum
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 2/62
2
Chapter 6The Traveling Salesman Problem
Hamilton Joins theCircuit
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 3/62
3
The Traveling Salesman ProblemOutline/learning Objectives
To identify and model Hamilton circuit
and Hamilton path problems. To recognize complete graphs and state
the number of Hamilton circuits that they
have. To identify traveling-salesman problems
and the difficulties faced in solving them.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 4/62
4
The Traveling Salesman ProblemOutline/learning Objectives
To implement brute-force, nearest-
neighbor, repeated nearest-neighbor, andcheapest-link algorithms to findapproximate solutions to traveling ±salesman problems.
To recognize the difference betweenefficient and inefficient algorithms.
To recognize the difference between
optimal and approximate algorithms.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 5/62
5
The Traveling Salesman Problem
6.1 Hamilton Circuits andH
amilton Paths
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 6/62
6
The Traveling Salesman Problem
Hamilton pathHamilton path
A path that visits each vertex of the graph onceand only once.
Hamilton circuitHamilton circuit
Acircuit that visits each vertex of the graphonce and only once (at the end, of course, the
circuit must return to the starting vertex).
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 7/62
7
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (a) shows a graph that has Euler
circuits and has Hamilton circuits. One
such Hamilton circuit is A, F, B, C, G,
D, E, A. Note that once a graph has a
Hamilton circuit, it automatically has aHamilton path-- The Hamilton circuit
can always be truncated into a Hamilton
path by dropping the last vertex of the
circuit.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 8/62
8
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (b) shows a graph that has no
Euler circuits but does have Euler paths
(for example C, D, E, B, A, D), has no
Hamilton circuits (sooner or later you
have to go to C, and then you are stuck) but does have Hamilton paths (for
example, A, B, E, D, C ). Aha, a graph
can have a Hamilton path but no
Hamilton Circuit!
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 9/62
9
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (c) shows a graph that has
neither Euler circuits nor paths (it has
four odd vertices), has Hamilton circuits
(for example A, B, C, D, E, A ± there are
plenty more), and consequently hasHamilton paths (for example, A, B, C,
D, E ).
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 10/62
10
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (d) shows a graph that has Euler
circuits (the vertices are all even), has
no Hamilton circuits (no matter what,
your are going to have to go through E
more than once!) but has Hamilton paths (for example, A, B, E, D, C ).
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 11/62
11
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (e) shows a graph that has no
Euler circuits but has Euler paths ( F and
G are the two odd vertices), had neither
Hamilton circuits nor Hamilton paths.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 12/62
12
The Traveling Salesman Problem
Hamilton Circuit (Paths vs Euler Circuit Path
Figure (f) shows a graph that has
neither Euler circuits nor Euler paths
(too many odd vertices), has neither
Hamilton circuits nor Hamilton paths.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 13/62
13
The Traveling Salesman Problem
The lesson in the previous Example is that the existence
of an Euler path or circuit in a graph tells us nothing about
the existence of a Hamilton path or circuit in that graph.
This is important because it implies that Euler¶s circuit
and path theorems from Chapter 5 are useless when it
comes to Hamilton circuits and paths.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 14/62
14
The Traveling Salesman Problem
There are, however, nice theorems that identify special
situations where a graph must have a Hamilton circuit.
This best known of these theorems is Dirac¶s theorem:
If a connected graph has N vertices (N > 2) and
all of them have degree bigger or equal to N / 2,
then the graph has a Hamilton circuit.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 15/62
15
The Traveling Salesman Problem
6.2 Complete Graphs
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 16/62
16
The Traveling Salesman Problem
If a graph has a Hamilton circuit, then how
many different Hamilton circuits does a ithave?
A graph with N vertices in which every pair of distinct vertices is joined by an edge is called a
complete graph on N vertices and denoted bythe symbol K N .
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 17/62
17
The Traveling Salesman Problem
Number of Edges inNumber of Edges inK K
N N
K N has N (N ± 1)/2 edges.
Of all graphs with N vertices and no multipleedges or loops, K N has the most edges.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 18/62
18
The Traveling Salesman Problem
Hamilton Circuits in K 4
If we travel the four vertices of K 4 in anarbitrary order, we get aHamilton path. For example, C, A, D, B is aHamilton path.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 19/62
19
The Traveling Salesman Problem
Hamilton Circuits in K 4
D, C, A, B is another Hamilton Path.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 20/62
20
The Traveling Salesman Problem
Hamilton Circuits in K 4
Each of these Hamiltonpaths can be closed intoa Hamilton circuit-- the
path C, A, D, B begetsthe circuit D, A, D, B, C .
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 21/62
21
The Traveling Salesman Problem
Hamilton Circuits in K 4
The path D, C, A, B
begets the circuit D, C, A,
B, D.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 22/62
22
The Traveling Salesman Problem
Hamilton Circuits in K 4
It is important toremember that the sameHamilton circuit can bewritten in many ways.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 23/62
23
The Traveling Salesman Problem
Hamilton Circuits in K 4
For example, C, A, D, B,C is the same circuit as A,D, B, C, A ± the only
difference is that in thefirst case we used C asthe reference point in thesecond case we used A.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 24/62
24
The Traveling Salesman Problem
Number of Hamilton Circuits inNumber of Hamilton Circuits in K K N N
There are (N ± 1)! Distinct Hamiltoncircuits in K N .
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 25/62
25
The Traveling Salesman Problem
6.3 TravelingSalesmanProblems
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 26/62
26
The Traveling Salesman Problem
The ³ traveling salesman´
is a convenient metaphor for many differentimportant real-lifeapplications, all involving
Hamilton circuits incomplete graphs but onlyoccasionally involvingsalespeople.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 27/62
27
The Traveling Salesman Problem
Any graph whose edges
have numbers attached tothem is called a weighted
graph, and the numbersare called the weights of
the edges. The graph iscalled a complete
weighted graph.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 28/62
28
The Traveling Salesman Problem
The problem we want to
solve is fundamentally thesame± find an optimal
Hamilton circuit (a
Hamilton circuit with least
total weight) for the givenweighted graph.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 29/62
29
The Traveling Salesman Problem
6.4 SimpleStrategies for Solving TSPs
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 30/62
30
The Traveling Salesman Problem
± ± Strategy 1 (ExhaustiveStrategy 1 (Exhaustive
Search)Search)
Make a list of all possibleHamilton circuits. For eachcircuit in the list, calculate
the total weight of thecircuit. From all thecircuits, choose the circuitwith smallest total weight.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 31/62
31
The Traveling Salesman Problem
± ± Strategy 2 (Go Cheap)Strategy 2 (Go Cheap)
Start from the home city.From there go to the citythat is the cheapest to getto. From each new city go
to the next city that ischeapest to get to. Whenthere are no more new citiesto go to, go back home.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 32/62
32
The Traveling Salesman Problem
6.5 The Brute-Force and NearestNeighbor Algorithms
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 33/62
33
The Traveling Salesman Problem
The Ex haustive Search strategy can be
formalized into an algorithm generally knownas the brute-force algorithm; the Go Cheap
strategy can be formalized into an algorithmknown as the nearest-neighbor algorithm.
In both cases, the objective of the algorithm isto find an optimal (cheapest, shortest, fastest)Hamilton circuit in a complete weightedgraph.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 34/62
34
The Traveling Salesman Problem
Algorithm 1: The BruteAlgorithm 1: The Brute--Force AlgorithmForce Algorithm
Step 1. Make a list of all the possibleHamilton circuits of the graph.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 35/62
35
The Traveling Salesman Problem
Algorithm 1: The BruteAlgorithm 1: The Brute--Force AlgorithmForce Algorithm
Step 2. For each Hamilton circuitcalculate its total weight (add the
weights of all the edges in the circuit).
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 36/62
36
The Traveling Salesman Problem
Algorithm 1: The BruteAlgorithm 1: The Brute--Force AlgorithmForce Algorithm
Step 3. Choose an optimal circuit(there is always more than one optimal
circuit to choose from!).
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 37/62
37
The Traveling Salesman Problem
Algorithm 2: The NearestAlgorithm 2: The Nearest--Neighbor AlgorithmNeighbor Algorithm
Start. Start at the designated startingvertex. If there is no designated
starting vertex, pick any vertex.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 38/62
38
The Traveling Salesman Problem
Algorithm 2: The NearestAlgorithm 2: The Nearest--Neighbor AlgorithmNeighbor Algorithm
First step. From the starting vertexgo to its nearest neighbor (the vertex
for which the corresponding edge hasthe smallest weight.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 39/62
39
The Traveling Salesman Problem
Algorithm 2: The NearestAlgorithm 2: The Nearest--Neighbor AlgorithmNeighbor Algorithm
Middle steps. From each vertex goto its nearest neighbor, choosing onlyamong the vertices that haven¶t been
yet visited. (If there is more than one,choose at random). Keep doing thisuntil all the vertices have been visited.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 40/62
40
The Traveling Salesman Problem
The brute-force algorithm is a classic
example of what is formally known asan inefficient algorithm ± analgorithm for which the number of steps needed to carry it out growsdisproportionately with the size of the problem.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 41/62
41
The Traveling Salesman Problem
The nearest-neighbor algorithm is an
efficient algorithm. Roughly speaking,an efficient algorithm is an algorithm for which the amount of computational effortrequired to implement the algorithmgrows in some reasonable proportionwith the size of the input to the problem.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 42/62
42
The Traveling Salesman Problem
6.6 Approximate Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 43/62
43
The Traveling Salesman Problem
A really good algorithm for solving TSP¶s in generalwould have to be both efficient (like the nearest-neighbor)
and optimal (like the brute-force). Unfortunately, nobody
knows of such an algorithm.
We will use the term approximate algorithm to describeany algorithm that produces solutions that are, most of the
time, reasonably close to the optimal solution.
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 44/62
44
The Traveling Salesman Problem
6.7 The RepetitiveNearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 45/62
45
The Traveling Salesman Problem
Let X be any vertex.Find the nearest-neighbor circuit using X
as the starting vertex andcalculate the total cost of the circuit.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 46/62
46
The Traveling Salesman Problem
We compute the nearest-neighbor circuit with A asthe starting vertex, andwe got A, C, E , D, B, A
with a total cost of $773.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 47/62
47
The Traveling Salesman Problem
Repeat the process witheach of the other verticesof the graph as thestarting vertex.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 48/62
48
The Traveling Salesman Problem
If we use B as thestarting vertex, thenearest-neighbor circuittakes us from B to C,then to A, E, D, and backto B, with a total cost of $722.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 49/62
49
The Traveling Salesman Problem
Remember we must startand end the trip at A ±this very same circuitwould take the form A, E ,
D, B, C, A.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 50/62
50
The Traveling Salesman Problem
The process is onceagain repeated using C,D, and E as the startingvertices with respectivecosts of $722, $722, and$741.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 51/62
51
The Traveling Salesman Problem
Of the nearest-neighbor circuits obtained, keepthe best one. If there is adesignated startingvertex, rewrite the circuitusing that vertex as thereference point.
Algorithm 3: The Repetitive Nearest-Neighbor Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 52/62
52
The Traveling Salesman Problem
6.8 The Cheapest-Link Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 53/62
53
The Traveling Salesman Problem
Step 1. Pick thecheapest link (edge withsmallest weight)available. Among all theedges of the graph, the³cheapest link´ is edge
AC , with a cost of $119.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 54/62
54
The Traveling Salesman Problem
Step 2. Pick the nextcheapest link availableand mark it. In this caseedge C E with a cost of $120.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 55/62
55
The Traveling Salesman Problem
Step 3, 4, «, N -1Continue picking andmarking the cheapestunmarked link available
that does not(a) close a circuit, or
(b) create three edgescoming out of a single
vertex.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 56/62
56
The Traveling Salesman Problem
The next cheapest linkavailable is edge BC($121), but we should notchoose BC± we would
have three edges comingout of vertex C.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 57/62
57
The Traveling Salesman Problem
The next cheapest linkavailable is AE ($133),but we can¶t take AE
either-- the vertices A, C,
and E would form a smallcircuit.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 58/62
58
The Traveling Salesman Problem
The next cheapest linkavailable is BD ($150).Choosing BD would notviolate either of the two
rules, so we can add it toour budding circuit.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 59/62
59
The Traveling Salesman Problem
The next cheapest linkavailable is AD ($152)and it works just fine.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 60/62
60
The Traveling Salesman Problem
Step N . Connect the lasttwo vertices to close thered circuit. At this point,we have only one way to
close up the Hamiltoncircuit, edge BE .
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 61/62
61
The Traveling Salesman Problem
The Hamilton circuit cannow be described usingany vertex as thereference point. For A,
we describe it as A, C, E ,B, D, A with a total costof $741.
Algorithm 4: The CheapestAlgorithm 4: The Cheapest--Link AlgorithmLink Algorithm
8/8/2019 Chapter 6 the Traveling Salesman Problem
http://slidepdf.com/reader/full/chapter-6-the-traveling-salesman-problem 62/62
The Traveling Salesman Problem
Conclusion
How does one find an optimalHow does one find an optimal
Hamilton circuit in a completeHamilton circuit in a completeweighted graph?weighted graph?
The nearestThe nearest--neighbor and cheapestneighbor and cheapest--link algorithms are two fairly simplelink algorithms are two fairly simple
strategies for attacking TSPs.strategies for attacking TSPs. The search for an optimal and efficientThe search for an optimal and efficient
general algorithm.general algorithm.