VOOZH about

URL: https://www.geeksforgeeks.org/dsa/best-first-search-informed-search/

⇱ Best First Search (Informed Search) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Best First Search (Informed Search)

Last Updated : 20 Mar, 2025

Best First Search is a heuristic search algorithm that selects the most promising node for expansion based on an evaluation function. It prioritizes nodes in the search space using a heuristic to estimate their potential. By iteratively choosing the most promising node, it aims to efficiently navigate towards the goal state, making it particularly effective for optimization problems.

We are given an edge list of a graph where every edge is represented as (u, v, w). Here u, v and w represent source, destination and weight of the edges respectively. We need to do Best First Search of the graph (Pick the minimum cost edge next).

Examples:

Input: source = 0, target = 9
edgeList = [ [ 0, 1, 3 ], [ 0, 2, 6 ], [ 0, 3, 5 ], [ 1, 4, 9 ],
[ 1, 5, 8 ], [ 2, 6, 12 ], [ 2, 7, 14 ], [ 3, 8, 7 ],
[ 8, 9, 5 ], [ 8, 10, 6 ], [ 9, 11, 1 ], [ 9, 12, 10 ], [ 9, 13, 2 ] ]
Output: 0 1 3 2 8 9
Explanation: Following describes the working of best first search:

  • Start at node 0. Among its neighbors, node 1 has the lowest cost (edge cost 3), so move from 0 to 1.
  • From node 1, since a better path forward isn’t available, backtrack to node 0.
  • From node 0, choose the next best neighbor—node 3 (edge cost 5)—and move to node 3.
  • At node 3, find that moving to node 2 leads to a lower incremental cost.
  • Then proceed from node 2 to node 8, and finally from node 8 to node 9 (the target).

Input: source = 0, target = 8
edgeList = [ [ 0, 1, 3 ], [ 0, 2, 6 ], [ 0, 3, 5 ], [ 1, 4, 9 ],
[ 1, 5, 8 ], [ 2, 6, 12 ], [ 2, 7, 14 ], [ 3, 8, 7 ],
[ 8, 9, 5 ], [ 8, 10, 6 ], [ 9, 11, 1 ], [ 9, 12, 10 ], [ 9, 13, 2 ] ]
Output: 0 1 3 2 8
Explanation: Following describes the working of best first search:

  • Start at node 0. Among its neighbors, node 1 has the lowest cost (edge cost 3), so move from 0 to 1.
  • From node 1, since a better path forward isn’t available, backtrack to node 0.
  • From node 0, choose the next best neighbor—node 3 (edge cost 5)—and move to node 3.
  • At node 3, find that moving to node 2 leads to a lower incremental cost.
  • Then proceed from node 2 to node 8 (the target)

Approach:

The idea is to use priority queue or heap to store the costs of edges that have lowest evaluation function value and operate similar to BFS algorithm.

Follow the below given steps:

  • Initialize an empty Priority Queue named pq.
  • Insert the starting node into pq.
  • While pq is not empty:
    • Remove the node u with the lowest evaluation value from pq.
    • If u is the goal node, terminate the search.
    • Otherwise, for each neighbor v of u: If v has not been visited, Mark v as visited and Insert v into pq.
    • Mark u as examined.
  • End the procedure when the goal is reached or pq becomes empty.

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 given the implementation:


Output
0 1 3 2 8 9 

Time Complexity: 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.
Space Complexity: O(n + e), where n is the number of nodes, and e is the number of edges.

Special cases of Best first search:

  1. Greedy Best first search algorithm
  2. A* search algorithm
Comment
Article Tags: