Log In Sign Up
Day 48

Day 48: Introduction to Backtracking

48/60 Days

Day 48: Introduction to Backtracking #

Welcome to Day 48 of our 60 Days of Coding Algorithm Challenge! Today, we’ll explore backtracking, a powerful algorithmic technique used for solving problems recursively by trying out different possibilities until finding a solution.

What is Backtracking? #

Backtracking is an algorithmic technique that considers searching every possible combination in order to solve a computational problem. It builds candidates to the solution incrementally and abandons a candidate (“backtracks”) as soon as it determines that the candidate cannot lead to a valid solution.

Key Concepts of Backtracking #

  1. Choice: At each step, we make a choice from a set of available options.
  2. Constraints: The choices we make must satisfy certain problem constraints.
  3. Goal: We have a specific goal or set of conditions that define a solution.

General Structure of a Backtracking Algorithm #

 1def backtrack(candidate):
 2    if find_solution(candidate):
 3        output(candidate)
 4        return
 5    
 6    for next_candidate in list_of_candidates:
 7        if is_valid(next_candidate):
 8            place(next_candidate)
 9 …