Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
five thirty-seven
Introduction
Lecture 21:
Permutations and Backtracking
Slide 1
five thirty-seven
five thirty-seven
Generating Permutations
Slide 4
Print-Perms(A, i)
1 if i = Length(A)
2
print A
3 else for j = i to Length(A) 1
4
swap A[i] and A[j]
5
Print-Perms(A, i + 1)
6
swap A[i] and A[j]
Note that a typical call is of the
form Print-Perms([1, 2, 3], 0),
but that portions of the input
array can be left immune to
permutation by choosing a second
argument i > 0.
Print-Perms([1, 2, 3], 2)
[1, 2, 3]
Print-Perms([1, 2, 3], 1)
[1, 2, 3]
[1, 3, 2]
Print-Perms([1, 2, 3], 0)
[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1] not lexicographic
[3, 1, 2]
five thirty-seven
five thirty-seven
Slide 5
Slide 7
five thirty-seven
five thirty-seven
Euclidean TSP
Slide 8
five thirty-seven
five thirty-seven
Slide 9
five thirty-seven
five thirty-seven
Slide 10
Slide 11
$ cat tsp10.txt
0.70501 0.58793
0.26077 0.84765
0.12284 0.19949
0.20125 0.85198
0.48505 0.08244
0.94810 0.30570
0.69991 0.32967
0.15261 0.59770
0.56046 0.39271
0.80336 0.23430
Slide 12
A 12-city problem?
five thirty-seven
five thirty-seven
Running Times
Slide 13
Slide 14
Performance Comparison
Slide 15
five thirty-seven
five thirty-seven
Tspwb(P , A, i)
// points, order, level
1 if i = Length(A)
2
curr Tour-Length(A, P , Length(A))
3
if curr < cand[0]
4
cand (curr, Copy(A))
5 else for j = i to Length(A) 1
6
swap A[i] and A[j]
7
if Tour-Length(A, P , i) < cand[0] // branch only if
8
Tspwb(P , A, i + 1)
// improvement possible
9
swap A[i] and A[j]
Comments on Backtracking
Slide 16
Year
Researchers
1954
1971
1975
100
1977
Grotschel
120
1980
318
1987
532
1987
666
1994
7,397
1998
13,509
2001
15,112
2005
33,810
five thirty-seven
five thirty-seven
Slide 17
Slide 19
five thirty-seven
five thirty-seven
Slide 18
Slide 20