8 Puzzle Problem in AI

Module - 2 AI Algorithms
8 Puzzle Problem in AI

The 8-puzzle is a well-known problem in the field of artificial intelligence and puzzle-solving. It serves as an intriguing model problem with various applications and is widely used to explore heuristic search and state-space exploration.

What is 8 Puzzle Problem in AI and Its Relevance?

The 8-puzzle, often regarded as a small, solvable piece of a larger puzzle, holds a central place in AI because of its relevance in understanding and developing algorithms for more complex problems.

Rules and Constraints:

To tackle the 8-puzzle, it's crucial to comprehend its rules and constraints:

  • The 8-puzzle is typically played on a 3x3 grid, which provides a 3x3 square arrangement for tiles. This grid structure is fundamental to the problem's organization.
  • The puzzle comprises 8 numbered tiles (usually from 1 to 8) and one blank tile. These numbered tiles can be slid into adjacent positions (horizontally or vertically) when there's an available space, which is occupied by the blank tile.
  • The objective of the 8-puzzle is to transform an initial state, defined by the arrangement of the tiles on the grid, into a specified goal state. The goal state is often a predefined configuration, such as having the tiles arranged in ascending order from left to right and top to bottom, with the blank tile in the bottom-right corner.

The Challenge:

The primary challenge of the 8-puzzle problem lies in starting from a given initial state and finding a sequence of moves that leads to the goal state.

This challenge is multifaceted:

  • The 8-puzzle has a vast number of possible states, making it essential to determine the most efficient sequence of moves.
  • The need to consider many potential states demands efficient search algorithms and heuristic functions to guide the search process.
  • The 8-puzzle serves as an essential problem-solving model, as many practical applications in AI, such as route planning and optimization, require similar search techniques. Understanding and mastering the 8-puzzle problem is a stepping stone to addressing more complex real-world challenges.

Explain 8 Puzzle Problem in AI

Describing the 8-Puzzle State:

The state of the 8-puzzle is represented using a 3x3 grid, where each cell can hold one of the numbered tiles or remain empty (occupied by the blank tile). This grid serves as a compact and systematic way to capture the configuration of the puzzle.

  • In a 3x3 grid, each cell can contain one of the following elements:
    • Numbered tiles, typically from 1 to 8.
    • A blank tile, represented as an empty cell.
  • The arrangement of these elements in the grid defines the state of the puzzle. The state represents the current position of the tiles within the grid, which can vary as the puzzle is manipulated.

Initial and Goal States:

In the context of the 8-puzzle, two fundamental states are of particular importance: the initial state and the goal state.

1. Initial State:

  • The initial state of the 8-puzzle represents the starting configuration. It's the state from which the puzzle-solving process begins.
  • The initial state can be any arrangement of the tiles, which can be specified manually or generated randomly.
  • The problem-solving algorithm aims to transform the initial state into the goal state using a sequence of valid moves.

2. Goal State:

  • The goal state represents the desired configuration that the puzzle should reach.
  • In most cases, the goal state involves arranging the numbered tiles in ascending order from left to right and top to bottom, with the blank tile in the bottom-right corner.
  • Achieving the goal state demonstrates the successful solution of the puzzle.

8-puzzle problem in AI

Significance of State Space:

State space is a critical concept in problem-solving, including the 8-puzzle. It provides a structured way to explore and navigate the puzzle's possible states.

  • State Space Definition:
    • The state space of the 8-puzzle encompasses all possible states that the puzzle can transition through, from the initial state to the goal state.
    • Each state in the state space represents a unique configuration of the puzzle.
  • Navigating the State Space:
    • Problem-solving algorithms, like search algorithms, traverse the state space systematically, evaluating different states to find an optimal path from the initial state to the goal state.
    • The state space's vastness highlights the complexity of the 8-puzzle problem, as there are numerous potential states to explore.
  • Search Strategies:
    • Within the state space, search strategies determine the order in which states are explored. Algorithms like Breadth-First Search and A* employ various techniques to efficiently navigate this space.

Understanding the representation of the 8-puzzle state, the concept of initial and goal states, and the significance of the state space is essential for grasping the problem-solving process in AI and heuristic search.

How AI Technique is Used to Solve 8 Puzzle Problem?

Introducing Search Algorithms:

Search algorithms play a central role in solving the 8-puzzle problem by systematically exploring possible states and finding a sequence of moves to reach the goal state.

  • Search algorithms are a fundamental tool in artificial intelligence and problem-solving, used to navigate complex state spaces efficiently.

Choice of Algorithms:

When approaching the eight puzzle problem in ai, the choice of search algorithm is critical. Several algorithms can be used, each with its advantages and trade-offs. Here, we discuss three common choices:

1. Breadth-First Search (BFS):

  • BFS is an uninformed search algorithm that explores states level by level.
  • It guarantees finding the shortest path to the goal state, making it a reliable choice for the 8-puzzle problem.
  • However, it may consume substantial memory, as it stores all explored states.

2. Depth-First Search (DFS):

  • DFS is another uninformed search algorithm that explores states deeply before backtracking.
  • It is memory-efficient but does not guarantee finding the optimal solution.
  • DFS can be suitable for solving the 8-puzzle when memory constraints are a concern.

3. A* Algorithm:

  • A* is an informed search algorithm that combines the cost to reach a state (g-value) with a heuristic estimate of the cost to reach the goal (h-value).
  • It is widely used for solving problems like the 8-puzzle, as it finds optimal solutions efficiently.
  • A* relies on an admissible heuristic to guide the search.

Explaining the Search Tree:

  • The search tree is a fundamental concept in search algorithms, including those used to solve the 8-puzzle.
  • It is a tree-like structure where each node represents a state, and edges between nodes represent valid moves.
  • The tree's root node corresponds to the initial state, and the leaf nodes represent various possible states.
    • Search algorithms systematically expand the tree by exploring states, evaluating their potential, and making decisions to move closer to the goal state.
  • The search tree provides a visual representation of the problem-solving process, aiding in the identification of the optimal solution.
  • The choice of search algorithm and its strategies, such as node selection and pruning, significantly impact the structure and exploration of the search tree.

By understanding the role of search algorithms, the choice of specific algorithms for the 8-puzzle problem, and the use of search trees to guide the solution process, participants will gain insights into the core mechanics of puzzle-solving in AI.

Introducing Heuristic Functions:

Heuristic functions are a vital component of informed search algorithms, like A*. They provide estimates of the cost to reach a goal state from a given state. Here's why they are essential:

  • Importance of Heuristic Functions: Heuristic functions are used to guide search algorithms by providing a measure of how promising a state is in reaching the goal. In other words, they help the algorithm make informed choices about which states to explore next.
  • Informed vs. Uninformed Search: Informed search algorithms, like A*, use heuristic functions to focus on more promising states, making them significantly more efficient than uninformed algorithms.

Heuristic Based Evaluation for 8-puzzle problem in AI

The A* Algorithm:

The A* algorithm is a widely used informed search algorithm that combines g-values (the actual cost to reach a state) and h-values (heuristic estimates of the cost to reach the goal) to find an optimal solution. Here's how it works:

  • Components of A:
    • g-value: The g-value represents the actual cost to reach a particular state from the initial state. It accumulates the costs of the path taken to that state.
    • h-value: The h-value is the heuristic estimate of the cost to reach the goal state from the current state. It provides an informed guess about the remaining cost.
  • Combining g and h in A:A evaluates states using the sum of their g and h values, referred to as the f-value. The algorithm prioritizes states with lower f-values because they are more likely to lead to an optimal solution.

Admissible and Consistent Heuristics for the 8-Puzzle:

  • In the context of the 8 puzzle problem in ai code, it's crucial to use admissible and consistent heuristics for A* to ensure optimality.
  • Admissible Heuristic: An admissible heuristic never overestimates the cost to reach the goal. In other words, it always provides a lower bound on the actual cost. This property ensures that A* will find an optimal solution if an admissible heuristic is used.
  • Consistent Heuristic: A consistent heuristic, also known as a monotonic heuristic, satisfies an additional property. It ensures that the estimated cost from a state to a successor state, plus the estimated cost from that successor to the goal, is never greater than the estimated cost from the initial state to the goal. This property helps maintain the optimality of A*.
  • For the 8-puzzle, a common admissible heuristic is the Misplaced Tiles Heuristic, which counts the number of tiles that are not in their goal position. This heuristic is admissible because it underestimates the cost to reach the goal.
  • Another admissible and consistent heuristic is the Manhattan Distance Heuristic, which calculates the sum of the distances each tile is from its goal position. This heuristic is also admissible and consistent, making it suitable for A*.

Understanding the role of heuristic functions, the mechanics of the A* algorithm, and the importance of admissible and consistent heuristics in the context of the 8-puzzle problem is crucial for comprehending how informed search methods work to find optimal solutions.


In this session, we delved into the fascinating world of the 8-puzzle problem, a classic puzzle that has been a hallmark of artificial intelligence and problem-solving for decades. We began by defining the 8-puzzle, its rules, and constraints, emphasizing its significance in the realm of AI and puzzle-solving.

We explored various representation techniques and state spaces, enabling us to visualize how search algorithms navigate through potential solutions. The choice of search algorithms, including Breadth-First Search (BFS) and A*, illuminated the fundamental role of informed search in optimizing problem-solving processes.

The discussion on heuristic functions and the A* algorithm unveiled the elegance of informed search methods. Admissible and consistent heuristics for the 8-puzzle, such as Misplaced Tiles and Manhattan Distance, illustrated the importance of selecting appropriate heuristics to ensure optimality in problem-solving.

In summary, this session offered a comprehensive understanding of how AI-driven search algorithms can effectively address complex puzzles like the 8-puzzle. Participants gained insights into the mechanics of puzzle-solving in AI, search strategies, and heuristic-based informed search.

Key Takeaways

  • The 8 puzzle problem in artificial intelligence is a classic puzzle used in AI to explore state space and search algorithms.
  • Search algorithms like Breadth-First Search (BFS) and A* play a central role in solving the 8-puzzle problem.
  • The search tree is a visual representation of how search algorithms explore and evaluate potential states.
  • Heuristic functions provide informed estimates of the cost to reach the goal state and guide informed search algorithms.
  • The A* algorithm combines g-values (actual cost) and h-values (heuristic estimates) to find optimal solutions.
  • Admissible and consistent heuristics are crucial for maintaining the optimality of A*.
  • The choice of search algorithm impacts memory usage and efficiency in solving the 8-puzzle.
  • Informed search methods can efficiently tackle complex puzzles and optimize problem-solving processes.
Recommended Courses
Certification in Full Stack Data Science and AI
20,000 people are doing this course
Become a job-ready Data Science professional in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Certification in Full Stack Web Development
20,000 people are doing this course
Become a job-ready Full Stack Web Developer in 30 weeks. Join the largest tech community in India. Pay only after you get a job above 5 LPA.
Masters Program in Data Science and Artificial Intelligence
20,000 people are doing this course
Join India's best Masters program in Data Science and Artificial Intelligence. Get the best jobs in top tech companies. Accredited by ECTS and globally recognised in EU, US, Canada and 60+ countries.

AlmaBetter’s curriculum is the best curriculum available online. AlmaBetter’s program is engaging, comprehensive, and student-centered. If you are honestly interested in Data Science, you cannot ask for a better platform than AlmaBetter.

Kamya Malhotra
Statistical Analyst
Fast forward your career in tech with AlmaBetter
Explore Courses

Vikash SrivastavaCo-founder & CPTO AlmaBetter

Vikas CTO

Related Tutorials to watch

view Allview-all

Top Articles toRead

view Allview-all
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter