Which Data Structure Is Used in Backtracking?


Scott Campbell

Backtracking is an important algorithmic technique used in computer science to solve problems by systematically searching through all possible solutions. A crucial component of backtracking algorithms is the data structure used to store and manipulate the problem space. In this article, we will explore the different data structures commonly used in backtracking and understand their strengths and weaknesses.

The Stack Data Structure

The stack data structure is one of the most commonly used data structures in backtracking algorithms. It follows the Last-In-First-Out (LIFO) principle, making it ideal for tracking the sequence of decisions made during backtracking.

When exploring a solution space, backtracking algorithms use a stack to keep track of the current state or path taken. Each decision made is pushed onto the stack, allowing for easy backtracking by simply popping off the last decision and exploring alternative paths.


  • Consider a maze-solving problem where we need to find a path from a starting point to an exit point.
  • A stack can be used to keep track of each step taken in the maze.
  • If we reach a dead end or encounter an obstacle, we can backtrack by popping off the last step from the stack and exploring other possible directions.

The Tree Data Structure

The tree data structure is another commonly used data structure in backtracking algorithms. It provides a hierarchical representation of decision choices and allows for efficient exploration of all possible paths.

In backtracking, a tree is often used to represent the problem space. Each node in the tree represents a particular state or decision point, while edges represent possible choices or transitions between states.


  • Consider the classic eight queens problem, where we need to place eight queens on an 8×8 chessboard without any queen attacking another.
  • A tree can be used to represent the decision choices at each row of the chessboard.
  • The root node represents the first row, and each child node represents a possible position for the queen in that row.
  • By traversing the tree, we can explore all possible combinations of queen placements and find a valid solution.

The Graph Data Structure

The graph data structure is often used in backtracking algorithms when the problem involves searching for a path or cycle in a graph. Backtracking on graphs helps us explore different paths and find solutions efficiently.

A graph consists of vertices (nodes) and edges (connections between nodes). Each vertex represents a state or decision point, while edges represent transitions between states or choices.


  • Consider a problem where we need to find all possible routes between two cities on a map.
  • A graph can be used to represent the cities as vertices and roads as edges connecting them.
  • By traversing the graph using backtracking, we can explore different routes until we reach the destination city or exhaust all possibilities.

In Conclusion

In backtracking algorithms, selecting an appropriate data structure is crucial for efficient exploration of solution spaces. The stack, tree, and graph data structures are commonly used depending on the nature of the problem at hand. Understanding their strengths and weaknesses will help you design effective backtracking algorithms for various applications.

Remember to choose your data structure wisely when implementing a backtracking algorithm and leverage the power of stacks, trees, or graphs to efficiently explore the problem space.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy