Open In App

Backtracking Algorithm

Last Updated : 17 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Backtracking algorithms are like problem-solving strategies that help explore different options to find the best solution. They work by trying out different paths and if one doesn’t work, they backtrack and try another until they find the right one. It’s like solving a puzzle by testing different pieces until they fit together perfectly.

Backtracking

What is Backtracking Algorithm?

Backtracking is a problem-solving algorithmic technique that involves finding a solution incrementally by trying different options and undoing them if they lead to a dead end.

It is commonly used in situations where you need to explore multiple possibilities to solve a problem, like searching for a path in a maze or solving puzzles like Sudoku. When a dead end is reached, the algorithm backtracks to the previous decision point and explores a different path until a solution is found or all possibilities have been exhausted.

How Does a Backtracking Algorithm Work?

A backtracking algorithm works by recursively exploring all possible solutions to a problem. It starts by choosing an initial solution, and then it explores all possible extensions of that solution. If an extension leads to a solution, the algorithm returns that solution. If an extension does not lead to a solution, the algorithm backtracks to the previous solution and tries a different extension.

The following is a general outline of how a backtracking algorithm works:

  1. Choose an initial solution.
  2. Explore all possible extensions of the current solution.
  3. If an extension leads to a solution, return that solution.
  4. If an extension does not lead to a solution, backtrack to the previous solution and try a different extension.
  5. Repeat steps 2-4 until all possible solutions have been explored.

Example of Backtracking Algorithm

Example: Finding the shortest path through a maze

Input: A maze represented as a 2D array, where 0 represents an open space and 1 represents a wall.

Algorithm:

  1. Start at the starting point.
  2. For each of the four possible directions (up, down, left, right), try moving in that direction.
  3. If moving in that direction leads to the ending point, return the path taken.
  4. If moving in that direction does not lead to the ending point, backtrack to the previous position and try a different direction.
  5. Repeat steps 2-4 until the ending point is reached or all possible paths have been explored.

When to Use a Backtracking Algorithm?

Backtracking algorithms are best used to solve problems that have the following characteristics:

  • There are multiple possible solutions to the problem.
  • The problem can be broken down into smaller subproblems.
  • The subproblems can be solved independently.

Applications of Backtracking Algorithm

Backtracking algorithms are used in a wide variety of applications, including:

  • Solving puzzles (e.g., Sudoku, crossword puzzles)
  • Finding the shortest path through a maze
  • Scheduling problems
  • Resource allocation problems
  • Network optimization problems

Basic of Backtracking Algorithm:

Standard Problems on Backtracking Algorithm:

Easy Problems on Backtracking Algorithm:

Medium Problems on Backtracking Algorithm:

Hard Problems on Backtracking Algorithm:

Quick Links :



Similar Reads

Top 20 Backtracking Algorithm Interview Questions
N Queens Problem Warnsdorff's Algorithm Word Break Problem Remove Invalid Parenthesis Match a pattern and string using regular expression Find Path from corner cell to middle cell in a maze Hamiltonian cycle Sudoku M Coloring Problem Rat in a Maze Print all permutations of a given string Crptarithmetic puzzle Find if there is a path of more than k
1 min read
Backtracking Algorithm in Python
Backtracking is a problem-solving algorithmic technique that involves finding a solution incrementally by trying different options and undoing them if they lead to a dead end. The backtracking algorithm is a recursive algorithm that is used to solve problems by making a series of choices, and if a choice leads to a dead end, it backtracks to the la
4 min read
Algorithms | Backtracking | Question 1
Which of the following is not a backtracking algorithm? (A) Knight tour problem (B) N queen problem (C) Tower of hanoi (D) M coloring problem Answer: (C) Explanation: Knight tour problem, N Queen problem and M coloring problem involve backtracking. Tower of hanoi uses simple recursion.
1 min read
Print the DFS traversal step-wise (Backtracking also)
Given a graph, the task is to print the DFS traversal of a graph which includes every step including the backtracking. 1st step:- 0 -> 1 2nd step:- 1 -> 5 3rd step:- 5 -> 1 (backtracking step) 4th step:- 1 -> 6... and so on till all the nodes are visited. Dfs step-wise(including backtracking) is: 0 1 5 1 6 7 8 7 6 1 0 2 4 2 9 3 10 Note:
10 min read
Rat in a Maze | Backtracking using Stack
Prerequisites - Recursion, Backtracking and Stack Data Structure. A Maze is given as N*M binary matrix of blocks and there is a rat initially at (0, 0) ie. maze[0][0] and the rat wants to eat food which is present at some given block in the maze (fx, fy). In a maze matrix, 0 means that the block is a dead end and 1 means that the block can be used
16 min read
Travelling Salesman Problem implementation using BackTracking
Travelling Salesman Problem (TSP): Given a set of cities and distance between every pair of cities, the problem is to find the shortest possible route that visits every city exactly once and returns back to the starting point.Note the difference between Hamiltonian Cycle and TSP. The Hamiltonian cycle problem is to find if there exist a tour that v
9 min read
Difference between Backtracking and Branch-N-Bound technique
Algorithms are the methodical sequence of steps which are defined to solve complex problems. In this article, we will see the difference between two such algorithms which are backtracking and branch and bound technique. Before getting into the differences, lets first understand each of these algorithms. Backtracking: Backtracking is a general algor
4 min read
Maximum size subset with given sum using Backtracking
Given an array arr[] consisting of N integers and an integer K, the task is to find the length of the longest subsequence with a sum equal to K.Examples: Input: arr[] = {-4, -2, -2, -1, 6}, K = 0 Output: 3 Explanation: The longest subsequence is of length 3 which is {-4, -2, 6} having sum 0.Input: arr[] = {-3, 0, 1, 1, 2}, K = 1 Output: 5 Explanati
9 min read
Generate all distinct subsequences of array using backtracking
Given an array arr[] consisting of N positive integers, the task is to generate all distinct subsequences of the array. Examples: Input: arr[] = {1, 2, 2}Output: {} {1} {1, 2} {1, 2, 2} {2} {2, 2}Explanation:The total subsequences of the given array are {}, {1}, {2}, {2}, {1, 2}, {1, 2}, {2, 2}, {1, 2, 2}.Since {2} and {1, 2} are repeated twice, pr
7 min read
What is the difference between Backtracking and Recursion?
What is Recursion? The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Properties of Recursion:Performing the same operations multiple times with different inputs.In every step, we try smaller inputs to make the problem smaller.A base condition is nee
2 min read