State Space
State Space
1. Overview
Here, we’ll discuss the state space search concept in detail with an
example.
Furthermore, we’ll present several applications where we can use this
concept.
The state space size can greatly affect a search algorithm’s efficiency. Hence,
it’s important to choose an appropriate representation and search strategy to
efficiently search the state space.
The most famous state space search algorithm is the A* algorithm. Other
popular state space search algorithms are breadth-first search (BFS), depth-
first search (DFS), hill climbing, simulated annealing, and genetic algorithms.
3. Steps
Now let’s discuss the steps of a typical state space search algorithm:
The first step is to initialize the search by setting the initial state as the current
state. After the initialization step, we check if the current state is a goal state.
Finally, if the current state is a goal state, we terminate the algorithm and
return the result.
However, if the current state is not the goal state, we generate the set of
possible states that can be reached from the current state. Additionally, these
states are known as successor states. Furthermore, for each successor state,
we check if it has been previously visited. If the state is already explored, we
skip the state. If it has not been visited, we add it to the queue of states to be
visited.
Moving forward, we set the next state in the queue as the current state and
check if it’s a goal state. If we find the goal state, we return the result.
Otherwise, we repeat the previous step until we find the goal state or finish
exploring all the states. Furthermore, if all possible states have been explored
and we can’t reach the target state, we return with no solution.
The specific implementation details of the algorithm depend on the problem.
Additionally, the algorithm’s performance depends on the data structures
we use to represent the states and keep track of the search.
4. Example
A common example of a state space search is the 8-puzzle problem. The
8-puzzle is a sliding puzzle that consists of 8 numbered tiles in a 3, 3 grid and
one blank space. The goal is to rearrange the tiles from a given initial state to
a final goal state by sliding the tiles into the blank space.
In this problem, we represent the state space by the 9 tiles in the puzzle and
their positions in the grid. Additionally, we present each state in the state
space by a 3 3 array with values from 1 to 8. Finally, we represent a blank
space with an employ tile.
The initial state represents the starting configuration of the tiles. The goal
state represents the desired configuration. Furthermore, the search algorithms
use the state space to find a sequence of moves that transform the initial state
into the goal state.
For example, we can use the breadth-first search to explore all possible states
reachable from the initial state. It explores all the states one by one in a
sequence. It ensures the solution but can become very slow for large state
spaces. Other algorithms, such as A* search, use heuristics to guide the
search more efficiently.
We’re taking a practical example of an 8-puzzle problem. Let’s pick current
and target states for this example:
Here, we aim to start from the current state and reach the target state by
sliding the numbers through the blank. Furthermore, the approach is to
explore all the states we can reach from the current state. For each new state,
we check if it’s the target state or not. Let’s take a look at how to reach the
target state from the current state:
5. Applications
State space search algorithms have a wide range of applications in
various fields, including artificial intelligence, robotics, game playing,
computer networks, operations research, bioinformatics, cryptography,
and supply chain management.
In artificial intelligence, we can use state space search algorithms to solve
problems such as pathfinding, planning, and scheduling. Additionally, we can
employ space search algorithms to plan the motion of robots, determining the
best sequence of actions to achieve a goal. Furthermore, we also use these
algorithms in games to determine the best move for a player, given a
particular game state.
In computer networks, we can take advantage of state space search
algorithms to optimize routing and resource allocation in computer networks.
Additionally, we also use them in operations research to solve optimization
problems, such as scheduling and resource allocation. We can also apply
these algorithms to find patterns in biological data and predict protein
structures in bioinformatics.
In order to find solutions to cryptographic problems, such as breaking codes
and finding cryptographic keys, we can employ these algorithms.
Furthermore, we can use these algorithms to optimize the flow of goods and
resources in logistics and supply chain management.
These are just a few examples of the many applications of state space search
algorithms. The ability to efficiently search large state spaces and find
solutions to complex problems makes state space search algorithms a
powerful tool in a wide range of fields.
6. Conclusion
In this tutorial, we discussed the state space search concept in detail with an
example. Furthermore, we explored different fields and applications where it
can be extremely useful.
Depth-First Search (DFS) and Breadth-First Search (BFS)
1. Introduction
Depth-First Search (DFS) and Breadth-First Search (BFS). Here, we’ll compare them and
discuss in which scenarios we should use one instead of the other.
2. Search
Search problems are those in which our task is to find the optimal path between a start
node and a goal node in a graph. There are several variations to search problems: the graph
may be directed or undirected, weighted or unweighted, and there may be more than one goal
node.
DFS and BFS are suitable for unweighted graphs, so we use them to find the shortest path
between the start and the goal.
4. Conceptual Difference
The following example illustrates the main conceptual difference between DFS and BFS. Let’s
imagine that we’ve got the following search tree:
Let’s mark each node by the ordinal number of its inclusion in the tree. The orders in which DFS
and BFS include the nodes differ:
In the BFS tree, all the inclusion numbers at level are lower than the numbers at level .
That’s not the case in DFS. In DFS, if node has a lower inclusion number than node , then all
the descendants of have lower numbers than and its descendants.
So, BFS grows the tree level by level, whereas DFS grows it sub-tree by sub-tree.
5. Implementation Differences
At each execution step, both DFS and BFS maintain what we call the search frontier. It’s a set
of nodes we identified as children of the nodes currently in the tree but didn’t add to it
yet. Since BFS always adds the child of the least recently included node, it uses a FIFO queue to
maintain the frontier. In contrast, DFS uses a LIFO queue in its iterative implementation.
When adding a node to the search tree, we also identify its children and add them to the frontier.
That’s called expanding the node. So, we can describe BFS and DFS as follows:
DFS is also very suitable for a recursive implementation. In that case, the call stack acts as a
frontier.
6.1. Completeness
DFS isn’t a complete algorithm. To see why let’s imagine that the graph contains a
cycle . Depending on the choice of the successor to add first to the tree when
considering the successors of a node, DFS may get stuck in a loop:
Even if we randomize the choice, we can still end in a loop and run indefinitely, although such
an outcome is less likely. So, DFS may never terminate even if the goal is very close to the start
node and the graph is finite and small:
If (the TLS version of) DFS goes to , it will forever alternate between and .
On the other hand, BFS is always complete if the graph is finite. If the graph is infinite, then
BFS is complete under the following condition:
A goal node is reachable from the start, and no node has an infinite number of successors.
6.2. Optimality
DFS isn’t an optimal algorithm either. It may return a sub-optimal path to the goal, and that
happens if the goal is reachable in more than one way, but DFS discovers a longer path first:
8. Conclusion
In this article, we compared Depth-First Search (DFS) to Breadth-First Search (BFS). While BFS
has some theoretical advantages over DFS, it’s impractical because of the high order of its space
complexity. That is why we use DFS more often.