The Wayback Machine - https://web.archive.org/web/20240807045723/https://www.geeksforgeeks.org/uniformed-search-algorithms-in-ai/
Open In App

Uniformed Search Algorithms in AI

Last Updated : 20 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

Uniformed search algorithms are a class of search algorithms that do not have any additional information about the problem other than what is given in the problem definition. They are also known as blind search algorithms.

In Artificial Intelligence (AI), search algorithms play a crucial role in finding solutions to problems. These algorithms systematically explore the search space to find the optimal path or solution.

In this article, we will explore some of the most commonly used uniformed search algorithms in AI.

Understanding Uniformed Search Algorithms

Uniformed search algorithms, also known as blind search algorithms, are a class of search algorithms used in artificial intelligence that do not use any domain-specific knowledge about the problem being solved. These algorithms rely solely on the information provided in the problem definition, such as the initial state, actions available in each state, and the goal state. Uniformed search algorithms are called “blind” because they do not have a heuristic function to guide the search towards the goal; instead, they explore the search space systematically.

Uninformed search algorithms play a fundamental role in artificial intelligence (AI) by providing basic search strategies for exploring problem spaces where no additional knowledge is available beyond the problem definition. These algorithms are crucial for solving a wide range of problems in AI, such as pathfinding, puzzle solving, and state-space search.

The main role of uninformed search algorithms is to systematically explore the search space to find a solution, without using any domain-specific knowledge or heuristics. While these algorithms may not always be the most efficient, they provide a baseline for understanding and solving complex problems in AI.

1. Breadth-First Search (BFS)

Breadth-First Search is one of the simplest and most fundamental search algorithms in AI. It starts at the initial state and explores all the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level. BFS uses a queue data structure to keep track of the nodes that are yet to be explored. It guarantees the shortest path from the initial state to the goal state if the path cost is a non-decreasing function of the depth of the node.

Here’s a simple implementation of the Breadth-First Search (BFS) algorithm in Python for solving AI problems. In this example, we’ll use BFS to find the shortest path in a maze represented by a 2D grid.

Python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from collections import deque

def bfs(maze, start, goal):
    queue = deque([(start, [])])
    visited = set()
    
    while queue:
        current, path = queue.popleft()
        x, y = current
        
        if current == goal:
            return path + [current]
        
        if current in visited:
            continue
        
        visited.add(current)
        
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < len(maze) and 0 <= ny < len(maze[0]) and maze[nx][ny] != 1:
                queue.append(((nx, ny), path + [current]))
    
    return None

def visualize_maze(maze, start, goal, path=None):
    cmap = ListedColormap(['white', 'black', 'red', 'blue', 'green'])
    bounds = [0, 0.5, 1.5, 2.5, 3.5, 4.5]
    norm = plt.Normalize(bounds[0], bounds[-1])
    
    fig, ax = plt.subplots()
    ax.imshow(maze, cmap=cmap, norm=norm)
    
    ax.scatter(start[1], start[0], color='yellow', marker='o', label='Start')
    ax.scatter(goal[1], goal[0], color='purple', marker='o', label='Goal')
    
    if path:
        for node in path[1:-1]:
            ax.scatter(node[1], node[0], color='green', marker='o')
    
    ax.legend()
    plt.show()

# Example maze
maze = np.array([
    [0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1],
    [0, 0, 0, 0, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
])

start = (0, 0)
goal = (4, 4)

path = bfs(maze, start, goal)

visualize_maze(maze, start, goal, path)

Output:

download-(3)

Graphical Representation of the maze grid with start position represented by yellow circle and goal position represented by purple circle.

2. Depth-First Search (DFS)

Depth-First Search is another basic search algorithm in AI. Unlike BFS, DFS explores as far as possible along each branch before backtracking. It uses a stack data structure to keep track of the nodes to be explored. DFS does not guarantee finding the shortest path to the goal node, but it is often more memory-efficient than BFS.

Python
def visualize_maze(maze, start, goal, path=None):
    cmap = ListedColormap(['white', 'black', 'red', 'blue', 'green'])
    bounds = [0, 0.5, 1.5, 2.5, 3.5, 4.5]
    norm = plt.Normalize(bounds[0], bounds[-1])
    
    fig, ax = plt.subplots()
    ax.imshow(maze, cmap=cmap, norm=norm)
    
    ax.scatter(start[1], start[0], color='yellow', marker='o', label='Start')
    ax.scatter(goal[1], goal[0], color='purple', marker='o', label='Goal')
    
    if path:
        for node in path[1:-1]:
            ax.scatter(node[1], node[0], color='green', marker='o')
    
    ax.legend()
    plt.show()

def dfs(maze, start, goal):
    stack = [(start, [])]
    visited = set()
    
    while stack:
        current, path = stack.pop()
        x, y = current
        
        if current == goal:
            return path + [current]
        
        if current in visited:
            continue
        
        visited.add(current)
        
        for dx, dy in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
            nx, ny = x + dx, y + dy
            if 0 <= nx < len(maze) and 0 <= ny < len(maze[0]) and maze[nx][ny] != 1:
                stack.append(((nx, ny), path + [current]))
    
    return None

# Example maze
maze = np.array([
    [0, 0, 0, 0, 0],
    [1, 1, 0, 1, 1],
    [0, 0, 0, 0, 0],
    [0, 1, 1, 1, 0],
    [0, 0, 0, 0, 0]
])

start = (0, 0)
goal = (4, 4)

path = dfs(maze, start, goal)

visualize_maze(maze, start, goal, path)

Output:

download-(4)

The output represents he maze grid with start position represented by yellow circle and goal position represented by purple circle.

3. Depth-Limited Search (DLS)

Depth-Limited Search is a variant of DFS where the depth of the search is limited to a certain level. This is done to avoid infinite loops in cases where the search space is infinite or too large. DLS is useful when the depth of the goal node is known.

4. Iterative Deepening Depth-First Search (IDDFS)

Iterative Deepening Depth-First Search is a combination of BFS and DFS. It repeatedly applies DFS with increasing depth limits until the goal node is found. IDDFS combines the benefits of BFS (guaranteed shortest path) and DFS (less memory consumption) by gradually increasing the depth limit.

5. Uniform-Cost Search (UCS)

Uniform-Cost Search is a variant of BFS where the cost of the path is taken into account. It explores the node with the lowest path cost first. UCS guarantees finding the optimal path to the goal node when the path cost is non-negative.

Uniform-cost search (UCS) is a tree or graph search algorithm used in artificial intelligence that finds the least cost path from a given initial node to a goal node. It’s useful in situations where paths have different costs associated with them. UCS expands the least costly node first, ensuring that when it reaches a goal node, it has found the least cost path to that node.

Below is a Python implementation of the Uniform-cost search algorithm. This code assumes you have a way to represent your graph, nodes, and costs.

Python
import networkx as nx
import matplotlib.pyplot as plt
import heapq

class Node:
    def __init__(self, state, parent=None, action=None, path_cost=0):
        self.state = state
        self.parent = parent
        self.action = action
        self.path_cost = path_cost

    def __lt__(self, other):
        return self.path_cost < other.path_cost

def uniform_cost_search(graph, start, goal):
    frontier = []
    heapq.heappush(frontier, Node(start))
    explored = set()
    path = []
    
    while frontier:
        node = heapq.heappop(frontier)
        
        if node.state == goal:
            path = reconstruct_path(node)
            break
        
        explored.add(node.state)
        
        for (cost, result_state) in graph[node.state]:
            if result_state not in explored:
                child_cost = node.path_cost + cost
                child_node = Node(result_state, node, None, child_cost)
                if not any(frontier_node.state == result_state and 
                           frontier_node.path_cost <= child_cost for frontier_node in frontier):
                    heapq.heappush(frontier, child_node)
                    
    return path

def reconstruct_path(node):
    path = []
    while node:
        path.append(node.state)
        node = node.parent
    return path[::-1]

def visualize_graph(graph, path=None):
    G = nx.DiGraph()
    labels = {}
    for node, edges in graph.items():
        for cost, child_node in edges:
            G.add_edge(node, child_node, weight=cost)
            labels[(node, child_node)] = cost
    
    pos = nx.spring_layout(G)
    nx.draw(G, pos, with_labels=True, node_color='lightblue', node_size=2000)
    nx.draw_networkx_edge_labels(G, pos, edge_labels=labels)
    
    if path:
        path_edges = list(zip(path[:-1], path[1:]))
        nx.draw_networkx_edges(G, pos, edgelist=path_edges, edge_color='r', width=2)
    
    plt.show()

# Define the graph
graph = {
    'A': [(1, 'B'), (3, 'C')],
    'B': [(2, 'D')],
    'C': [(5, 'D'), (2, 'B')],
    'D': []
}

start = 'A'
goal = 'D'
path = uniform_cost_search(graph, start, goal)

visualize_graph(graph, path)
print("Path from", start, "to", goal, ":", path)

Output:

Path from A to D : ['A', 'B', 'D']
download-(5)

The graph represent the optimal path determined by the UCS algorithm


Role of Uniformed Search Algorithm in AI

  1. Pathfinding: One of the most common applications of uniformed search algorithms is in pathfinding problems, such as finding the shortest path between two points on a map. Algorithms like Breadth-First Search (BFS) and Depth-First Search (DFS) are used to explore the map and find the optimal path.
  2. Puzzle Solving: Uniformed search algorithms are also used in puzzle-solving problems, such as the Eight Puzzle or the Fifteen Puzzle. These algorithms can be used to find a sequence of moves that lead to the solution of the puzzle.
  3. Game Playing: In game-playing AI, uniformed search algorithms can be used to explore the game tree and find the optimal sequence of moves to win the game. Algorithms like DFS and BFS are often used in conjunction with minimax or alpha-beta pruning to improve efficiency.
  4. Robot Navigation: Uniformed search algorithms can be used in robotics for path planning and navigation tasks. Robots can use these algorithms to explore their environment and find the best path to reach a target location while avoiding obstacles.
  5. Web Crawling: Web crawlers use uniformed search algorithms to explore the web and index web pages. BFS is commonly used in web crawling to systematically visit web pages and follow links to discover new pages.

Conclusion

Uniformed search algorithms are essential tools in the field of AI for finding optimal solutions to problems. Each algorithm has its own strengths and weaknesses, and the choice of algorithm depends on the specific problem at hand. By understanding these algorithms, AI practitioners can apply them effectively to a wide range of problems, from pathfinding in games to route planning in logistics.



Similar Reads

Difference Between Depth First Search, Breadth First Search and Depth Limit Search in AI
In AI, search algorithms like Depth First Search (DFS), Breadth First Search (BFS), and Depth Limit Search (DLS) are essential for systematically exploring a search space to find solutions. In this article, we'll compare these algorithms, detailing their features, benefits, and uses. Table of Content Depth First Search (DFS):Breadth First Search (B
4 min read
Genetic Algorithms vs. Local Search Optimization Algorithms in AI
Artificial Intelligence (AI) has revolutionized how we solve problems and optimize systems. Two popular methods in the optimization field are Local Search Optimization (LSO) algorithms and Genetic Algorithms (GAs). While both are used to tackle complex issues, their approaches, uses, and performance characteristics differ significantly. The article
10 min read
Breadth-first Search is a special case of Uniform-cost search
In AI there are mainly two types of search techniques: Un-informed/blind search techniquesInformed search techniques Search algorithms under the Uninformed category are: Breadth-first searchUniform cost searchDepth-first searchDepth limited searchIterative deepening depth-first searchBidirectional search Search algorithms under the Informed categor
6 min read
Difference between Best-First Search and A* Search?
Best-First Search: Best-First search is a searching algorithm used to find the shortest path which uses distance as a heuristic. The distance between the starting node and the goal node is taken as heuristics. It defines the evaluation function for each node n in the graph as f(n) = h(n) where h(n) is heuristics function. A*Search: A*search is a se
2 min read
Comparing Randomized Search and Grid Search for Hyperparameter Estimation in Scikit Learn
Hyperparameters are the parameters that determine the behavior and performance of a machine-learning model. These parameters are not learned during training but are instead set prior to training. The process of finding the optimal values for these hyperparameters is known as hyperparameter optimization, and it is an important step in the developmen
8 min read
CatBoost Grid search and random search
Finding the best model and setting it up for optimum performance may be difficult in the realm of machine learning. Thankfully, methods like Grid Search and Random Search may be used to help. We shall clarify these techniques in this article, focusing on CatBoost, a potential gradient-boosting library. Building precise and effective machine learnin
8 min read
Memory-bounded search ( Memory Bounded Heuristic Search ) in AI
Search algorithms are fundamental techniques in the field of artificial intelligence (AI) that let agents or systems solve challenging issues. Memory-bounded search strategies are necessary because AI systems often encounter constrained memory resources in real-world circumstances. The notion of memory-bound search, often referred to as memory-boun
10 min read
Adversarial Search Algorithms
Adversarial search algorithms are the backbone of strategic decision-making in artificial intelligence, it enables the agents to navigate competitive scenarios effectively. This article offers concise yet comprehensive advantages of these algorithms from their foundational principles to practical applications. Let's uncover the strategies that driv
15+ min read
Alpha-Beta pruning in Adversarial Search Algorithms
In artificial intelligence, particularly in game playing and decision-making, adversarial search algorithms are used to model and solve problems where two or more players compete against each other. One of the most well-known techniques in this domain is alpha-beta pruning. This article explores the concept of alpha-beta pruning, its implementation
6 min read
State Space Search Algorithms for AI Planning
State space search is a fundamental technique in artificial intelligence (AI) for solving planning problems. In AI planning, the goal is to determine a sequence of actions that transitions from an initial state to a desired goal state. State space search algorithms explore all possible states and actions to find an optimal or feasible solution. Thi
8 min read
three90RightbarBannerImg