Solving a puzzle using search algorithms
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Puzzles, ranging from classic sliding blocks to complex n-dimensional arrays, pose intriguing challenges that are often best addressed using search algorithms. These algorithms systematically explore potential solutions until they find a satisfactory answer. This article delves into different search algorithms used to solve puzzles, illustrating how they work with technical details and examples.
Basic Concepts
Search Algorithms Overview
Search algorithms are strategies for navigating through a problem space to find a goal. They can be classified into two main categories:
- Uninformed Search Algorithms: These algorithms lack additional information about states beyond what's provided in the problem description. They include:
- Breadth-First Search (BFS)
- Depth-First Search (DFS)
- Informed Search Algorithms: These algorithms use heuristic information to make educated guesses, such as:
- A* Search
- Greedy Best-First Search
Cost and Heuristics
- Cost refers to the path's length or "expense" in reaching a particular node or state.
- Heuristics are estimations that measure the proximity to the goal, crucial in informed algorithms for guiding the search more efficiently.
Solving Puzzles with Search Algorithms
Uninformed Search Example: Sliding Puzzle
A sliding puzzle is a simple yet classic example:
- Puzzle Description: Let's take a 3x3 grid with numbers 1-8 and an empty tile.
- Goal: Arrange tiles in order, leaving the blank space at the bottom-right.
Breadth-First Search (BFS)
BFS explores all possible states level by level:
- Initialize: Start from the initial state.
- Expand Nodes: Visit all immediate neighbors.
- Track Visited Nodes: To avoid revisiting.
- Queue Implementation: Use a FIFO queue to manage the nodes.
Code Example (Python Pseudocode):
- `g(n)` = cost from the start to the current node
- `h(n)` = heuristic estimate from the current node to the goal
- Memory Usage: BFS and DFS can use significant memory for complex puzzles.
- Efficiency: Choosing the right heuristic in A* is critical for performance.
Related reading
- Solving The 8 Puzzle With A Algorithm
- Sorting an Array in TensorFlow
- Sorting by simliarity
- SpaCy Spancat Model is Not Making Predictions
- Solving linear equations represented as a string
- Solving N-Queens Problem... How far can we go?
- Spark K-fold Cross Validation
- Spark ML - MulticlassClassificationEvaluator - can we get precision/recall by each class label?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.