Algorithmics - Lecture 131 LECTURE 13: Backtracking.

Slides:



Advertisements
Similar presentations
1 Finite Constraint Domains. 2 u Constraint satisfaction problems (CSP) u A backtracking solver u Node and arc consistency u Bounds consistency u Generalized.
Advertisements

BackTracking Algorithms
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
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.
Algorithmics - Lecture 71 LECTURE 7: Algorithms design techniques - Decrease and conquer -
Dynamic Programming Dynamic Programming is a general algorithm design technique for solving problems defined by recurrences with overlapping subproblems.
Algorithm Strategies Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
Backtracking What is backtracking?
1 Discrete Structures & Algorithms Graphs and Trees: II EECE 320.
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.
Backtracking Reading Material: Chapter 13, Sections 1, 2, 4, and 5.
Dynamic Programming A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River,
Backtracking.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Algorithmics - Lecture 21 LECTURE 2: Algorithms description - examples -
Tractable Symmetry Breaking Using Restricted Search Trees Colva M. Roney-Dougal, Ian P. Gent, Tom Kelsey, Steve Linton Presented by: Shant Karakashian.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 8 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Dr. Jouhaina Chaouachi Siala
Artificial Intelligence Lecture 9. Outline Search in State Space State Space Graphs Decision Trees Backtracking in Decision Trees.
Design Techniques for Approximation Algorithms and Approximation Classes.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
Algorithmics - Lecture 8-91 LECTURE 8: Divide and conquer.
Fundamentals of Algorithms MCS - 2 Lecture # 7
Lecture 2: General Problem-Solving Methods. Greedy Method Divide-and-Conquer Backtracking Dynamic Programming Graph Traversal Linear Programming Reduction.
Local Search Algorithms This lecture topic Chapter Next lecture topic Chapter 5 (Please read lecture topic material before and after each lecture.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Dr.Abeer Mahmoud ARTIFICIAL INTELLIGENCE (CS 461D) Dr. Abeer Mahmoud Computer science Department Princess Nora University Faculty of Computer & Information.
Télécom 2A – Algo Complexity (1) Time Complexity and the divide and conquer strategy Or : how to measure algorithm run-time And : design efficient algorithms.
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.
Algorithmics - Lecture 101 LECTURE 10: Greedy technique.
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.
Sudoku Jordi Cortadella Department of Computer Science.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
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.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
Flow in Network. Graph, oriented graph, network A graph G =(V, E) is specified by a non empty set of nodes V and a set of edges E such that each edge.
12-CRS-0106 REVISED 8 FEB 2013 CSG523/ Desain dan Analisis Algoritma Dynamic Programming Intelligence, Computing, Multimedia (ICM)
Algorithmics - Lecture 121 LECTURE 11: Dynamic programming - II -
§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.
Optimization Problems In which a set of choices must be made in order to arrive at an optimal (min/max) solution, subject to some constraints. (There may.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Algorithmics - Lecture 61 LECTURE 6: Analysis of sorting methods.
Analysis & Design of Algorithms (CSCE 321)
Chapter 13 Backtracking Introduction The 3-coloring problem
Local Search Algorithms and Optimization Problems
February 11, 2016Introduction to Artificial Intelligence Lecture 6: Search in State Spaces II 1 State-Space Graphs There are various methods for searching.
Algorithmics - Lecture 41 LECTURE 4: Analysis of Algorithms Efficiency (I)
1 CSC 384 Lecture Slides (c) , C. Boutilier and P. Poupart CSC384: Lecture 16  Last time Searching a Graphplan for a plan, and relaxed plan heuristics.
 2004 SDU 1 Lecture5-Strongly Connected Components.
TU/e Algorithms (2IL15) – Lecture 3 1 DYNAMIC PROGRAMMING
Dynamic Programming (optimization problem) CS3024 Lecture 10.
An Analysis of the n- Queens problem Saleem Karamali.
1 Hojjat Ghaderi [Courtesy of Fahiem Bacchus], University of Toronto, Fall 2006 CSC384: Intro to Artificial Intelligence Backtracking Search I ● Announcements.
Unit – 5: Backtracking For detail discussion, students are advised to refer the class discussion.
CSG3F3/ Desain dan Analisis Algoritma
Greedy algorithms: CSC317
Algorithmics - Lecture 11
Data Structures and Algorithms
BACKTRACKING- GENERAL METHOD
CS 213: Data Structures and Algorithms
Problem Solving: Brute Force Approaches
Analysis and design of algorithm
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Presentation transcript:

Algorithmics - Lecture 131 LECTURE 13: Backtracking

Algorithmics - Lecture 132 Outline What is backtracking ? The general structure of the algorithm Applications: generating permutations, generating subsets, n-queens problem, map coloring, path finding, maze problem

Algorithmics - Lecture 133 What is backtracking? It is a systematic search strategy of the state-space of combinatorial problems It is mainly used to solve problems which ask for finding elements of a set which satisfy some constraints. Most of the problems which can be solved by backtracking have the following general form: “ Find a subset S of A 1 x A 2 x … x A n (A k – finite sets) such that each element s=(s 1,s 2,…,s n ) satisfy some constraints” Example: generating all permutations of {1,2,…,n} A k = {1,2,…,n} for all k s i <> s j for all i<>j (restriction: distinct components)

Algorithmics - Lecture 134 What is backtracking? Basic ideas: the solutions are constructed in an incremental manner by finding the components successively each partial solution is evaluated in order to establish if it is promising (a promising solution could lead to a final solution while a non-promising one does not satisfy the partial constraints induced by the problem constraints) if all possible values for a component do not lead to a promising (valid or viable) partial solution then one come back to the previous component and try another value for it backtracking implicitly constructs a state space tree: –The root corresponds to an initial state (before the search for a solution begins) –An internal node corresponds to a promising partial solution –An external node (leaf) corresponds either to a non-promising partial solution or to a final solution

Algorithmics - Lecture 135 What is backtracking? Example: state space tree for permutations generation (*,*,*) (1,*,*)(2,*,*)(3,*,*) (1,1,*)(1,2,*)(1,3,*) (1,2,3)(1,3,2) (2,1,*)(2,2,*)(2,3,*)(3,1,*)(3,2,*)(3,3,*) (2,1,3)(2,3,1)(3,1,2)(3,2,1)

Algorithmics - Lecture 136 Outline What is backtracking ? The general structure of the algorithm Applications: generating permutations, generating subsets, n-queens problem, map coloring, path finding, maze problem

Algorithmics - Lecture 137 The general structure of the algorithm Basic steps: 1.Choose the solution representation 2.Establish the sets A 1,…,A n and the order in which their elements are processed 3.Derive from the problem restrictions the conditions which a partial solution should satisfy in order to be promising (valid). These conditions are sometimes called continuation conditions. 4.Choose a criterion to decide when a partial solution is a final one

Algorithmics - Lecture 138 The general structure of the algorithm Example: permutations generation 1.Solution representation: each permutation is a vector s=(s 1,s 2,…s n ) satisfying: s i <>s j for all i<>j 2.Sets A 1,…,A n : {1,2,…,n}. Each set will be processed in the natural order of the elements 3.Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy s k <>s i for all i<k 4.Criterion to decide when a partial solution is a final one: k=n

Algorithmics - Lecture 139 The general structure of the algorithm Some notations: (s 1,s 2,…,s k ) partial solution k – index for constructing s A k = {a k 1,…,a k mk } m k =card{A k } i k - index for scanning A k

Algorithmics - Lecture 1310 The general structure of the algorithm Backtracking(A 1, A 2, …, A n ) k:=1; i k :=0 WHILE k>0 DO i k :=i k +1 v:=False WHILE v=False AND i k <=m k DO s k :=a k ik IF (s 1,..s k ) is valid THEN v:=True ELSE i k :=i k +1 ENDIF ENDWHILE IF v=True THEN IF “(s 1,…,s k ) is a final solution” THEN “process the final solution” ELSE k:=k+1; i k :=0 ENDIF ELSE k:=k-1 ENDIF ENDWHILE Search for a value of k-th component which leads to a promising partial solution If such a value exists check if a final solution was obtained If it doesn’t exist then go back to the previous component If it is not a final solution go to the next component If it is a solution then process it and go to try the next possible value

Algorithmics - Lecture 1311 The general structure of the algorithm The recursive variant: Suppose that A 1,…,A n and s are global variables Let k be the component to be filled in The algorithm will be called with BT_rec(1) BT_rec(k) IF “(s 1,…,s k-1 ) is a solution” THEN “process it” ELSE FOR j:=1,m k DO s k :=a k j IF “(s 1,…s k ) is valid” THEN BT_rec(k+1) ENDIF ENDFOR ENDIF Try each possible value Go to fill in the next component

Algorithmics - Lecture 1312 Outline What is backtracking ? The general structure of the algorithm Applications: generating permutations, generating subsets, n-queens problem, map coloring, path finding, maze problem

Algorithmics - Lecture 1313 Application: generating permutations Backtracking(A 1, A 2, …, A n ) k:=1; i k :=0 WHILE k>0 DO i k :=i k +1 v:=False WHILE v=False AND i k <=m k DO s k :=a k ik IF (s 1,..s k ) is valid THEN v:=True ELSE i k :=i k +1 ENDIF ENDWHILE IF v=True THEN IF “(s 1,…,s k ) is a final solution” THEN “process the final solution” ELSE k:=k+1; i k :=0 ENDIF ELSE k:=k-1 ENDIF ENDWHILE permutations(n) k:=1; s[k]:=0 WHILE k>0 DO s[k]:=s[k]+1 v:=False WHILE v=False AND s[k]<=n DO IF valid(s[1..k]) THEN v:=True ELSE s[k]:=s[k]+1 ENDWHILE IF v=True THEN IF k=n THEN WRITE s[1..n] ELSE k:=k+1; s[k]:=0 ELSE k:=k-1 ENDIF ENDIF ENDWHILE

Algorithmics - Lecture 1314 Application: generating permutations Function to check if a partial solution is a valid one valid(s[1..k]) FOR i:=1,k-1 DO IF s[k]=s[i] THEN RETURN FALSE ENDIF ENDFOR RETURN TRUE Recursive variant: perm_rec(k) IF k=n+1 THEN WRITE s[1..n] ELSE FOR i:=1,n DO s[k]:=i IF valid(s[1..k])=True THEN perm_rec(k+1) ENDIF ENDFOR ENDIF

Algorithmics - Lecture 1315 Outline What is backtracking ? The general structure of the algorithm Applications: generating permutations, generating subsets, n-queens problem, map coloring, path finding, maze problem

Algorithmics - Lecture 1316 Application: generating subsets Let A={a 1,…,a n } be a finite set. Generate all subsets of A having m elements. Example: A={1,2,3}, m=2, S={{1,2},{1,3},{2,3}} Solution representation: each subset is represented by its characteristic vector (s i =1 if a i belongs to the subset and s i =0 otherwise) Sets A 1,…,A n : {0,1}. Each set will be processed in the natural order of the elements (first 0 then 1) Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy s 1 +s 2 +…+s k <= m (the partial subset contains at most m elements) Criterion to decide when a partial solution is a final one: s 1 +s 2 +…+s k = m (m elements were already selected)

Algorithmics - Lecture 1317 Application: generating subsets Iterative algorithm subsets(n,m) k:=1 s[k]:=-1 WHILE k>0 DO s[k]:=s[k]+1; IF s[k]<=1 AND sum(s[1..k])<=m THEN IF sum(s[1..k])=m THEN s[k+1..n]=0 WRITE s[1..n] ELSE k:=k+1; s[k]:=-1 ENDIF ELSE k:=k-1 ENDIF ENDWHILE Recursive algorithm subsets_rec(k) IF sum(s[1..k-1])=m THEN s[k..n]=0 WRITE s[1..n] ELSE s[k]:=0; subsets_rec(k+1); s[k]:=1; subsets_rec(k+1); ENDIF Rmk: sum(s[1..k]) computes the sum of the first k components of s[1..n]

Algorithmics - Lecture 1318 Outline What is backtracking ? The general structure of the algorithm Applications: generating permutations, generating subsets, n-queens problem, map coloring, path finding, maze problem

Algorithmics - Lecture 1319 Application: n-queens problem Find all possibilities of placing n queens on a n-by-n chessboard such that they does not attack each other: - each line contains only one queen - each column contains only one queen - each diagonal contains only one queen This is a classical problem proposed by Max Bezzel (1850) an studied by several mathematicians of the time (Gauss, Cantor) Examples: if n<=3 there is no solution; if n=4 there are two solutions As n becomes larger the number of solutions becomes also larger (for n=8 there are 92 solutions)

Algorithmics - Lecture 1320 Application: n-queens problem 1.Solution representation: we shall consider that queen k will be placed on row k. Thus for each queen it suffices to explicitly specify only the column to which it belongs: The solution will be represented as an array (s 1,…,s n ) with s k = the column on which the queen k is placed 2.Sets A 1,…,A n : {1,2,…,n}. Each set will be processed in the natural order of the elements (starting from 1 to n) 3.Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy the problems restrictions (no more than one queen on a line, column or diagonal) 4.Criterion to decide when a partial solution is a final one: k = n (all n queens have been placed)

Algorithmics - Lecture 1321 Application: n-queens problem Continuation conditions: Let (s 1,s 2,…,s k ) be a partial solution. It is a valid partial solution if it satisfies: All queens are on different rows - implicitly satisfied by the solution representation (each queen is placed on its own row) All queens are on different columns: s i <>s j for all i<>j (it is enough to check that s k <>s i for all i<=k-1) All queens are on different diagonals: |i-j| <> |s i – s j | for all i<>j (it is enough to check that |k-i|<>| s k - s i | for all 1<=i<=k-1) Indeed ….

Algorithmics - Lecture 1322 Application: n-queens problem Remark: two queens i and j are on the same diagonal if either i-s i =j-s j  i-j = s i -s j or i+s i =j+s j  i-j= s j -s i This means |i-j|=|s i -s j | i-j=0 i-j=-1 i-j=-(n-2) i-j=1 i-j=n-2 i+j=n+1 i+j=n i+j=3 i+j=2n-1 i+j=n+2

Algorithmics - Lecture 1323 Application: n-queens problem Validation(s[1..k]) FOR i:=1,k-1 DO IF s[k]=s[i] OR |i-k|=|s[i]-s[k]| THEN RETURN False ENDIF ENDFOR RETURN True Algorithm: Queens(k) IF k=n+1 THEN WRITE s[1..n] ELSE FOR i:=1,n DO s[k]:=i IF Validation(s[1..k])=True THEN Queens(k+1) ENDIF ENDFOR ENDIF

Algorithmics - Lecture 1324 Application: map coloring Problem: Let us consider a geographical map containing n countries. Propose a coloring of the map by using 4<=m<n colors such that any two neighboring countries have different colors Mathematical related problem: any map can be colored by using at most 4 colors (proved in 1976 by Appel and Haken) – one of the first results of computer assisted theorem proving

Algorithmics - Lecture 1325 Application: map coloring Problem: Let us consider a geographical map containing n countries. Propose a coloring of the map by using 4<=m<n colors such that any two neighboring countries have different colors Problem formalization: Let us consider that the neighborhood relation between countries is represented as a matrix N as follows: 0 if i and j are not neighbors N(i,j) = 1 if i and j are neighbors Find a map coloring S=(s 1,…,s n ) with s k in {1,…,m} such that for all pairs (i,j) with N(i,j)=1 the elements s i and s j are different (s i <>s j )

Algorithmics - Lecture 1326 Application: map coloring 1.Solution representation S=(s 1,…,s n ) with s k representing the color associated to country k 2. Sets A 1,…,A n : {1,2,…,m}. Each set will be processed in the natural order of the elements (starting from 1 to m) 3.Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy s i <>s j for all pairs (i,j) with N(i,j)=1 For each k it suffices to check that s k <>s j for all pairs i in {1,2,…,k-1} with N(i,k)=1 4. Criterion to decide when a partial solution is a final one: k = n (all countries have been colored)

Algorithmics - Lecture 1327 Application: map coloring Recursive algorithm Coloring(k) IF k=n+1 THEN WRITE s[1..n] ELSE FOR j:=1,m DO s[k]:=j IF valid(s[1..k])=True THEN coloring(k+1) ENDIF ENDFOR ENDIF Validation algorithm valid(s[1..k]) FOR i:=1,k-1 DO IF N[i,k]=1 AND s[i]=s[k] THEN RETURN False ENDIF ENDFOR RETURN True Call: Coloring(1)

Algorithmics - Lecture 1328 Application: path finding Let us consider a set of n towns. There is a network of routes between these towns. Generate all routes which connect two given towns such that the route doesn’t reach twice the same town Arad Timisoara Satu Mare Brasov Iasi Bucuresti Constanta Towns: 1.Arad 2.Brasov 3.Bucuresti 4.Constanta 5.Iasi 6.Satu-Mare 7.Timisoara Routes from Arad to Constanta: 1->7->3->4 1->2->3->4 1->6->5->3->4 1->6->5->2->3->4

Algorithmics - Lecture 1329 Application: path finding Problem formalization: Let us consider that the connections are stored in a matrix C as follows: 0 if doesn’t exist a direct connection between i and j C(i,j) = 1 if there is a direct connection between i and j Find all routes S=(s 1,…,s m ) with s k in {1,…,n} denoting the town visited at moment k such that s 1 is the starting town s m is the destination town s i <>s j for all i <>j (a town is visited only once) C(s i,s i+1 )=1 (there exists a direct connections between towns visited at successive moments)

Algorithmics - Lecture 1330 Application: path finding 1. Solution representation S=(s 1,…,s m ) with s k representing the town visited at moment k 2.Sets A 1,…,A n : {1,2,…,n}. Each set will be processed in the natural order of the elements (starting from 1 to n) 3. Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy: s k <>s j for all j in {1,2,…,k-1} C(s k-1,s k )=1 4. Criterion to decide when a partial solution is a final one: s k = destination town

Algorithmics - Lecture 1331 Application: path finding Recursive algorithm routes(k) IF s[k-1]=destination town THEN WRITE s[1..k-1] ELSE FOR j:=1,n DO s[k]:=j IF valid(s[1..k])=True THEN routes(k+1) ENDIF ENDFOR ENDIF Validation algorithm Valid(s[1..k]) IF C[s[k-1],s[k]]=0 THEN RETURN False ENDIF FOR i:=1,k-1 DO IF s[i]=s[k] THEN RETURN False ENDIF ENDFOR RETURN True Call: s[1]:=starting town routes(2)

Algorithmics - Lecture 1332 Application: maze Maze problem. Let us consider a maze defined on a nxn grid. Find a path in the maze which starts from the position (1,1) and finishes in (nxn) Only white cells can be accessed. From a given cell (i,j) one can pass in one of the following neighboring positions: (i,j) (i,j-1)(i,j+1) (i-1,j) (i+1,j) Remark: cells on the border have fewer neighbours

Algorithmics - Lecture 1333 Application: maze Problem formalization. The maze is stored as a nxn matrix 0 free cell M(i,j) = 1 occupied cell Find a path S=(s 1,…,s m ) with s k in {1,…,n}x{1,…,n} denoting the indices corresponding to the cell visited at moment k s 1 is the starting cell (1,1) s m is the destination cell (n,n) s k <>s qj for all k <>q (a cell is visited at most once) M(s k )=0 (each visited cell is a free one) s k and s k+1 are neighborhood cells

Algorithmics - Lecture 1334 Application: maze 1. Solution representation S=(s 1,…,s n ) with s k representing the cell visited at moment k 2. Sets A 1,…,A n are subsets of {1,2,…,n}x{1,2,…,n}. For each cell (i,j) there is a set of at most 4 neighbors 3. Continuation conditions: a partial solution (s 1,s 2,…,s k ) should satisfy: s k <>s q for all q in {1,2,…,k-1} M(s k )=0 s k-1 and s k are neighbours 4. Criterion to decide when a partial solution is a final one: s k = (n,n)

Algorithmics - Lecture 1335 Application: maze maze(k) IF s[k-1]=(n,n) THEN WRITE s[1..k] ELSE // try all neighbouring cells s[k].i:=s[k-1].i-1; s[k].j:=s[k-1].j // up IF valid(s[1..k])=True THEN maze(k+1) ENDIF s[k].i:=s[k-1].i+1; s[k].j:=s[k-1].j // down IF valid(s[1..k])=True THEN maze(k+1) ENDIF s[k].i:=s[k-1].i; s[k].j:=s[k-1].j-1 // left IF valid(s[1..k])=True THEN maze(k+1) ENDIF s[k].i:=s[k-1].i; s[k].j:=s[k-1].j+1 // right IF valid(s[1..k])=True THEN maze(k+1) ENDIF ENDIF

Algorithmics - Lecture 1336 Application: maze valid(s[1..k]) IF s[k].i n OR s[k].j n // out of the grid THEN RETURN False ENDIF IF M[s[k].i,s[k].j]=1 THEN RETURN False ENDIF // occupied cell FOR q:=1,k-1 DO // loop IF s[k].i=s[q].i AND s[k].j=s[q].j THEN RETURN False ENDIF ENDFOR RETURN True Call of algorithm maze: s[1].i:=1; s[1].j:=1 maze(2)

Algorithmics - Lecture 1337 Next week: revision for exam Topics for exam: Iterative algorithms: design and analysis Recursive algorithms: efficiency analysis Decrease and conquer algorithms (design and efficiency analysis) Divide and conquer algorithms (design and efficiency analysis) Sorting algorithms (basic methods, efficient methods) Greedy algorithms (design, correctness and efficiency analysis) Dynamic programming (design and efficiency analysis) Backtracking (design)