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

Slides:



Advertisements
Similar presentations
Backtracking Algorithm
Advertisements

BackTracking Algorithms
Techniques for Dealing with Hard Problems Backtrack: –Systematically enumerates all potential solutions by continually trying to extend a partial solution.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
5-1 Chapter 5 Tree Searching Strategies. 5-2 Satisfiability problem Tree representation of 8 assignments. If there are n variables x 1, x 2, …,x n, then.
Branch & Bound Algorithms
September 26, 2012Introduction to Artificial Intelligence Lecture 7: Search in State Spaces I 1 After our “Haskell in a Nutshell” excursion, let us move.
B ACKTRACK SEARCH ALGORITHM. B ACKTRACKING Suppose you have to make a series of decisions, among various choices, where You don’t have enough information.
S. J. Shyu Chap. 1 Introduction 1 The Design and Analysis of Algorithms Chapter 1 Introduction S. J. Shyu.
Sum of Subsets and Knapsack
Backtracking.
6 - 1 § 6 The Searching Strategies e.g. satisfiability problem x1x1 x2x2 x3x3 FFF FFT FTF FTT TFF TFT TTF TTT.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CS420 lecture ten BACKTRACK. Solution vectors In optimization problems, or more general in search problems, a set of choices are to be made to arrive.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Chapter 11: Limitations of Algorithmic Power
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Ch 13 – Backtracking + Branch-and-Bound
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
5-1 Chapter 5 Tree Searching Strategies. 5-2 Breadth-first search (BFS) 8-puzzle problem The breadth-first search uses a queue to hold all expanded nodes.
Jin Zheng, Central South University1 Branch-and-bound.
Backtracking.
Busby, Dodge, Fleming, and Negrusa. Backtracking Algorithm Is used to solve problems for which a sequence of objects is to be selected from a set such.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Search.
Dr. Jouhaina Chaouachi Siala
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Design and Analysis of Algorithms - Chapter 111 How to tackle those difficult problems... There are two principal approaches to tackling NP-hard problems.
Chapter 12 Coping with the Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
BackTracking CS335. N-Queens The object is to place queens on a chess board in such as way as no queen can capture another one in a single move –Recall.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
CS 312: Algorithm Analysis Lecture #32: Intro. to State-Space Search This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported.
CSE 024: Design & Analysis of Algorithms Chapter 9: NP Completeness Sedgewick Chp:40 David Luebke’s Course Notes / University of Virginia, Computer Science.
1 Lower Bounds Lower bound: an estimate on a minimum amount of work needed to solve a given problem Examples: b number of comparisons needed to find the.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
알고리즘 설계 및 분석 Foundations of Algorithm 유관우. Digital Media Lab. 2 Chap4. Greedy Approach Grabs data items in sequence, each time with “best” choice, without.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
Search by partial solutions.  nodes are partial or complete states  graphs are DAGs (may be trees) source (root) is empty state sinks (leaves) are complete.
CSE 589 Part VI. Reading Skiena, Sections 5.5 and 6.8 CLR, chapter 37.
COPING WITH THE LIMITATIONS OF ALGORITHM POWER
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Design and Analysis of Algorithms - Chapter 101 Our old list of problems b Sorting b Searching b Shortest paths in a graph b Minimum spanning tree b Primality.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
LIMITATIONS OF ALGORITHM POWER
Ch22.Branch and Bound.
Backtracking & Brute Force Optimization Intro2CS – weeks
Search in State Spaces Problem solving as search Search consists of –state space –operators –start state –goal states A Search Tree is an efficient way.
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
CSCE350 Algorithms and Data Structure Lecture 21 Jianjun Hu Department of Computer Science and Engineering University of South Carolina
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
The Theory of NP-Completeness 1. Nondeterministic algorithms A nondeterminstic algorithm consists of phase 1: guessing phase 2: checking If the checking.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSG3F3/ Desain dan Analisis Algoritma
BackTracking CS255.
Depth-First Search N-Queens Problem Hamiltonian Circuits
Back Tracking.
Branch and Bound.
Analysis & Design of Algorithms (CSCE 321)
راهبرد عقبگرد (Backtracking)
Backtracking and Branch-and-Bound
Unit –VII Coping with limitations of algorithm power.
Lecture 4: Tree Search Strategies
Presentation transcript:

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

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

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); } DFS : BFS :

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 Q (i, col(i)) Q (k, col(k)) Q (i,col(i)) Q (k,col(k))

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 = Queen 문제의 state-space tree (x 1, x 2, x 3, x 4 )=(2, 4, 1, 3)

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)

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 ,173, ⅹ ⅹ , ⅹ ⅹ , ⅹ ⅹ n 17 2, ⅹ ⅹ 10 7

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 }

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) } ⅹ ⅹ ⅹ ⅹⅹⅹ ⅹ

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

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. 시작 X X X XX

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

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

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)

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

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 = x 2 =1

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

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

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

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