?????(Backtracking) - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

?????(Backtracking)

Description:

(Backtracking) The Divide-and-Conquer Strategy ( ) binary Searching Quick Sort . The Greedy Method( ) Prim MST Kruskal ... – PowerPoint PPT presentation

Number of Views:76
Avg rating:3.0/5.0
Slides: 56
Provided by: CMHs8
Category:

less

Transcript and Presenter's Notes

Title: ?????(Backtracking)


1
?????(Backtracking)
2
  • The Divide-and-Conquer Strategy (????)
  • binary Searching?Quick Sort.
  • The Greedy Method(?????)
  • Prim MST?Kruskal MST?Djikstra's algorithm
  • Dynamic Programming(?????)
  • ??????????????????
  • Trace Back(??)
  • ????????????.
  • Branch-and-Bound (????)

3
??
  • ?????????????????
  • ???????????????????????,???????????????
  • ??,n-Queen??????? n ??????? n?n
    ??????????????????????n?????????????n2????????

4
  • ??????????(Depth-first search) ??????

5
Depth-First Search
1
6
depth_first_tree_search Algorithm
void depth_first_tree_search ( node v ) node
u visit v // ?? v for ( v ?????? u )
// ???????? depth_first_tree_search ( u )

7
???
V1
V2
V4
V3
V5
8
n4 ? n-Queen ??
?
?


?
?


????????????,????????? ???????????
9
4-Queen??????????
Start
lti, jgt ??? i ???? ??? j ??
1,1
1,2
1,3
1,4
2,1
2,1
2,2
2,3
2,4
3,1
3,2
3,3
3,4
4,1
4,2
4,3
4,4
4,1
4,2
4,3
4,4
??????????????????????????
10
??????
  1. ????????????????,??????????,??????????????
  2. ???(nonpromising)???????????????
  3. ???(promising)?????????????

11
??????(?)
  1. ??(pruning)???????nonpromising???,???????????
  2. ?????????(pruned state space tree)??????????????
    ?

12
???????? checknode
void checknode ( node v ) node u if (
promising ( v ) ) if ( v ??? ) ????
else for ( v ?????? u ) checknode ( u
)
????????
13
(No Transcript)
14
(No Transcript)
15
4-Queen ??????




?




?
?
?
?
?
?
?
?
?
?
?
?
?
?
??nonpromosing?? ????????,? ?????????? ??
16
?expand???checknode???
void expand ( node v ) node u for ( v
?????? u ) if ( promising ( u ) ) if ( ? u
??? ) ???? else expand ( u )
??????
?????????
17
The n-Queens Problem
Col(i) ? i ???? ?????? Col(3) 1, Col(6)
4 Col(6) - Col(3) 4 - 1 3 ( 6 - 3 ) Col(2)
8, Col(6) 4 Col(6) - Col(2) 4 - 8 -4 (
2 - 6 ) ???? Col(6) - Col(2) 4 ( 6 - 2 )
18
The Backtracking Algorithm for the n-Queens
Problem
void queens ( index i ) index j if
( promising (i) ) if ( i n ) cout ltlt col
1 ? col n else for ( j 1 j
lt n j ) col i 1 j //
????? (i1) ?? queens (i 1) // ???????
j ??
19
The Backtracking Algorithm for the n-Queens
Problem
bool promising ( index i ) index k
bool switch k 1 switch true
// ???????????? while ( k lt i
switch ) // ? i ???? if (col i col
k abs (col i - col k) i - k)
switch false k return switch

20
??
?????????????
???????????????? n 8 ?,
21
??
???? promising ???????,? n8 ????, ???????????????
,????????? ????????,????,??????????? ??????
???????
? promising ??
????? ??????? ???????????????
22
???????????? n-??????????????
N ??????????? ??????????? ???????? ?????promising ???
4 341 24 61 17
8 19,173,961 40,320 15,721 2057
12 9.73?1012 4.79?108 1.01?107 8.56?105
14 1.20?1016 8.72?108 3.78?108 2.74?107
???????????? ???????,???? n ! ????
? promising ?, expand ????? ??????
DFS
23
????????
???????????????????? ????????? ( n ??? )
?????,??? ???????????,??????????
????????????????????
24
Sum-of-Subsets ??
?? 5.2 ????????W???? ?? n 5,W 21 ? w15
w26 w310 w411 w516 ?? w1w2w3
5610 21 w1w5 516 21
w3w4 1011 21 ????? w1,w2,w3 ? w1,w5
? w3,w4 ? 0-1 ?????,???????,?????????
25
n ???,????????
?5.7 Sum-of-Subsets ??? n 3 ???????
26
n3,W6 ??????
???
27
Sum-of-Subsets ?????
???????????????? ?? wi1 ?? i ???????????? ?
weight ?? i ???????????? nonpromising
???? weight wi1 gt W ?? total ???????????
nonpromising ???? weight total lt W
28
?????????? n4?W13
????????????nonpromising
??????????????
29
????? Sum-of-Subsets ??
???? n ????(??)???????W,??????
??W??????? ????? n ,???????????
w,???W? ?????????W??????? void sum_of_subsets
( index i , int weight , int total ) if
( promising ( i ) ) //??? i ?????
promising if ( weight W ) cout ltlt
include1 ? include i //?? else
includei 1 "yes" //?? wi 1
??,????? sum_of_subsets ( i1 , weight
wi1, total - wi1 ) includei 1 "no"
//??? wi 1 ??,????? sum_of_subsets (
i1 , weight , total - wi1 )
30
(?)
bool promising ( index i ) return ( weight
total gt W ) ( weight W weight
wi1 lt W )
promising ?? (?????????? ??????? gt
W) ? (?????????? W) ? (??????????
?????????? lt W )
31
?????????
????? 1222...2n 2n1 - 1 (?? A.3) ???
Monte Carlo ????????????
32
????
??? 2-??????, ?? 3-??????(6?)? m-????????? ?????
?????? ??????? ??? m ???? ?? m ???????? ????????
V1
V2
V3
V4
33
????(Planar)
??
Planar
???????????????????,???Planar?
??(V1,V5)?(V2,V4)????? Planar?
34
???????3-????
0 1 1 1
1 0 1 0
1 1 0 1
1 0 1 0
??? ??
????
35
? m-????(?????)
??????????,?? m ??????????
??,??????????????? ????? n ? m,??? n ????????
(??? ?????)? ?????????,??? m ????
????????? 1 ? n ? vector
???,vectori ?????? i ?????? (??? 1 ? m)?
36
void m_coloring ( index i ) int color if
( promising ( i ) ) if ( i n ) cout ltlt
vector1 ? vcolorn else for ( color
1 color lt m color ) vcolori1
color //??????? m_coloring( i1 )
//?????
37
bool promising ( index i ) index j bool
switch switch true j 1 while ( j lt
i switch ) if ( Wij vcolori
vcolorj ) switch false //????????? j

//???? return switch
38
????????
V1
V2
V3
V4
V5
V6
V7
V8
V1?V2?V8?V7?V6?V5?V4?V3?V1
V1
V2
V3
V4
V5
?????????????
39
???????????
1. ???? i ????????????? i-1 ????? 2. ???? n-1
????????????? 0 ????? 3. ???? i ??????????? i-1
????? ????????????,?????? ?????,????? V1
?????????
40
??????????????
????????????????????? ????? n? ???
n ???????,??????? W ??? ?????? 1 ? n
???? Wij true,??? ? i ??? j
????????? ?????????????,?????????,?
????????????????? vindex ??? ,??? 1 ?
n-1,vindexi ??????? i ??? ???????
vindex0?
41

void hamiltonian ( index i ) index j
if ( promising ( i ) )
//??????? i ?????? if ( i n-1 ) ???
vindex0 ? vindexn-1 ??
else for ( j2 j lt n j )
//???????????? vindex i1 j
hamiltonian ( i1 )
?????? vindex0 1 //?V1???????
hamiltonian ( 0 )
42

bool promising ( index i ) index j
bool switch if ( i n-1
! Wvindexn-1vindex0 ) switch false
//???????????? else if ( i gt 0 !
Wvindexi-1vindexi ) switch false
//? i ?????(i-1)??? else switch
true j 1 while ( j lt i switch )
//????????? if ( vindexi vindexj
) switch false j
return switch
43
?????????????????
44
????? 0-1????
Sum-of-Subsets ???????????? W ?????? ?
0-1???????????? W ??,??????? ????????,????????????
????? ?????? void checknode ( node v )
node u if ( value ( v ) ? best ?? )
best value ( v ) if ( promising ( v
) ) for ( ?? v ???? u ) checknode ( u )

???,?????? ?? best?
?????????,?? promising?
45
????0-1???????
(1) ??????????? i ????,??????
?????????,?????? nonpromising? ? weight
?? i ???????????? ? weight ? W ???????
nonpromising ?
?????????
46
????0-1???????(?)
(2) ???????????????????? promising ??????
?? i ?????????
???
? i1 ?? k-1 ?????????? ???? k ?????
? 1 ? i ????????
????? ? k ??? ???
? k ??? ????? ??
? i ??? ????? ????
? k-1 ???? ???
47
? i ??????????,?? nonpromising bound ?
maxprofit
????????? ???????
? i ???????? ???
48
?? n4,W16
i Pi wi Pi/wi
1 40 2 20
2 30 5 6
3 50 10 5
4 10 5 2
???????????? (Pi / Wi )???????? ?????????????????
,????? ???? ( ? profit ) ???? ( ? weight
) ????? ( ? bound )
???
49
????????????????
???? profit
maxprofit 0
???? weight
?????? bound
????
????
maxprofit 40
maxprofit 90
maxprofit 70
maxprofit 80
maxprofit 70
maxprofit 90
maxprofit 70
maxprofit 90
maxprofit 80
???
maxprofit 90
maxprofit 80
maxprofit 90
???
50
(0,0)??????
1. ? maxprofit ?? 0 ?( ???????????????) 2. ??
(0, 0) ??,????? (a) ???? profit ?
weight? profit 0 weight 0 (b) ????
bound ?? 2510 17 ? 17 gt 16 (???????
W) ????? 3 ???????????? W? gt k 3
(?????????? k ?)
?5.4
51
(0,0)??????(?)
P1
P2
?3?????????
?????? (0, 0) ? promising? ?? (1) weight 0 ??
16 (W ??) (2) bound 115 ?? 0 (??
maxprofit ??)
?5.4
52
????0-1????
???? n ??????? weight ? profit ? (?????)?
?? W ????????? W ????,??????
?????????? ????? n ? W? ?? w ? p
(???? 1 ? n,???pi/wi????
???)? ??bestset ?? (??? 1 ? n),?? bestseti ??
yes ?? ??? i ???,no ????? i ????
??? maxprofit (?????)?
53

void knapsack ( index i , int profit , int
weight ) if ( weight lt W profit gt
maxprofit ) //??????,????? maxprofit profit
//?????? maxprofit ?????? numbest i
//? numbest ?????????? bestset
include //? bestset ?????
if ( promising ( i ) )
//??? i ????????? include i1 "yes"
//? wi1 knapsack ( i1 , profit pi1 ,
weight wi1 ) include i1 "no"
//?? wi1 knapsack ( i1 , profit , weight
)
54

bool promising ( index i ) index j , k
int totweight float bound
//? i ????????? if ( weight gt W )
return false //?????? else j
i 1 //? i ?????????????????????? bound
profit totweight weight while ( j lt n
totweight wj lt W ) totweight
totweight wj //?????????? bound bound
pj //??????????????? j k j
//??? k ?????????(??????) if ( k lt
n ) bound bound (W-totweight) ( pk /
wk ) return bound gt maxprofit
//???????????? promising
//??,???? i
???? nonpromising
?k??? ?????
55
????????????? maxprofit ????????? numbest 0
//?????????????? maxprofit 0 knapsack ( 0,
0, 0 ) cout ltlt maxprofit //??????? for
( j 1 j lt numbest j )
//?????????? cout ltlt bestsetj
Write a Comment
User Comments (0)
About PowerShow.com