💻
Software Development
DSA
DSA
  • Data structure
    • Bit
      • MSB & LSB
  • Arrays
  • Linked List
  • Stack
  • Queue
  • Hash Table
  • Tree
  • Graph
  • Algorithm
    • Bitwise
      • Challenges
    • Array
      • Two pointer
      • Sliding Window
      • Kadane Algorithm
      • Naive Pattern Searching
      • Knuth-Morris-Pratt Algorithm (KMP)
      • Quick Select Algorithm
      • Boyer - Moore Majority Vote Algorithm
      • Floyd's Cycle Detection Algorithm
    • Linked List
      • Challenges
    • Searching
      • Binary Search
        • Challenges
      • Linear Search
      • Depth First Search
      • Breadth First Search
      • Fuzzy Search
      • Rabin-Karp Algorithm
      • A* Algorithm
      • Z Algorithm
    • Sorting
      • Dutch National Flag Algorithm
    • String
      • Manacher Algorithm
    • Gready
      • Knapsack Problem Algorithms
    • Backtracking
    • Tree
      • Moris Traversal
        • Preorder
        • Inorder
    • Graph
      • Biconnected Components
        • Tarjan's Algorithm
      • Kruskal's Algorithm
      • Bellman Ford Algorithm
      • Floyd Warshall Algorithm
      • Topological Sort Algorithm
      • Flood Fill Algorithm
      • Lee Algorithm
      • Boruvka's Algorithm
      • Johnson's Algorithm
      • Kosaraju's Algorithm
    • Rate limitting
      • Token bucket
      • Leaking bucket
      • Fixed window counter
      • Sliding window log
      • Sliding window counter
    • Prioroty Queue
      • Dijkstra's Algorithm
      • Huffman Coding Compression Algorithm
      • Prim's Algorithm
      • kth largest element in an array
    • More
      • Dynamic Programming
      • Pigeonhole Principle
        • Challenges
      • Euclidean algorithm
      • Brian Kernighan’s algorithm
      • Sieve of Eratosthenes Algorithm
      • Sieve of Atkin
      • Luhn Algorithm
      • Maximizing XOR
    • Some pattern to remember when facing the challenges
Powered by GitBook
On this page
  • What is Backtracking Algorithm?
  • How Does a Backtracking Algorithm Work?
  • Example of Backtracking Algorithm
  • When to Use a Backtracking Algorithm?
  • Applications of Backtracking Algorithm
  1. Algorithm

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

Leetcode

PreviousKnapsack Problem AlgorithmsNextTree

Last updated 19 days ago

Restore IP Addresses - LeetCodeLeetCode
Logo
N-Queens - LeetCodeLeetCode
Logo