Open In App

Best First Search (Informed Search)

Last Updated : 21 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In BFS and DFS, when we are at a node, we can consider any of the adjacent as the next node. So both BFS and DFS blindly explore paths without considering any cost function. 

The idea of Best First Search is to use an evaluation function to decide which adjacent is most promising and then explore.

Best First Search falls under the category of Heuristic Search or Informed Search.

Implementation of Best First Search:

We use a priority queue or heap to store the costs of nodes that have the lowest evaluation function value. So the implementation is a variation of BFS, we just need to change Queue to PriorityQueue. 

// Pseudocode for Best First Search
Best-First-Search(Graph g, Node start)
1) Create an empty PriorityQueue
PriorityQueue pq;
2) Insert "start" in pq.
pq.insert(start)
3) Until PriorityQueue is empty
u = PriorityQueue.DeleteMin
If u is the goal
Exit
Else
Foreach neighbor v of u
If v "Unvisited"
Mark v "Visited"
pq.insert(v)
Mark u "Examined"
End procedure

Illustration:

Let us consider the below example:

Best-First-Search-Informed-Search

Best First Search (Informed Search)

  • We start from source “S” and search for goal “I” using given costs and Best First search.
     
  • pq initially contains S
    • We remove S from pq and process unvisited neighbors of S to pq.
    • pq now contains {A, C, B} (C is put before B because C has lesser cost)
       
  • We remove A from pq and process unvisited neighbors of A to pq.
    • pq now contains {C, B, E, D}
       
  • We remove C from pq and process unvisited neighbors of C to pq.
    • pq now contains {B, H, E, D}
       
  • We remove B from pq and process unvisited neighbors of B to pq.
    • pq now contains {H, E, D, F, G}
  • We remove H from pq.  
  • Since our goal “I” is a neighbor of H, we return.

Below is the implementation of the above idea:

C++




// C++ program to implement Best First Search using priority
// queue
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
 
vector<vector<pi> > graph;
 
// Function for adding edges to graph
void addedge(int x, int y, int cost)
{
    graph[x].push_back(make_pair(cost, y));
    graph[y].push_back(make_pair(cost, x));
}
 
// Function For Implementing Best First Search
// Gives output path having lowest cost
void best_first_search(int actual_Src, int target, int n)
{
    vector<bool> visited(n, false);
    // MIN HEAP priority queue
    priority_queue<pi, vector<pi>, greater<pi> > pq;
    // sorting in pq gets done by first value of pair
    pq.push(make_pair(0, actual_Src));
    int s = actual_Src;
    visited[s] = true;
    while (!pq.empty()) {
        int x = pq.top().second;
        // Displaying the path having lowest cost
        cout << x << " ";
        pq.pop();
        if (x == target)
            break;
 
        for (int i = 0; i < graph[x].size(); i++) {
            if (!visited[graph[x][i].second]) {
                visited[graph[x][i].second] = true;
                pq.push(make_pair(graph[x][i].first,graph[x][i].second));
            }
        }
    }
}
 
// Driver code to test above methods
int main()
{
    // No. of Nodes
    int v = 14;
    graph.resize(v);
 
    // The nodes shown in above example(by alphabets) are
    // implemented using integers addedge(x,y,cost);
    addedge(0, 1, 3);
    addedge(0, 2, 6);
    addedge(0, 3, 5);
    addedge(1, 4, 9);
    addedge(1, 5, 8);
    addedge(2, 6, 12);
    addedge(2, 7, 14);
    addedge(3, 8, 7);
    addedge(8, 9, 5);
    addedge(8, 10, 6);
    addedge(9, 11, 1);
    addedge(9, 12, 10);
    addedge(9, 13, 2);
 
    int source = 0;
    int target = 9;
 
    // Function call
    best_first_search(source, target, v);
 
    return 0;
}


Java




// Java program to implement Best First Search using priority
// queue
import java.util.ArrayList;
import java.util.PriorityQueue;
 
public class GFG
{
  static ArrayList<ArrayList<edge> > adj = new ArrayList<>();
 
  // Function for adding edges to graph
  static class edge implements Comparable<edge>
  {
    int v, cost;
    edge(int v, int cost)
    {
      this.v = v;
      this.cost = cost;
    }
    @Override public int compareTo(edge o)
    {
      if (o.cost < cost) {
        return 1;
      }
      else
        return -1;
    }
  }
 
  public GFG(int v)
  {
    for (int i = 0; i < v; i++) {
      adj.add(new ArrayList<>());
    }
  }
 
  // Function For Implementing Best First Search
  // Gives output path having lowest cost
  static void best_first_search(int source, int target, int v)
  {
 
    // MIN HEAP priority queue
    PriorityQueue<edge> pq = new PriorityQueue<>();
    boolean visited[] = new boolean[v];
    visited = true;
 
    // sorting in pq gets done by first value of pair
    pq.add(new edge(source, -1));
    while (!pq.isEmpty()) {
      int x = pq.poll().v;
 
      // Displaying the path having lowest cost
      System.out.print(x + " ");
      if (target == x) {
        break;
      }
      for (edge adjacentNodeEdge : adj.get(x)) {
        if (!visited[adjacentNodeEdge.v]) {
          visited[adjacentNodeEdge.v] = true;
          pq.add(adjacentNodeEdge);
        }
      }
    }
  }
  void addedge(int u, int v, int cost)
  {
    adj.get(u).add(new edge(v, cost));
    adj.get(v).add(new edge(u, cost));
  }
 
  // Driver code to test above methods
  public static void main(String args[])
  {
 
    // No. of Nodes
    int v = 14;
 
    // The nodes shown in above example(by alphabets) are
    // implemented using integers addedge(x,y,cost);
    GFG graph = new GFG(v);
    graph.addedge(0, 1, 3);
    graph.addedge(0, 2, 6);
    graph.addedge(0, 3, 5);
    graph.addedge(1, 4, 9);
    graph.addedge(1, 5, 8);
    graph.addedge(2, 6, 12);
    graph.addedge(2, 7, 14);
    graph.addedge(3, 8, 7);
    graph.addedge(8, 9, 5);
    graph.addedge(8, 10, 6);
    graph.addedge(9, 11, 1);
    graph.addedge(9, 12, 10);
    graph.addedge(9, 13, 2);
 
    int source = 0;
    int target = 9;
 
    // Function call
    best_first_search(source, target, v);
  }
}
 
// This code is contributed by Prithi_Dey


Python3




from queue import PriorityQueue
v = 14
graph = [[] for i in range(v)]
 
# Function For Implementing Best First Search
# Gives output path having lowest cost
 
 
def best_first_search(actual_Src, target, n):
    visited = [False] * n
    pq = PriorityQueue()
    pq.put((0, actual_Src))
    visited[actual_Src] = True
     
    while pq.empty() == False:
        u = pq.get()[1]
        # Displaying the path having lowest cost
        print(u, end=" ")
        if u == target:
            break
 
        for v, c in graph[u]:
            if visited[v] == False:
                visited[v] = True
                pq.put((c, v))
    print()
 
# Function for adding edges to graph
 
 
def addedge(x, y, cost):
    graph[x].append((y, cost))
    graph[y].append((x, cost))
 
 
# The nodes shown in above example(by alphabets) are
# implemented using integers addedge(x,y,cost);
addedge(0, 1, 3)
addedge(0, 2, 6)
addedge(0, 3, 5)
addedge(1, 4, 9)
addedge(1, 5, 8)
addedge(2, 6, 12)
addedge(2, 7, 14)
addedge(3, 8, 7)
addedge(8, 9, 5)
addedge(8, 10, 6)
addedge(9, 11, 1)
addedge(9, 12, 10)
addedge(9, 13, 2)
 
source = 0
target = 9
best_first_search(source, target, v)
 
# This code is contributed by Jyotheeswar Ganne


C#




using System;
using System.Collections.Generic;
 
using System.Collections;
// C# program to implement Best First Search using priority
// queue
 
 
class HelloWorld {
     
    public static LinkedList<Tuple<int, int>>[] graph;
     
    // Function for adding edges to graph
    public static void addedge(int x, int y, int cost)
    {
        graph[x].AddLast(new Tuple<int, int>(cost, y));
        graph[y].AddLast(new Tuple<int, int>(cost, x));
    }
 
    // Function for finding the minimum weight element.
    public static Tuple<int,int> get_min(LinkedList<Tuple<int,int>> pq){
        // Assuming the maximum wt can be of 1e5.
        Tuple<int,int> curr_min = new Tuple<int,int>(100000, 100000);
        foreach(var ele in pq){
            if(ele.Item1 == curr_min.Item1){
                if(ele.Item2 < curr_min.Item2){
                    curr_min = ele;
                }
            }
            else{
                if(ele.Item1 < curr_min.Item1){
                    curr_min = ele;
                }
            }
        }
         
        return curr_min;
    }
     
    // Function For Implementing Best First Search
    // Gives output path having lowest cost
    public static void best_first_search(int actual_Src, int target, int n)
    {
        int[] visited = new int[n];
        for(int i = 0; i < n; i++){
            visited[i] = 0;
        }
         
        // MIN HEAP priority queue
        LinkedList<Tuple<int, int>> pq = new LinkedList<Tuple<int,int>>();
 
        // sorting in pq gets done by first value of pair
        pq.AddLast(new Tuple<int, int> (0, actual_Src));
        int s = actual_Src;
        visited[s] = 1;
        while (pq.Count > 0) {
             
            Tuple<int,int> curr_min = get_min(pq);
            int x = curr_min.Item2;
            pq.Remove(curr_min);
             
            Console.Write(x + " ");
            // Displaying the path having lowest cost
            if (x == target)
                break;
 
            LinkedList<Tuple<int,int>> list = graph[x];
            foreach(var val in list)
            {
                if (visited[val.Item2] == 0) {
                    visited[val.Item2] = 1;
                    pq.AddLast(new Tuple<int,int>(val.Item1, val.Item2));
                }
            }
             
        }
    }
     
    static void Main() {
        // No. of Nodes
        int v = 14;
        graph = new LinkedList<Tuple<int, int>>[v];
        for (int i = 0; i < graph.Length; ++i){
            graph[i] = new LinkedList<Tuple<int, int>>();
        }
 
        // The nodes shown in above example(by alphabets) are
        // implemented using integers addedge(x,y,cost);
        addedge(0, 1, 3);
        addedge(0, 2, 6);
        addedge(0, 3, 5);
        addedge(1, 4, 9);
        addedge(1, 5, 8);
        addedge(2, 6, 12);
        addedge(2, 7, 14);
        addedge(3, 8, 7);
        addedge(8, 9, 5);
        addedge(8, 10, 6);
        addedge(9, 11, 1);
        addedge(9, 12, 10);
        addedge(9, 13, 2);
 
        int source = 0;
        int target = 9;
 
        // Function call
        best_first_search(source, target, v);
    }
}
 
// The code is contributed by Nidhi goel.


Javascript




<script>
// javascript program to implement Best First Search using priority
// queue
 
 
// Function for adding edges to graph
function addedge(x, y, cost)
{
    graph[x].push([cost, y]);
    graph[y].push([cost, x]);
}
 
// Function For Implementing Best First Search
// Gives output path having lowest cost
function best_first_search(actual_Src, target, n)
{
    let visited = new Array(n).fill(false);
     
    // MIN HEAP priority queue
    let pq = [];
 
    // sorting in pq gets done by first value of pair
    pq.push([0, actual_Src]);
    let s = actual_Src;
    visited[s] = true;
    while (pq.length > 0) {
        let x = pq[0][1];
        // Displaying the path having lowest cost
        document.write(x + " ");
        pq.shift();
        if (x == target) break;
 
        for (let i = 0; i < graph[x].length; i++) {
            if (visited[graph[x][i][1]] == 0) {
                visited[graph[x][i][1]] = true;
                pq.push([graph[x][i][0],graph[x][i][1]]);
                pq.sort((a, b)=>{
                    if(a[0] != b[0]){
                        return a[0]-b[0];
                    }
                    return a[1]-b[1];
                });
            }
        }
    }
}
 
// Driver code to test above methods
 
// No. of Nodes
let v = 14;
let graph = new Array(v);
for(let i = 0; i < v; i++){
    graph[i] = new Array(0);
}
 
// The nodes shown in above example(by alphabets) are
// implemented using integers addedge(x,y,cost);
addedge(0, 1, 3);
addedge(0, 2, 6);
addedge(0, 3, 5);
addedge(1, 4, 9);
addedge(1, 5, 8);
addedge(2, 6, 12);
addedge(2, 7, 14);
addedge(3, 8, 7);
addedge(8, 9, 5);
addedge(8, 10, 6);
addedge(9, 11, 1);
addedge(9, 12, 10);
addedge(9, 13, 2);
 
let source = 0;
let target = 9;
 
// Function call
best_first_search(source, target, v);
 
// The code is contributed by Nidhi goel.
</script>


Output

0 1 3 2 8 9 

Analysis : 

  • The worst-case time complexity for Best First Search is O(n * log n) where n is the number of nodes. In the worst case, we may have to visit all nodes before we reach goal. Note that priority queue is implemented using Min(or Max) Heap, and insert and remove operations take O(log n) time.
  • The performance of the algorithm depends on how well the cost or evaluation function is designed.

Special cases of Best first search:

  1. Greedy Best first search algorithm
  2. A* search algorithm

 



Similar Reads

Difference between Informed and Uninformed Search in AI
What is an Informed Search in AI? algorithms have information on the goal state which helps in more efficient searching. This information is obtained by a function that estimates how close a state is to the goal state. Informed search in AI is a type of search algorithm that uses additional information to guide the search process, allowing for more
5 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
Greedy Best first search algorithm
What is the Greedy-Best-first search algorithm?Greedy Best-First Search is an AI search algorithm that attempts to find the most promising path from a given starting point to a goal. It prioritizes paths that appear to be the most promising, regardless of whether or not they are actually the shortest path. The algorithm works by evaluating the cost
5 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
Iterative Deepening Search(IDS) or Iterative Deepening Depth First Search(IDDFS)
There are two common ways to traverse a graph, BFS and DFS. Considering a Tree (or Graph) of huge height and width, both BFS and DFS are not very efficient due to following reasons. DFS first traverses nodes going through one adjacent of root, then next adjacent. The problem with this approach is, if there is a node close to root, but not in first
10 min read
FIFO (First-In-First-Out) approach in Programming
FIFO is an abbreviation for first in, first out. It is a method for handling data structures where the first element is processed first and the newest element is processed last. Real-life example: In this example, following things are to be considered: There is a ticket counter where people come, take tickets and go.People enter a line (queue) to g
5 min read
Find the first day of a given year from a base year having first day as Monday
Given two integers Y and B representing two years, the task is to find the day of the week in which 1st January of the year Y lies assuming that 1st January of the year B was a Monday. Examples: Input:Y = 2020B = 1900Output:WednesdayExplanation:01/01/2020 was a Wednesday considering that 01/01/1900 was a Monday Input:Y = 2020B = 1905Output:Thursday
6 min read
Count distinct sequences obtained by replacing all elements of subarrays having equal first and last elements with the first element any number of times
Given an array arr[] consisting of N integers, the task is to find the number of different sequences that can be formed after performing the below operation on the given array arr[] any number of times. Choose two indices i and j such that arr[i] is equal to arr[j] and update all the elements in the range [i, j] in the array to arr[i]. Examples: In
7 min read
Minimize first node of Linked List by deleting first or adding one deleted node at start
Given a singly linked list, and an integer K, the task is to make the first node value as minimum as possible in K operations where in each operation: Select the first node of the linked list and remove it.Add a previously removed node at the start of the linked list. Examples: Input: list: 1-&gt;4-&gt;2-&gt;5-&gt;3, K=4 Output:1Explanation: 1st op
8 min read
Maximize first element of Array by deleting first or adding a previously deleted element
Given an array arr[] of size N, and an integer K, the task is to maximize the first element of the array in K operations where in each operation: If the array is not empty, remove the topmost element of the array.Add any one of the previously removed element back at the starting of the array. Examples: Input: arr[] = [5, 2, 2, 4, 0, 6], K = 4Output
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg