Backtracking

Backtracking Concept

  • Select one option when multiple choices exist

  • When a choice is made, a new set of choices is generated

  • Continue making choices until reaching a final state

    • If you keep making correct choices, you reach the goal state!

Difference between Backtracking and DFS

  • Reduce the number of attempts by not following a path if it seems unlikely to lead to a solution

    • Prunning

  • While DFS tracks all paths, Backtracking cuts off unnecessary paths early

  • When there are too many cases for DFS

    • DFS on problems with N! cases becomes unprocessable

  • Applying Back Tracking algorithm generally reduces the number of cases, but in worst case scenarios, it still requires exponential time, making it unprocessable

  • The path from root node to leaf node becomes a candidate solution, and we can find the answer among these candidates using DFS

    • However, this method is inefficient because it searches all descendant nodes of nodes that have no possibility of being solutions

Backtracking Technique

  • After checking the promisingess of a node, if it's determined to be unpromising, backtrack to the parent node and move to the next child node

  • When visiting a node:

    • If the path including that node cannot be a solution, the node is unpromising

    • If there's a possibility for a solution, it's promising

Prunning

: No longer consider paths that include unpromising nodes

Backtracking Algorithm Procedure

  1. Perform DFS (Depth-First Search) on the state space tree

  2. Check if each node is promising

  3. If the node is unpromising, return to its parent node and continue searching

N-Queen Problem

SWEA 2806

Subset

Finding subsets

ex)

Solving Problems by Building Space State Tree

Finding Permutations using Backtracking

The approach is similar to finding subsets

ex)

Last updated