Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CS Artificial Intelligence

Lab 3 : Search Algorithm: BFS & DFS

1. Objectives
1.1. To familiarize students with basic concepts of un-informed search technique, i.e. Breadth
First Search and Depth First Search
2. Outcome
2.1. Students will be able to:
a) Understand the working of BFS & DFS
b) Program BFS & DFS
3. Introduction
UNINFORMED SEARCH

The term means that the strategies have no additional information about states beyond
that provided in the problem definition. All they can do is generate successors and
distinguish a goal state from a non-goal state.

All search strategies are distinguished by the order in which nodes are expanded.

Breadth-first search

Breadth-first search is a simple strategy in which the root node is expanded first, then all the
successors of the root node are expanded next, then their successors, and so on. In general, all
the nodes are expanded at a given depth in the search tree before any nodes at the next level
are expanded.

In Breadth-first search the shallowest unexpanded node is chosen for expansion. This is
achieved very simply by using a FIFO queue for the frontier. Thus, new nodes (which are always
deeper than their parents) go to the back of the queue, and old nodes, which are shallower
than the new nodes, get expanded first.

Expanded nodes are actually the next possible actions in search for a goal and are kept in a FIFO
queue termed as the Frontier,

Nodes that get visited for expansion are put into the explored list which keeps track of the
order of visiting of nodes.

Algorithm Step-Wise Breakdown:


CS Artificial Intelligence
Lab 3 : Search Algorithm: BFS & DFS

1. Algorithm starts with a starting node being in the frontier queue.

2. In a loop each time a node from the frontier queue’s front is selected for expansion.

3. All possible actions from that node is determined

4. That node is then termed as explored and is added to the explored list.

5. Now, for each action it is determined if it is the goal node? If yes, the goal is reached
and the program is concluded.

6. If that action is not goal, then it is determined that whether it is present in the explored
list or the frontier queue already?

7. If the action in question is not contained in both explored and frontier, then it qualifies
to be pushed in the frontier queue for later expansion.

8. The loop continues from step 2.

Pseudo Code:

LAB TASK 1
CS Artificial Intelligence
Lab 3 : Search Algorithm: BFS & DFS

Your task is to implement Breadth First Search(BFS) in python programming language.

You must implement a function for BFS:

BFS (graph, start, goal)

This function takes input a graph (representing state space), a start node and a goal node.

BFS must return a path, from the start node that leads to the goal node.

Use the following graph to test your function.


Start node being ‘A’ and the Goal ‘F’.
CS Artificial Intelligence
Lab 3 : Search Algorithm: BFS & DFS

Depth-first search

Depth-first search always expands one of the nodes at the deepest level of the tree. Only when
the search hits a dead end (a non-goal node with no expansion) does the search go back and
expand nodes at shallower levels. This strategy can be implemented by GENERAL-SEARCH with
a queuing function that always puts the newly generated states at the front of the queue.
Because the expanded node was the deepest, its successors will be even deeper and are
therefore now the deepest. The progress of the search is illustrated in Figure below:

Depth-first search has very modest memory requirements. As the figure shows, it needs to
store only a single path from the root to a leaf node, along with the remaining unexpanded
sibling nodes for each node on the path.

Algorithm Step-Wise Breakdown:

1. Algorithm starts with a starting node being in the frontier queue.

2. In a loop each time a node from the frontier stack’s top is selected for expansion.

3. All possible actions from that node are determined

4. That node is then termed as explored and is added to the explored list.

5. Now, for each action it is determined if it is the goal node? If yes, the goal is reached
and the program is concluded.

6. If that action is not goal, then it is determined that whether it is present in the explored
CS Artificial Intelligence
Lab 3 : Search Algorithm: BFS & DFS

list or the frontier already?

7. If the action in question is not contained in both explored and frontier, then it qualifies
to be pushed in the frontier for later expansion.

8. The loop continues from step 2.

LAB TASK # 2

Your task is to implement Depth First Search(DFS) in python programming language.

You must implement a function for DFS:

DFS (graph, start, goal)

This function takes input a graph (representing state space), a start node and a goal node.

DFS must traverse the graph .

Use the following graph to test your function.


Start node being ‘A’ and the Goal ‘F’.

You might also like