Please enable JavaScript.
Coggle requires JavaScript to display documents.
Backtracking - Coggle Diagram
Backtracking
Backtracking is used to find all possible solutions available to a problem. When it realises that it has made a bad choice, it undoes the last choice by backing it up. It searches the state space tree until it has found a solution for the problem.
n-Queens problem
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other.
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.
-
Subset-Sum Problem
Given a set of non-negative integers, and a value sum, determine if there is a subset of the given set with sum equal to given sum.
1- Start with an empty set
2 - Add the next element from the list to the set
3 - If the subset is having sum M, then stop with that subset as solution.
4 - If the subset is not feasible or if we have reached the end of the set, then backtrack through the subset until we find the most suitable value.
5 - If the subset is feasible (sum of seubset < M) then go to step 2.
6 - If we have visited all the elements without finding a suitable subset and if no backtracking is possible then stop without solution.
-