A Course under Centre of Excellence as Initiative of Department of Science and
Technology, Government of Bihar
GOVERNMENT POLYTECHNIC SAHARSA
Presenter: Prof. Shubham HoD(Computer Science and Engineering) Todays Class ➢8 Puzzle Problem ➢N Queen Problem 8 Puzzle Problem • The 8-puzzle problem belongs to the category of “sliding block puzzle” type of problem. • The 8-puzzle is a square tray in which eight square tiles are placed. The remaining ninth square is uncovered. Each tile in the tray has a number on it. • Given a 3×3 board with 8 tiles (every tile has one number from 1 to 8) and one empty space. The objective is to place the numbers on tiles to match the final configuration using the empty space. We can slide four adjacent (left, right, above, and below) tiles into the empty space. • The goal is to transform the starting position into the goal position by sliding the tiles around. 8 Puzzle Problem 8 Puzzle without Heuristics • Blind Search(Uninformed) • BFS(Breadth First Search)- Process each Level then proceed to next level • 4 Moves(Up, Down, Left , Right) • We will get optimal state but with higher complexity • Initial State Goal State 1 2 3 1 2 3 4 6 4 5 6 7 5 8 7 8
Time Complexity- O(b^d) b= branching factor, d=depth
Up, Right, Down Branch= (4*3+4*2+1*4)/9= 3 Maximum depth=20 T.C=3^20 8 Puzzle with Heuristics(Informed Search) • Calculate heuristics h(x)=number of misplaced boxes • Similar to best first search with minimum number of misplaced boxes
Initial State Goal State
1 2 3 1 2 3 4 6 4 5 6 7 5 8 7 8
Time Complexity- O(b^d) b= branching factor, d=depth
Up, Right, Down Branch= (4*3+4*2+1*4)/9= 3 Maximum depth=20 T.C=3^20 N Queen Problem • N - Queens problem is to place n - queens in such a manner on an n x n chessboard that no queens attack each other by being in the same row, column or diagonal. • It can be seen that for n =1, the problem has a trivial solution, and no solution exists for n =2 and n =3. So first we will consider the 4 queens problem and then generate it to n - queens problem. • The problem can be solved using Backtracking approach. • The queen can attack in three cases: • 1. In same roe • 2. In same column • 3. In same diagonal N Queens Problem • Let us assume the size of board is 4*4 • Total possible solution is 16C4 • We will put each queen in one row and in each queen in one column.
4*4 Chess Board
Solutions • We have to arrange four queens, Q1, Q2, Q3 and Q4 in 4 x 4 chess board. We will put ith queen in ith row. Let us start with position (1, 1). Q1 is the only queen, so there is no issue. partial solution is <1> • We cannot place Q2 at positions (2, 1) or (2, 2). Position (2, 3) is acceptable. partial solution is <1, 3>. • Next, Q3 cannot be placed in position (3, 1) as Q1 attacks her. And it cannot be placed at (3, 2), (3, 3) or (3, 4) as Q2 attacks her. There is no way to put Q3 in third row. Hence, the algorithm backtracks and goes back to the previous solution and readjusts the position of queen Q2. Q2 is moved from positions (2, 3) to (2, 4). Partial solution is <1, 4> • Now, Q3 can be placed at position (3, 2). Partial solution is <1, 4, 3>. • Queen Q4 cannot be placed anywhere in row four. So again, backtrack to the previous solution and readjust the position of Q3. Q3 cannot be placed on (3, 3) or(3, 4). So the algorithm backtracks even further. • All possible choices for Q2 are already explored, hence the algorithm goes back to partial solution <1> and moves the queen Q1 from (1, 1) to (1, 2). And this process continues until a solution is found. 8 Queen Problem • Solutions