Skip to main content

Search Algorithms Explained with Python

Introduction to Search Algorithms

Search algorithms are foundational tools in artificial intelligence (AI) and computer science. They enable problem-solving agents to explore complex environments, discover solutions, and make decisions. A search algorithm systematically examines possible paths in a problem space to find a solution, such as reaching a goal state from a given start state.

Broadly, search algorithms are divided into two main categories:

  • Uninformed Search Algorithms: These algorithms do not have any domain-specific knowledge beyond the problem definition. They explore the search space blindly, treating all nodes equally without estimating the direction of the goal. While often inefficient in large or complex environments, they are guaranteed to find a solution (if one exists) under certain conditions, and they form the conceptual foundation for more advanced techniques.

  • Informed Search Algorithms: Also known as heuristic search algorithms, these methods use additional information (heuristics) to evaluate the desirability of different nodes. This allows the algorithm to prioritize certain paths and reach the goal more efficiently.

This document provides detailed explanations, complexity analysis, and Python implementations of both uninformed and informed search algorithms. Each section is supported by example use cases to enhance understanding.


1. Uninformed Search Algorithms

1.1 Breadth-First Search (BFS)

Principle: Explore all the nodes at the present depth before moving on to nodes at the next depth level. It uses a queue (FIFO).

Time Complexity: $O(b^d)$
Space Complexity: $O(b^d)$

Python Code:

from collections import deque

def bfs(graph, start):
    visited = set()
    queue = deque([start])

    while queue:
        node = queue.popleft()
        if node not in visited:
            print(node, end=" ")
            visited.add(node)
            queue.extend(graph[node])

# Example usage:
graph = {
    'A' : ['B','C'],
    'B' : ['D', 'E'],
    'C' : ['F'],
    'D' : [],
    'E' : ['F'],
    'F' : []
}
bfs(graph, 'A')

1.2 Depth-First Search (DFS)

Principle: Explore as far as possible along each branch before backtracking. It uses a stack (LIFO).

Time Complexity: $O(b^d)$
Space Complexity: O(bd)

Python Code:

def dfs(graph, start, visited=None):
    if visited is None:
        visited = set()
    visited.add(start)
    print(start, end=" ")

    for neighbor in graph[start]:
        if neighbor not in visited:
            dfs(graph, neighbor, visited)

# Example usage:
dfs(graph, 'A')

1.3 Uniform Cost Search (UCS)

Principle: Like BFS but uses cost instead of depth. It always expands the lowest cost node.

Time Complexity: $O(b^(1+C*/ε))$ where C* is the cost of the optimal solution.
Space Complexity: $O(b^(1+C*/ε))$

Python Code:

import heapq

def ucs(graph, start, goal):
    queue = [(0, start)]
    visited = set()

    while queue:
        cost, node = heapq.heappop(queue)
        if node == goal:
            print(f"Reached {goal} with cost {cost}")
            return
        if node not in visited:
            visited.add(node)
            for neighbor, weight in graph[node]:
                heapq.heappush(queue, (cost + weight, neighbor))

# Example usage:
graph_cost = {
    'A': [('B', 1), ('C', 5)],
    'B': [('D', 3), ('E', 1)],
    'C': [('F', 2)],
    'D': [],
    'E': [('F', 1)],
    'F': []
}
ucs(graph_cost, 'A', 'F')

1.4 Depth-Limited Search (DLS)

Principle: DFS with a maximum depth limit to avoid going too deep.

Time Complexity: $O(b^l)$
Space Complexity: O(bl)

Python Code:

def dls(graph, node, goal, limit):
    if node == goal:
        print(f"Found {goal}")
        return True
    if limit <= 0:
        return False
    for neighbor in graph[node]:
        if dls(graph, neighbor, goal, limit - 1):
            return True
    return False

# Example usage:
dls(graph, 'A', 'F', 3)

1.5 Iterative Deepening Depth-First Search (IDDFS)

Principle: Repeatedly apply DLS with increasing limits.

Time Complexity: $O(b^d)$
Space Complexity: O(bd)

Python Code:

def iddfs(graph, start, goal, max_depth):
    for depth in range(max_depth):
        if dls(graph, start, goal, depth):
            return True
    return False

# Example usage:
iddfs(graph, 'A', 'F', 5)

2. Informed Search Algorithms

2.1 Greedy Best-First Search

Principle: Uses heuristic function h(n) to choose the node that appears to be closest to the goal.

Time Complexity: $O(b^m)$ (where m is maximum depth of the search tree)
Space Complexity: $O(b^m)$

Python Code:

def greedy_best_first(graph, start, goal, heuristic):
    queue = [(heuristic[start], start)]
    visited = set()

    while queue:
        _, node = heapq.heappop(queue)
        if node == goal:
            print(f"Reached {goal}")
            return
        if node not in visited:
            visited.add(node)
            for neighbor in graph[node]:
                heapq.heappush(queue, (heuristic[neighbor], neighbor))

# Example usage:
heuristic = {'A': 5, 'B': 4, 'C': 2, 'D': 6, 'E': 1, 'F': 0}
graph_simple = {
    'A': ['B', 'C'],
    'B': ['D', 'E'],
    'C': ['F'],
    'D': [],
    'E': ['F'],
    'F': []
}
greedy_best_first(graph_simple, 'A', 'F', heuristic)

2.2 A* Search

Principle: Combines UCS and Greedy Best-First by using f(n) = g(n) + h(n), where g(n) is cost so far and h(n) is estimated cost to goal.

Time Complexity: Depends on heuristic quality, worst case $O(b^d)$
Space Complexity: $O(b^d)$

Python Code:

def a_star(graph, start, goal, heuristic):
    queue = [(heuristic[start], 0, start)]
    visited = set()

    while queue:
        f, cost, node = heapq.heappop(queue)
        if node == goal:
            print(f"Reached {goal} with total cost {cost}")
            return
        if node not in visited:
            visited.add(node)
            for neighbor, weight in graph[node]:
                g = cost + weight
                h = heuristic[neighbor]
                heapq.heappush(queue, (g+h, g, neighbor))

# Example usage:
graph_weighted = {
    'A': [('B', 1), ('C', 5)],
    'B': [('D', 3), ('E', 1)],
    'C': [('F', 2)],
    'D': [],
    'E': [('F', 1)],
    'F': []
}
heuristic2 = {'A': 5, 'B': 4, 'C': 2, 'D': 6, 'E': 1, 'F': 0}
a_star(graph_weighted, 'A', 'F', heuristic2)

3. References

Comments

Popular

How to Fine-Tune LLaMA 3.2-1B-Instruct for Korean Instruction Tasks with LoRA and Hugging Face

LLaMA 3.2-1B-Instruct is a lightweight instruction-tuned language model released by Meta. It is designed to handle a wide range of instruction-based tasks with relatively low computational resources. Although the model was trained with multilingual capabilities, its performance on languages not included in its training set—such as Korean—is limited. This tutorial demonstrates how to fine-tune this open-source model on a Korean dataset using Hugging Face Transformers and PEFT (specifically LoRA), enabling it to better respond to Korean instructions. 1. Prerequisites Before running the example code below, ensure you have the following libraries installed: pip install torch transformers datasets peft accelerate mlflow huggingface_hub To use the LLaMA model or KoAlpaca datasets, you'll need a Hugging Face token. Additionally, you may need to handle potential CUDA Out-Of-Memory (OOM) errors. The following code takes care of both: from huggingface_hub import login login("y...

RF-DETR: Overcoming the Limitations of DETR in Object Detection

RF-DETR (Region-Focused DETR), proposed in April 2025, is an advanced object detection architecture designed to overcome fundamental drawbacks of the original DETR (DEtection TRansformer) . In this technical article, we explore RF-DETR's contributions, architecture, and how it compares with both DETR and the improved model D-FINE . We also provide experimental benchmarks and discuss its real-world applicability. RF-DETR Architecture diagram for object detection Limitations of DETR DETR revolutionized object detection by leveraging the Transformer architecture, enabling end-to-end learning without anchor boxes or NMS (Non-Maximum Suppression). However, DETR has notable limitations: Slow convergence, requiring heavy data augmentation and long training schedules Degraded performance on low-resolution objects and complex scenes Lack of locality due to global self-attention mechanisms Key Innovations in RF-DETR RF-DETR intr...

SVD and Truncated SVD Explained: Theory, Python Examples, and Applications in Machine Learning & Deep Learning

Singular Value Decomposition (SVD) is a matrix factorization method widely used in mathematics, engineering, and economics. Since it's a crucial concept applied in accelerating matrix computations and data compression, it's worth studying at least once. SVD (Singular Value Decomposition) Theory Singular Value Decomposition (SVD) is a matrix factorization technique applicable to any real or complex matrix. Any matrix  A (m×n)  can be decomposed as follows: $A = U * \Sigma * V^T$ U : Orthogonal matrix composed of left singular vectors $(m \times m)$ Σ : Diagonal matrix $(m \times n)$ with singular values on the diagonal $V^T$ : Transposed matrix of right singular vectors $(n \times n)$ The singular values represent the energy or information content of matrix A, enabling tasks like dimensionality reduction or noise filtering. Truncated SVD Truncated SVD approximates the original matrix using only the top  k  singular values and corresponding singular vectors: $A \approx...