Presentation is loading. Please wait.

Presentation is loading. Please wait.

알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking (Eg) 큰 성 (castle)- 수 백 개의 방 (rooms) 그리고 “Sleeping Beauty” Systematic.

Similar presentations


Presentation on theme: "알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking (Eg) 큰 성 (castle)- 수 백 개의 방 (rooms) 그리고 “Sleeping Beauty” Systematic."— Presentation transcript:

1 알고리즘 설계 및 분석 Foundations of Algorithm 유관우

2 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(2 n, nW)) Backtracking : can be very efficient if good bounding function (promising function). Recursion

3 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); } 1 2 3 456 78 9 DFS : 1 2 4 7 9 8 6 3 5 BFS : 1 2 3 4 5 6 7 8 9 1 2 357 11 4 6 8910

4 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|) Q Q Q Q 1 2 3 4 5 6 7 8 Q (i, col(i)) Q (k, col(k)) Q (i,col(i)) Q (k,col(k))

5 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) X 1 =1 X 2 =2 3 4 4 3 4 34 2 3 23 2 243 23 4 1 41 3 2 4 1 34 1 24 3 2 1 2 3 4 4-Queen 문제의 state-space tree (x 1, x 2, x 3, x 4 )=(2, 4, 1, 3)

6 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)

7 Digital Media Lab. 7 Better (faster) Algorithm Monte Carlo Algorithm “place almost all queens randomly, then do remaining queens using backtracking.” #Nodes DFS (same row X) (n n ) #Nodes DFS (same col. X) (n!) #Nodes Backtracking #Nodes Promising Backtracking 341 19,173,961 9.73 ⅹ 10 12 1.20 ⅹ 10 16 24 40,320 4.79 ⅹ 10 8 8.72 ⅹ 10 10 61 15,721 1.01 ⅹ 10 7 3.78 ⅹ 10 8 4 8 12 14 n 17 2,057 8.56 ⅹ 10 5 2.74 ⅹ 10 7

8 Digital Media Lab. 8 Sum-of-subsets problem A special case of 0/1-knapsack S = {item 1, item 2,…, item n } n items w[1..n] = (w 1, w 2, …, w n ) weights W. Find a subset A ⊆ S s.t. ∑w i =W (Eg) n=5, W=21 w[1..5]=(5,6,10,11,16) Solutions : {w 1,w 2,w 3 }, {w 1,w 5 }, {w 3,w 4 } 0/1-knapsack 과의 관계 ? NP-Complete State space tree : 2 n leaves A w1w1 w2w2 w3w3 w2w2 w3w3 w3w3 w3w3 O OO OOOO {w 1,w 2 } {w 3 }

9 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 (w 1,w 2,w 3,w 4 )=(3,4,5,6) function promising (i: index) : boolean; { promising=(weight + total ≥ W) and (weight=W or weight+w[i+1]≤ W) } ⅹ 12 7 3978 13 4 043 7 0 0 3 30 4 5 0 0 4 5 5 6 00 0 0 ⅹ ⅹ ⅹⅹⅹ ⅹ

10 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 then write(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

11 Digital Media Lab. 11 Graph Coloring (p199) M-coloring problem; Color undirected graph with colors. 2 adjacent vertices : diff.color (Eg) V1V1 V2V2 V4V4 V3V3 2-coloring X 3-coloring O State-space tree : leaves Promising function : check adjacent vertices for the same color. 시작 123 213 213 2 13 X X X XX

12 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);

13 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.)

14 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 node0-th node 3. i -th node ≠ 0 ~ ( i -1)-th node V1V1 V2V2 V6V6 V5V5 V3V3 V7V7 V4V4 V8V8 V1V1 V2V2 V5V5 V3V3 V4V4 HC : X (Eg)

15 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) ;

16 Digital Media Lab. 16 0/1 Knapsack problem w[1..n] = (w 1, w 2,…, w n ) p[1..n] = (p 1, p 2,…, p n ) W: Knapsack size Determine A ⊆ S maximizing State-space tree 2 n leaves x 1 =1 x 2 =1 x 3 =1 x 4 =1 0 0 0 000 00 0000 0 0 1 11110 1 11 1 1 x 2 =1

17 Digital Media Lab. 17 Strategy Procedure 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 P i /W i ) 지금까지 이익 + 남은 자루용량으로 최대이익 ≤ 지금까지 최고이익  nonpromising

18 Digital Media Lab. 18 (Eg) n=4, W=16 (p 1, p 2, p 3, p 4 )=(40, 30, 50,10) (w 1, w 2, w 3, w 4 )=(2, 5,10, 5) (P i /W i )=(20, 6, 5, 2) 3 2 Item 1 X XXXX X X 0 115 40 2 115 0 82 70 7 115 40 2 98 120 17 70 7 80 12 80 70 7 70 90 12 98 40 2 50 100 17 90 12 90

19 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; } }

20 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 : p i = w i =1(1 ≤i ≤n-1) p n = w n =W=n (2 n+1 -1)nodes in total


Download ppt "알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap. 5 Backtracking (Eg) 큰 성 (castle)- 수 백 개의 방 (rooms) 그리고 “Sleeping Beauty” Systematic."

Similar presentations


Ads by Google