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).
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.