没有幻灯片标题 - Zhejiang University
Download
Report
Transcript 没有幻灯片标题 - Zhejiang University
§5 Backtracking Algorithms
A sure-fire way to find the answer to a problem is to make a
list of all candidate answers, examine each, and following the
examination of all or some of the candidates, declare the identified
answer.
Backtracking enables us to eliminate the explicit examination
of a large subset ofSuuuure
the candidates
guaranteeing
— if thewhile
list isstill
finite
and it is that the
answer will be found
if the
algorithm
run to following
termination.
possible
to identify
theisanswer
the examinations. AND, if there are
The basic idea isnot
that
we have a partial solution ( x1, ... ,
toosuppose
many candidates.
xi ) where each xk Sk for 1 k i < n. First we add xi+1 Si+1
and check if ( x1, ... , xi, xi+1 ) satisfies the constrains.
If the answer
pruning
is “yes” we continue to add the next x, else we delete xi and
backtrack to the previous partial solution ( x1, ... , xi1 ).
1/13
§5 Backtracking Algorithms
1. Eight Queens
8 7 6 5 4 3 2 1
Find a placement of 8 queens on an 8 8 chessboard such that no
two queens attack.
Two queens are said to attack iff they are in the same row, column,
diagonal, or antidiagonal of the chessboard.
1 2 3 4 5 6 7 8
Q
Q
Q
Q
Q
Q
Q
Q
Qi ::= queen in the i-th row
xi ::= the column index in which Qi is
This implies
Solution that
= ( xthe
1, x
2, ... , xmust
8)
solution
= ( 4, 6, 8, 2,
1,...3,, 8.
5)
be a permutation
of 7,
1, 2,
Thus
theimplies
number
candidates
8of
This
8
candidates
For the problem
with space
n queens,
in
the
solution
inare
the n!
solution
space.
there
candidates
is reduced
to 8!.
in the solution space.
Constrains: Si = { 1,2,3,4,5,6,7,8 } for 1 i 8
xi xj if i j
2/13
( xi xj ) / (i j ) 1
§5 Backtracking Algorithms
Method: Take the problem of 4 queens as an example
Step 1: Construct a game tree
1
x1 = 1
2
2
x2 = 2
3
3
x3 = 3
4
6
4
1
13
42
3 3
34
3
4
24
29
19
4 1
4 1
1
2
4
40
45
35
3 2
50
4 1
4 1
1
2
3
56
61
51
2 2
3 1
3 1
4 3 4 1 3 1
4 2 4 1 2 1
3 2 3 1 2 1
7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
4 ! leaves
Each path from the root to a leaf defines an element of the solution space.
3/13
2
9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
x4 = 4 3 4 2 3 2
5
4
18
8
4 2
3
§5 Backtracking Algorithms
1
x1 = 1
2
2
x2 = 2
3
3
x3 = 3
4
6
4
1
13
42
3 3
34
3
4
24
29
19
4 1
4 1
1
2
4
40
45
35
3 2
50
4 1
4 1
1
2
3
56
61
51
2 2
3 1
3 1
4 3 4 1 3 1
4 2 4 1 2 1
3 2 3 1 2 1
7 10 12 15 17 21 23 26 28 31 33 37 39 42 44 47 49 53 55 58 60 63 65
Step 2: Perform a depth-first search ( post-order traversal ) to
examine the paths
( 2, 4, 1, 3 )
Note: No tree is actually constructed. The game
tree is just an abstract concept.
4/13
2
9 11 14 16 20 22 25 27 30 32 36 38 41 43 46 48 52 54 57 59 62 64
x4 = 4 3 4 2 3 2
5
4
18
8
4 2
3
Q Q
Q Q Q Q
Q Q Q Q
Q
§5 Backtracking Algorithms
2. The Turnpike Reconstruction Problem
Given N points on the x-axis with coordinates x1 < x2 < …< xN .
Assume that x1 = 0. There are N ( N – 1 ) / 2 distances between
every pair of points.
Given N ( N – 1 ) / 2 distances. Reconstruct a point set from the
distances.
Home work:
p.420
10.41
〖Example〗Given D = { 1, 2, 2, 2, 3, 3, 3, 4, 5, 5, 5, 6, 7, 8, 10 }
Homometric point sets
Step 1: N ( N – 1 ) / 2 = 15 implies N = ?6
x5 = 8
Step 2: x1 = 0 and x6 = 10
Step 3: find the next largest
distance and check
x4 = 7
x3 = 6
x2 = 3
x2 = 4 x3 = 4
( 0, 3, 5, 6, 8, 10 )
Please read Figure 10.64 and 10.65 for details.
5/13
x2 = 2
x4 = 6
x3 = 5
Research Project 14
Review of
Programming Contest Rules (25)
Given the ACM-ICPC's rule of scoring, your task is
to write a program to find yourself the order of solving
the problems which gives you the best chance of winning
the contest.
Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/
6/13
Research Project 15
Good-Bye Party (25)
In every summer there are students graduating from school. A lot of
graduating students would like to host their parties to say good-bye to their
classmates and friends. This kind of activity is called "bg" on our BBS.
Attending different parties will give you different feelings. You may
assign each party a degree of happiness which is a non-negative integer that
shows how you feel about attending to that party.
Now you are given a list of parties containing the length of each party
and the time on which the host will be leaving school. It would be nice to
schedule the parties so that you can obtain maximum of happiness. Since
there could be as many as 30 parties on the list, you'll need the computer to
help you.
Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/
7/13
§3 Dynamic Programming
Use a table instead of recursion
1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2)
int Fib( int N )
{
if ( N <= 1 )
return 1;
else
return Fib( N - 1 ) + Fib( N - 2 );
}
T(N) T(N – 1) + T(N – 2)
T(N) F(N)
8/13
§3 Dynamic Programming
F6
F5
F4
F4
F3
F2
F1
F3
F2
F1 F1
F2
F0 F1
F3
F1
F0
F2
F1
F2
F1 F1
F0
F0
F0
Trouble-maker: The growth of redundant calculations is explosive.
Solution: Record the two most recently computed values to avoid
recursive calls.
int Fibonacci ( int N )
{ int i, Last, NextToLast, Answer;
if ( N <= 1 ) return 1;
Last = NextToLast = 1; /* F(0) = F(1) = 1 */
for ( i = 2; i <= N; i++ ) {
Answer = Last + NextToLast; /* F(i) = F(i-1) + F(i-2) */
NextToLast = Last; Last = Answer; /* update F(i-1) and F(i-2) */
} /* end-for */
return Answer;
}
T(N) = O(N)
9/13
§3 Dynamic Programming
2. Ordering Matrix Multiplications
〖Example〗 Suppose we are to multiply 4 matrices
M1 [ 1020 ] M2 [ 2050 ] M3 [ 501 ] M4 [ 1100 ] .
If we multiply in the order
M1 [ 1020 ] ( M2 [ 2050 ] ( M3 [ 501 ] M4 [ 1100 ] ) )
Then the computing time is
50 1 100 + 20 50 100 + 10 20 100 = 125,000
If we multiply in the order
( M1 [ 1020 ] ( M2 [ 2050 ] M3 [ 501 ] ) ) M4 [ 1100 ]
Then the computing time is
20 50 1 + 10 20 1 + 10 1 100 = 2,200
Problem: In which order can we compute the product of n matrices
with minimal computing time?
10/13
§3 Dynamic Programming
Let bn = number of different ways to compute M1 M2 Mn. Then
we have b2 = 1, b3 = 2, b4 = 5,
Let Mij = Mi Mj . Then M1n = M1 Mn = M1i Mi+1 n
n 1
bn bi bn i whe reIfnj
1= and
b1 the
1only
.
–
i
k
,
then
bn
bO(i Nb2 n)i
i 1
There
valuesare
Monly
required
to
xy
values ofMMijsatisfy
.
compute
ij
n
bn O( n4 n )
/* Catalan number
y – x < k */
.
Suppose we are to multiply n matrices M1 Mn
where Mi is an ri1 ri matrix. Let mij be the cost of the optimal way
to compute Mi Mj . Then we have the recurrence
equations:
if i j
0
m ij
m in{ m il m l 1 j ri 1 rl r j } i f j i
il j
11/13
§3 Dynamic Programming
/* r contains number of columns for each of the N matrices */
/* r[ 0 ] is the number of rows in matrix 1 */
/* Minimum number of multiplications is left in M[ 1 ][ N ] */
void OptMatrix( const long r[ ], int N, TwoDimArray M )
{ int i, j, k, L;
long ThisM;
for( i = 1; i <= N; i++ ) M[ i ][ i ] = 0;
for( k = 1; k < N; k++ ) /* k = j - i */
for( i = 1; i <= N - k; i++ ) { /* For each position */
j = i + k; M[ i ][ j ] = Infinity;
for( L = i; L < j; L++ ) {
ThisM = M[ i ][ L ] + M[ L + 1 ][ j ]
m1 , N
+ r[ i - 1 ] * r[ L ] * r[ j ];
m1 , N 1 m 2 , N
if ( ThisM < M[ i ][ j ] ) /* Update min */
M[ i ][ j ] = ThisM;
m1 , 2
m2,3
} /* end for-L */
m1 ,1
m2,2
} /* end for-Left */
T(N) = O(N3 )
}
m N 1, N
m N 1, N 1
i f i j 10.46 on p.388
0 ordering please refer to Figure
To record the
12/13
m ij
m in{ m il m l 1 j ri 1 rl r j } i f j i
il j
mN ,N
Research Project 16
Diff (25)
In computing, diff is a file comparison utility that
outputs the differences between two files. It is typically
used to show the changes between a file and a former
version of the same file. Diff displays the changes made
per line for text files.
Now you are supposed to simulate the diff operation.
Detailed requirements can be downloaded from
http://acm.zju.edu.cn/dsaa/
13/13