# 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 techniques
- Informed search techniques

**Search algorithms under the** **Uninformed category are:**

- Breadth-first search
- Uniform cost search
- Depth-first search
- Depth limited search
- Iterative deepening depth-first search
- Bidirectional search

**Search algorithms under **the **Informed category are:**

- Best first search
- A* search

Now, we shall define the steps of BFS and UCS searches, then we will derive BFS from the UCS search algorithm.

## Breadth-first** search algorithm: **

Open list is implemented using Queue for BFS.

- Add the initial node x0 to the open list.
- Take a node x from the front-end of open list. If the open list is empty then we can’t proceed further hence can’t find the target node.

(a) If open list is empty, stop with faiture.

(b) On the other hand, if x is the target node, stop with success.- Expand x to obtain a set S of child nodes of x, and put x into the closed list.
- For each node x’ in set S, if it is not in the closed list, add it to the open list along with the edge (x, x’).
- Return to Step 2.

**Property of BFS:**

- It is a complete search in the sense that it can always find the solution in the finite steps in the finite search graph.
- The transition cost function is d(x, x’) = 1 or any constant.

**Advantages of Breadth-First Search:**

- It will find the shortest path between the starting point and any other reachable points.
- It always finds optimal solutions.
- There is nothing like a useless path in BFS since it searches level by level.
- Finds the closest goal in less time.

## Uniform cost search algorithm:

- Add the initial node x0 and its cost C(x0) to the open list.
- Get a node x from the top of the open list. (a) If the open list is empty then we can’t proceed further and hence can’t find the solution. So, if open list is empty then stop with failure. (b) If x is the target node then we can stop here. So, if x is target node then stop with success.
- Expand x to get a set S of the child nodes of x, and move x to the closed list.
- Pick each x’ from the set S which is not present in the closed list, find its accumulated cost: C(x’) = C(x) + d(x, x’).
- If x’ is not present in the open list: Add x’, C(x’) and (x, x’) to the open list. (a) If x’ in already present in the open list, update its cost C(x’) and link (x, x’) in the open list if the new cost is smaller than old cost.
- Sort the open list based on the node costs, and return to step-2.

**Properties of UCS:**

- We can always find the best path from the initial node to the current node.
- C(x) is the accumulated cost of node x, from the initial node.
- d(x, x’) is the cost for state transition from node x to node x’.
- If we set d(x, x’)=1 for all edges, UCS becomes equivalent to Breadth-First Search.

**Deriving BFS from UCS: **

The cost function in BFS: C(x’) = C(x) + 1 …(1)

The cost function in UCS: C(x’) = C(x) + d(x, x’) …(2)

C(x0) = 1 in BFS and UCS …(3)

If we make transition cost from node x to x’ = 1 then;

UCS: C(x’) = C(x) + 1 …(4)

Thus, from (1) and (4), we conclude that BFS is derived from UCS by making transition cost as 1.

Therefore, BFS is a special case of UCS.

## How uniform-cost search is a special case of A* search – Artificial Intelligence

- Add the initial node x0 and its cost C(x0) to the open list.
- Get a node x from the top of the open list. (a) If the open list is empty then we can’t proceed further and hence can’t find the solution. So, if open list is empty then stop with failure. (b) If x is the target node then we can stop here. So, if x is target node then stop with success.
- Expand x to get a set S of the child nodes of x, and move x to the closed list.
- Pick each x’ from the set S which is not present in the closed list, find its accumulated cost: C(x’) = C(x) + d(x, x’).
- If x’ is not present in the open list: Add x’, C(x’) and (x, x’) to the open list. (a) If x’ in already present in the open list, update its cost C(x’) and link (x, x’) in the open list if the new cost is smaller than old cost.
- Sort the open list based on the node costs, and return to step-2.

**Properties of UCS:**

- We can always find the best path from the initial node to the current node.
- C(x) is the accumulated cost of node x, from the initial node.
- d(x, x’) is the cost for state transition from node x to node x’.
- If we set d(x, x’)=1 for all edges, UCS becomes equivalent to Breadth-First Search.

## A* search algorithm:

- Put the initial node x0 and its cost F(x0)=H(x0) to the open list.
- Get a node x from the top of the open list.

- (a) If the open list is empty then we can’t proceed further and hence can’t find the solution. So, if open list is empty then stop with failure.
- (b) If x is the target node then we can stop here. So, if x is target node then stop with success.
- Expand x to get a set S of child nodes. Put x to the closed list.
- H(x) = Heuristic cost of reaching upto state x from initial state.
- d(x, x’) = cost of state transition from state x to state x’.
- For each x’ in S, find its cost: C(x’)=C(x)+d(x,x’)

- (a) If x’ is in the closed list but the new cost is smaller than the old one, move x’ to the open list and update the edge (x,x’) and the cost.
- (b) Else, if x’ is in the open list, but the new cost is smaller than the old one, update the edge (x,x’) and the cost.
- (c)Else (if x’ is not in the open list nor in the closed list), put x’ along with the edge (x,x’) and the cost F to the open list.
- (d) Sort the open list according to the costs of the nodes, and return to Step 2.

A* algorithm promises the best optimal solution.

Deriving UCS from A* algorithm:Cost function in UCS: C(x’) = C(x} + d(x, x’) …(1)

Cost function in A*: F(x’) = F(x) + d(x, x’) + [H(x’) – H(x)] …(2)

Put H(x) = 0, for all nodes x.

Substitute H(x) = 0, in equation (2).

We get: F(x’) = F(x) + d(x, x’) + [0 – 0]

where F(x) = cost of reach state x.

Hence, F(x) = C(x)

Thus, A* becomes UCS if heuristic cost of every node is 0. So, UCS is a special case of A* algorithm.

Attention reader! Don’t stop learning now. Get hold of all the important Machine Learning Concepts with the **Machine Learning Foundation Course** at a student-friendly price and become industry ready.