알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5...
Click here to load reader
-
Upload
beverly-stone -
Category
Documents
-
view
212 -
download
0
description
Transcript of 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5...
![Page 1: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/1.jpg)
알고리즘 설계 및 분석Foundations of Algorithm유관우
![Page 2: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/2.jpg)
Digital Media Lab. 2
Chap. 5 Backtracking(Eg) 큰 성 (castle)- 수 백 개의 방 (rooms) 그리고 “ Sleeping Beauty” Systematic search - BFS, DFS 갈필요 없는 길 – 미리 알 수 있다면… (Bounding function or promising function) DFS with Bounding function (promising function) Worst-case : exhaustive search But, 대부분의 겨우 : 몹시 효율적 NP-Complete problems (Eg) 0-1 knapsack problem D.P. : O(min(2n, nW)) Backtracking : can be very efficient if good bounding function (promising function). Recursion
![Page 3: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/3.jpg)
Digital Media Lab. 3
Backtracking : DFS of a tree except that nodes are visited if promising
Promising function DFS (Depth First Search)
Procedure d_f_s_tree( v: node)var u : node{ visit v; // some action. // for each child u of v do d_f_s_tree(u);}
12 3
4 5 6
7 8
9
DFS : 1 2 4 7 9 8 6 3 5
BFS : 1 2 3 4 5 6 7 8 9
1
2
3 5 7 114
6
8 9 10
![Page 4: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/4.jpg)
Digital Media Lab. 4
n-Queens Problem nⅹn Chess board n Queens (e.g.) 8-Queens problem
Promising function( Bounding fcn) (i) same row ⅹ (ii) same column ⅹ (col(i) ≠ col(k)) (iii) same diagonal ⅹ (|col(i)-col(k)|≠|i-k|)
, ,)(2
2
nn
n n
,! , nnn
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
Q (i, col
(i)) Q (k, col
(k))
Q (i,col(i))
Q (k,col(k))
![Page 5: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/5.jpg)
Digital Media Lab. 5
#leaf nodes : n!=4! DFS : “Backtrack “ at dead ends – 4! Leaf nodes (n!) Backtracking : “Backtrack “ if non-promising (prom. fcn.) (pruning)
X1=1
X2=2 3 4
43
4 3 4 2 3 2 3 2
2 4 32 3 4 1 4 1 3 2 4
1 3 4 1 2 4 321
2 3 4
4-Queen 문제의 state-space tree
(x1, x2, x3, x4)=(2, 4, 1, 3)
![Page 6: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/6.jpg)
Digital Media Lab. 6
procedure queens ( i: index);var j : index;{ if promising(i) then if I==n then write (col[1]~col[n]); else for j=1 to n do { col[i+1]=j; queens(i+1); }function promising ( i : index) : boolean;var k : index;{ k=1; promising=true; while k<i and promising do { if (col[i]==col[k]) or abs(col[i]-col[k])==abs(i-k)) promising=false; k++;}} main : queens(0); Print all solutions. (Need to Exit)
![Page 7: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/7.jpg)
Digital Media Lab. 7
Better (faster) AlgorithmMonte Carlo Algorithm“place almost all queens randomly, then do remaining queens using backtracking.”
#Nodes DFS (same row X)
(nn)
#Nodes DFS(same col. X)
(n!)
#NodesBacktracking
#Nodes Promising
Backtracking341
19,173,9619.73ⅹ1012
1.20ⅹ1016
2440,320
4.79ⅹ108
8.72ⅹ1010
6115,721
1.01ⅹ107
3.78ⅹ108
48
1214
n
172,057
8.56ⅹ105
2.74ⅹ107
![Page 8: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/8.jpg)
Digital Media Lab. 8
Sum-of-subsets problem A special case of 0/1-knapsack S = {item1, item2,…, itemn} n items w[1..n] = (w1, w2, …, wn) weights W. Find a subset A⊆S s.t. ∑wi=W(Eg) n=5, W=21 w[1..5]=(5,6,10,11,16) Solutions : {w1,w2,w3}, {w1,w5}, {w3,w4}
0/1-knapsack 과의 관계 ? NP-Complete State space tree : 2n leaves
A
w1
w2
w3
w2
w3 w3 w3
O
O O
OOOO
{w1,w2} {w3}
![Page 9: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/9.jpg)
Digital Media Lab. 9
Assumption : weights are in sorted order Promising function
ⅹ weight + w i+1 > W at i-th level ⅹ weight + total < W(Eg) n=4, W=13 (w1,w2,w3,w4)=(3,4,5,6)
function promising (i: index) : boolean;{ promising=(weight + total ≥ W) and (weight=W or weight+w[i+1]≤ W)}
ⅹ
12
7
3 97 8
13
4
0437
0
033 0
4
5
0 04
55
6
0 00
0ⅹ
ⅹ
ⅹⅹⅹⅹ
![Page 10: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/10.jpg)
Digital Media Lab. 10
Sum_of_subsets(0,0,total); Initially, total =
procedure sum_of_subsets ( i : index; weight, total : integer);{ if promising(i) then
if weight= W thenwrite(include[1]~include[i]);
else{ include[i+1] = ‘yes’; sum_of_subsets(i+1 , weight+w[i+1], total-w[i+1]);
include[i+1] = ‘no’; sum_of_subsets(i+1 , weight, total-w[i+1]); }}
#nodes in state space tree: For almost all instances : Only small portion But
122 1
0
nn
k
k
nodesWwWw nn
n
ii )12(:, 1
1
1
n
iiw
1
![Page 11: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/11.jpg)
Digital Media Lab. 11
Graph Coloring(p199) M-coloring problem;
Color undirected graph with colors.2 adjacent vertices : diff.color(Eg)
m
V1 V2
V4 V3
2-coloring X3-coloring O
State-space tree : leaves
Promising function : check adjacent vertices for the same color.
시작1 2 3
21 3
21 3
21 3X
X
X
X X
nmnodes
11...1
121
mmmmmn
n
m
![Page 12: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/12.jpg)
Digital Media Lab. 12
function promising(i:index) : boolean;var j : index;{ promising=true;
j=1;while j<i and promising do {if W[i,j ] and vcolor[i]==vcolor[j] then promising =false;j++; }
}procedure m_coloring(i: index)var color: integer;{ if promising(i) then
if i==n then write(vcolor[1]~vcolor[n])else for color = 1 to m do {
vcolor[i+1]=color; m_color(i+1); }}
main: m_coloring(0);
![Page 13: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/13.jpg)
Digital Media Lab. 13
Hamiltonian Circuits Problem TSP problem in chapter 3.
Brute-force: n!, (n-1)! D.P. : When n=20, B.F 3800 years D.P. 45 sec. More efficient solution? See chapter 6
Given an undirected or directed graph, find a tour(HC). (need not be S.P.)
32)2)(1( nnn
![Page 14: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/14.jpg)
Digital Media Lab. 14
State-space tree(n-1)! Leaves worst-case.
Promising function:1. i-th node (i+1)-th node.2.(n-1)-th node 0-th node3. i-th node ≠ 0 ~ (i-1)-th node
V1 V2
V6V5
V3
V7
V4
V8
V1 V2
V5
V3
V4HC : X
(Eg)
![Page 15: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/15.jpg)
Digital Media Lab. 15
function promising (i : index) : boolean;var j : index;{ if (i==n-1) and not W[vindex[n-1], vindex[0]] then promising=false;
else if i >0 and not W[vindex[i-1], vindex[i])then promising = false;else { promising = true; j = 1; while j <i and proimising do { if vindex[i] == vindex[j] then promising = false; j++; } }
}Procedure hamilton (i : index) { if promising(i) then
if (i == n-1) then write(vindex[0] ~ vindex[n-1])else for j = 2 to n do {vindex[i+1]=j;hamilton(i+1); }
}
main: vindex[0]=1; hamilton(0) ;
![Page 16: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/16.jpg)
Digital Media Lab. 16
0/1 Knapsack problem w[1..n] = (w1, w2,…, wn) p[1..n] = (p1, p2,…, pn) W: Knapsack size Determine A S maximizing⊆
State-space tree
2n leaves
x1=1x2=
1x3=1x4=
1
00
0
00000
00000
01
11 1 1 01
1 11
1
x2=1
WwtspA
iA
i ..
![Page 17: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/17.jpg)
Digital Media Lab. 17
StrategyProcedure checknode(v : node);{ if value(v) > best then best = value(v);
if promising(v) then for each child u of v, checknode(u); } Obvious promising fcn
weight ≥W (need not expand)
Less obvious promising function.Note: 0/1 Knapsack 의 해≤ fractional Knapsack 의 해At level i,
maxprofit: best solution so far.Then, nonpromising if bound ≤ maxprofit.(Note: Sorted according to Pi/Wi)지금까지 이익 + 남은 자루용량으로 최대이익 ≤ 지금까지 최고이익 nonpromising
k
kk
ij
k
ij
wpWp
Ww
)totweight()profit(bound
)(,weighttotweight
1
1j
1
1j
![Page 18: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/18.jpg)
Digital Media Lab. 18
(Eg) n=4, W=16(p1, p2, p3, p4)=(40, 30, 50,10)(w1, w2, w3, w4)=(2, 5,10, 5)(Pi/Wi)=(20, 6, 5, 2)
3
2
Item 1
X
X X X X
X
X
00
115
402
115
00
82
707
115
402
98
12017
707
80801280
707
70
901298
402
5010017
901290
![Page 19: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/19.jpg)
Digital Media Lab. 19
function promising(i :index) : boolean;{ if weight ≥W then promising = false;
else { j = i+1; bound = profit; totweight = weight; while j ≤ n && totweight + w[j] ≤ W {
totalweight = totalweight + w[j]; bound = bound +p[j] ;
j++; } k=j ; if k ≤n then { bound += (W-totweight)*p[k]/w[k]; promising = bound > maxprofit; }
}}
![Page 20: 알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking…](https://reader037.fdocuments.net/reader037/viewer/2022101123/5a4d1c117f8b9ab0599f6e9f/html5/thumbnails/20.jpg)
Digital Media Lab. 20
Procedure knapsack(i :index) ; profit, weight : integer);{ if weight ≤ W and profit >maxprofit then
maxprofit = profit; numbest = i ; bestset = include;} // array of size n //if promising(i) then {include[i +1]=‘yes’;knapsack(i+1, profit+p[i +1], weight+w[i +1]);include[i +1]=‘no’;knapsack(i +1, profit, weight);
}}Numbest = maxprofit =0;knapsack(0,0,0);write(maxprofit ,bestset[1]~bestset[i]);
Very bad instance :pi= wi =1 (1 ≤i ≤n-1)pn= wn =W=n(2n+1-1)nodes in total