Docsity

Backtracking Algorithm: Solving Complex Problems with Multiple Solutions, Lecture notes of Computer Science

Partial preview of the text

Download Backtracking Algorithm: Solving Complex Problems with Multiple Solutions and more Lecture notes Computer Science in PDF only on Docsity!

Chapter- 5

Backtracking Algorithm

Outline

Backtracking

— What is backtracking? — When and how to use backtracking?

Backtracking Algorithm Applications

— N queens Problem — Hamiltonian cycle — Graph coloring

  • Backtracking : Example #

Backtracking : Example #1 description

  • (^) In the above example 1:
    • (^) It uses recursive calling to find the solution by building a

solution step by step increasing values with time.

  • (^) It removes the solutions that doesn't give rise to the

solution of the problem based on the constraints given to

solve the problem.

Backtracking…

  • (^) Backtracking uses brute force approach.
    • (^) Brute force approach said that for any given problem tries out all the possible solutions and chooses the desired/best solutions.
  • (^) This strategies we already following in dynamic programming
  • (^) If you want an optimal solution, you must go for dynamic programming.
  • (^) Note: Backtracking is not for optimization, instead backtracking is used to solve problems that have multiple solutions and we want all those solutions.

Backtracking : Example

  • (^) Here,
  • (^) Green is the start point, blue is the intermediate point, red are points with no feasible solution, dark green is end solution.
  • (^) Here, when the algorithm propagates to an end to check if it is a solution or not, if it is then returns the solution otherwise backtracks to the point one step behind it to find track to the next point to find solution.

State Space Tree

  • (^) A space state tree is a tree representing all the possible states (solution or non-solution) of the problem from the root as an initial state to the leaf as a terminal state.

Backtracking Approach: Example

  • (^) Problem: You want to find all the possible ways of arranging 2 boys and 1 girl on 3 benches. Constraint: Girl should not be on the middle bench.
  • (^) Solution: There are a total of 3! = 6 possibilities. We will try all the possibilities and get the possible solutions. We recursively try all the possibilities.
  • (^) All the possibilities are:

Backtracking Algorithm Applications

— To solve N queens Problem — To find all Hamiltonian paths present in a graph — Graph coloring — Maze solving problem

N-queen Problem

  • (^) Backtracking problem to find the solution to N-Queen Problem.
  • (^) In N-Queen problem, we are given an nxn chessboard and we have to place n queens on the board in such a way that no two queens attack each other.
  • (^) A queen will attack another queen if it is placed in rows, colunm or diagonal points in its way.
  • (^) Here, we will do 4-Queen problem.
  • (^) Here, the solution is −

Backtracking Algorithm

  • (^) The idea is to place queens one by one in different columns, starting from the leftmost column.
  • (^) When we place a queen in a column, we check for clashes with already placed queens.
  • (^) In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution.
  • (^) If we do not find such a row due to clashes then we backtrack and return false.

Backtracking Algorithm…

_1) Start in the leftmost column

  1. If all queens are placed return true
  2. Try all rows in the current column. Do following for every tried row. a) If the queen can be placed safely in this row then mark this [row, column] as part of the solution and recursively check if placing queen here leads to a solution. b) If placing the queen in [row, column] leads to a solution then return true. c) If placing queen doesn't lead to a solution then unmark this [row, column] (Backtrack) and go to step (a) to try other rows.
  3. If all rows have been tried and nothing worked, return false to trigger backtracking._

N-queen Problem- Example …

N-queen Problem- Example …