Backtracking

From WikiMD's Food, Medicine & Wellness Encyclopedia

Backtracking

Backtracking is a computational algorithmic technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree). It is a type of Depth First Search but with added constraints. Backtracking is used in a variety of problems, including puzzle solving and constraint satisfaction problems.

Overview[edit | edit source]

Backtracking can be thought of as a selective tree/graph traversal method. It is used for solving problems that can be broken down into smaller problems of the same type. A classic example is the Eight Queens Puzzle, where the goal is to place eight queens on a chessboard such that no two queens threaten each other. Backtracking works by attempting to place a queen on a board, and if placing the next queen leads to a conflict, it backtracks by removing the previously placed queen and trying another position.

Algorithm[edit | edit source]

The backtracking algorithm consists of a recursive function that attempts to solve the problem. The function typically follows this structure:

1. Base Case: If the current solution is a valid and complete solution, return this solution. 2. For each candidate:

  a. Check if the candidate can potentially lead to a solution.
  b. If the candidate is valid, add it to the current solution.
  c. Recursively attempt to build on this candidate.
  d. If the candidate does not lead to a solution, remove it from the current solution (backtrack).

Applications[edit | edit source]

Backtracking is used in various applications such as:

Advantages and Disadvantages[edit | edit source]

Advantages[edit | edit source]

  • Backtracking provides a systematic way to iterate through all possible configurations of a problem.
  • It is flexible and can be applied to many types of problems.

Disadvantages[edit | edit source]

  • It can be inefficient, as it explores some paths that may lead to a dead end and thus, might involve a lot of unnecessary calculations.
  • The time complexity can be exponential in the worst case, making it impractical for large datasets.

See Also[edit | edit source]

Backtracking Resources
Doctor showing form.jpg
Wiki.png

Navigation: Wellness - Encyclopedia - Health topics - Disease Index‏‎ - Drugs - World Directory - Gray's Anatomy - Keto diet - Recipes

Search WikiMD


Ad.Tired of being Overweight? Try W8MD's physician weight loss program.
Semaglutide (Ozempic / Wegovy and Tirzepatide (Mounjaro / Zepbound) available.
Advertise on WikiMD

WikiMD is not a substitute for professional medical advice. See full disclaimer.

Credits:Most images are courtesy of Wikimedia commons, and templates Wikipedia, licensed under CC BY SA or similar.

Contributors: Prab R. Tumpati, MD