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:
- Sudoku solving
- Crossword puzzle solving
- Graph coloring
- Finding all Hamiltonian cycles in a graph
- Knapsack problems
- Permutations and combinations
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 | |
---|---|
|
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's Wellness Encyclopedia |
Let Food Be Thy Medicine Medicine Thy Food - Hippocrates |
Translate this page: - East Asian
中文,
日本,
한국어,
South Asian
हिन्दी,
தமிழ்,
తెలుగు,
Urdu,
ಕನ್ನಡ,
Southeast Asian
Indonesian,
Vietnamese,
Thai,
မြန်မာဘာသာ,
বাংলা
European
español,
Deutsch,
français,
Greek,
português do Brasil,
polski,
română,
русский,
Nederlands,
norsk,
svenska,
suomi,
Italian
Middle Eastern & African
عربى,
Turkish,
Persian,
Hebrew,
Afrikaans,
isiZulu,
Kiswahili,
Other
Bulgarian,
Hungarian,
Czech,
Swedish,
മലയാളം,
मराठी,
ਪੰਜਾਬੀ,
ગુજરાતી,
Portuguese,
Ukrainian
Medical Disclaimer: WikiMD is not a substitute for professional medical advice. The information on WikiMD is provided as an information resource only, may be incorrect, outdated or misleading, and is not to be used or relied on for any diagnostic or treatment purposes. Please consult your health care provider before making any healthcare decisions or for guidance about a specific medical condition. WikiMD expressly disclaims responsibility, and shall have no liability, for any damages, loss, injury, or liability whatsoever suffered as a result of your reliance on the information contained in this site. By visiting this site you agree to the foregoing terms and conditions, which may from time to time be changed or supplemented by WikiMD. If you do not agree to the foregoing terms and conditions, you should not enter or use this site. 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