Site icon NF AI

Search Algorithm in AI: Types, Features and Examples

A search algorithm is an algorithm that helps a user to find and retrieval data from a collection of data. I would do my best to share search algorithm regarding its types, features with examples and its working mechanisms.

How many types of search algorithm in ai?

There are three types of search algorithm:

  1. Uninformed search algorithm
  2. Informed search algorithm
  3. Heuristic search algorithm

1. Uninformed search algorithm

What is Uninformed search algorithm?

The uninformed search algorithm is a search algorithm that does not utilize any information about the search space other than the rules of the game. The algorithm simply expands nodes in the search tree in a pre-determined fashion until a goal node is reached.

How Uninformed search algorithm works?

There is no single uninformed search algorithm, but rather a family of algorithms that all fall under the category of “uninformed search.” Uninformed search algorithms are those that do not make use of any information about the goal state or the path to the goal state when making decisions about which states to explore. Instead, these algorithms simply expand the search tree in a predetermined order until a goal state is found.

Features of Uninformed search algorithm

  1. There is no information about the goal state or the path to the goal state.
  2. There is no information about the search space.
  3. Uninformed search algorithms do not use any domain knowledge.
  4. Uninformed search algorithms are also called blind search algorithms.

Examples of Uninformed search algorithm

1. Brute-force search

Brute-force search methods are known to sometimes lead to suboptimal solutions. Simulated annealing can be used to avoid getting stuck in a local minimum.

def simulated_annealing(graph, start, end, max_steps=100, max_temp=100, cooling_rate=0.003):
    """
    Simulated annealing algorithm.
    """
    path = [start]
    path_length = 0
    temp = max_temp
    while temp > 1:
        new_path = []
        for i in range(1, max_steps):
            new_path.append(random.choice(graph[path[-1]]))
            if new_path[-1] == end:
                path_length = i
                break
        if path_length:
            break
        else:
            path = new_path
        temp *= 1 - cooling_rate
    return path_length, path
2. Breadth-first search

Breadth-first search algorithm to find smallest complete graph. In a breadth-first search, the algorithm starts at the root node and explores all of the neighbor nodes at the present depth prior to moving on to the nodes at the next depth level.

def bfs(graph, start):
    visited, queue = set(), [start]
    while queue:
        vertex = queue.pop(0)
        if vertex not in visited:
            visited.add(vertex)
            queue.extend(graph[vertex] - visited)
    return visited
3. Depth-first search

In a depth-first search, the algorithm starts at the root node and explores as far as possible along each branch before backtracking.

def dfs(graph, start, end, path=[]):
    path = path + [start]
    if start == end:
        return path
    for node in graph[start]:
        if node not in path:
            newpath = dfs(graph, node, end, path)
            if newpath:
                return newpath
    return None
4. Iterative deepening depth-first search

Iterative deepening depth-first search is a type of brute-force search algorithm that first explores the deepest possible path from the root node before backtracking to the root and traversing the tree just as deep but in a different branch. It essentially starts at the root and tries to go as deep as possible until it reaches a leaf node. If it does not find the goal, it backtracks and goes to the next deepest possible path and so on until it either finds a goal or exhausts all possible paths.

def dfs(graph, start, goal):
    stack = [(start, [start])]
    while stack:
        (vertex, path) = stack.pop()
        if vertex not in path:
            if vertex == goal:
                return path
            for next in graph[vertex] - set(path):
                stack.append((next, path + [next]))
    return None

2. Informed Search Algorithm

What is informed search algorithm?

Informed search is a search algorithm that uses knowledge about the problem space in order to make search more efficient. Informed search algorithms rely on heuristics, which are pieces of information about the problem space that can help guide the search.

How informed search algorithm works?

An informed search algorithm is a search algorithm that uses domain-specific knowledge to prune the search space, making the search more efficient. This type of algorithm typically uses a heuristic function to guide the search.

Features of informed search algorithm

An informed search algorithm is an algorithm that is able to take advantage of additional information about the search space to find a solution more quickly than an uninformed search algorithm.

Informed search algorithms typically use some form of heuristic to guide the search. Heuristics are pieces of information that help to guide the search towards a more promising area of the search space. There are many different types of heuristics that can be used, and the choice of heuristic can have a significant impact on the performance of the algorithm.

Informed search algorithms are often used in tasks where the search space is very large and an exhaustive search is not feasible. They are also often used in situations where the goal state is not known in advance and needs to be discovered through exploration of the search space.

Examples of informed search algorithm

Informed search algorithms include best-first search, greedy search, and A*.

Best-first search is an algorithm that expands nodes in a graph in order of their heuristic value. That is, it expands the node that is closest to the goal.

Greedy search is an algorithm that expands nodes in a graph in order of their heuristic value. That is, it expands the node that is closest to the goal.

A* is an algorithm that expands nodes in a graph in order of a cost function f(n), which is a combination of the heuristic value h(n) and the path cost g(n).

def astar(graph, start, goal):
    frontier = PriorityQueue()
    frontier.put(start, 0)
    came_from = {}
    cost_so_far = {}
    came_from[start] = None
    cost_so_far[start] = 0

    while not frontier.empty():
        current = frontier.get()

        if current == goal:
            break

        for next in graph.neighbors(current):
            new_cost = cost_so_far[current] + graph.cost(current, next)
            if next not in cost_so_far or new_cost < cost_so_far[next]:
                cost_so_far[next] = new_cost
                priority = new_cost + heuristic(goal, next)
                frontier.put(next, priority)
                came_from[next] = current

    return came_from, cost_so_far

3. Heuristic Search Algorithm

What is Heuristic search algorithm?

A heuristic search algorithm is an algorithm that attempts to find a path to the goal state as quickly as possible while minimizing the amount of search effort required. Heuristic search algorithms are often used in artificial intelligence applications.

How Heuristic search algorithm works?

Heuristic search algorithm is a type of algorithm that is used to solve problems by searching through a set of possible solutions until it finds a solution that meets or exceed a specified goal.

Features of Heuristic search algorithm

Some features of heuristic search algorithms are as follows:

Examples of Heuristic search algorithm

One example of a heuristic search algorithm is the A* algorithm.

def heuristic(a, b):
    (x1, y1) = a
    (x2, y2) = b
    return abs(x1 - x2) + abs(y1 - y2)
Exit mobile version