Вы находитесь на странице: 1из 5

Assignment No.

1
Aim: Implement BFS and DFS algorithm for route finding problem.
Objective:
Student will learn:
i)

Uninformed Search

ii)

The basic concepts of BFS and DFS.

iii) To solve Route Finding Problem using BFS and DFS.


Theory:
Uninformed Search Methods:

Uninformed search methods belong to the category of methods that have no prior knowledge of the
problem. They are generally exhaustive methods that are brute force, searching all possible paths. Since
these methods work in the absence of any information, they are also referred to as blind search methods.
These methods can be understood by a very simple example. If would like to search a boy named
Ram in a school, with the number of classes from 1 to 10 and each class having two divisions then the
search would involve going to each and every class till the student named Ram, we are looking for, is
found. Since no information is available, we need to select certain strategy and expand the nodes (here,
the classes) in some predefined order. Bread first search and depth first search are two basic approaches
of expanding and visiting nodes in the absence of any knowledge.
Breadth First Search (BFS)
The search technique in BFS follows the shallow node approach. Here the basic idea is that across the
level of the tree, all the nodes are searched. Queue data structure is used to carry out the search, where
things happen on first in first out basis (FIFO).

Figure 1.1 Breadth first search tree.

ZEAL College of Engineering and Research

Let us take an example of BFS, consider that A is the start state and D is the goal state (Figure 1.2).

A
B
C

D
Figure 1.2 Sample tree input for BFS
In BFS, The step-wise working for the following graph:
Queue

Check

A
Removed A, is it goal? No, add children. So, B and E are added.
BE
E

Removed B, is it goal? No, add children. So, C is added.

EC
C

Removed E, is it goal? No, add children. So, F and G are added.

CFG
FG

Removed C, is it goal? No, add children. So, D is added.

FGD
GD

Removed F, is it goal? No, add children.


Cannot be added, leaf node, so remove next node.

Removed G, is it goal? No, add children.


Cannot be added, leaf node, so remove next node.

Empty

Removed D, is it goal? YES!

The breadth first search algorithm is summarized as follows:


1.

Create a node-list (queue) that initially contains the first node.

2.

Do till a goal state is found


i.

Remove X from node-list. If node-list is empty, then exit.

ZEAL College of Engineering and Research

ii.

Check if this is goal state.

iii.

If yes, return the state.

iv.

If not, get the next level nodes, i.e., nodes reachable from the parent and add to node-list.

Depth-first search (DFS)


In this method, a single branch of the tree is considered at a time, nodes are visited by going through the
depth of the tree from the starting node.

Figure 1.3 Depth first search tree.


With the start node of A and the goal state of D, we have the DFS approach carried out as follows:

A
B
C

D
Figure 1.4 Sample tree input for DFS.

ZEAL College of Engineering and Research

Stack
Step 1:

Top

Pop A, is it goal? No, push its children on the stack.


As successor is pushed.

Top

A
Step 2:

Top

E
C

Pop B, is it goal? No, push its children on the stack.


Bs successor is pushed.

Top

E
Step 3:

Top

E
D

Pop C, is it goal? No, push its children on the stack.


Cs successor is pushed.

Top

E
Step 4:

Top

Pop D, is it goal? YES!

E
The depth first search algorithm is summarized as follows:
1.
2.

If initial state is the final state success : exit


Do till you get success or failure.
i.
Generate a successor, if no more successors can be generated failure.
ii.
Go to state 1 with initial state to be the current generated successor.

Route Finding
Route finding is defined in terms of specified locations and transition along links between them. Let us
consider airline travel planning problem. The objective is to arrive to a destination.
Initial state: Starting point of the journey
Goal state: Final destination
Operators/Actions: Flight from current location to reach the next location.

ZEAL College of Engineering and Research

Advantages of DFS

Requires less memory than BFS since only need to remember the current path

If lucky, can find a solution without examining much of the state space

with cycle-checking, looping can be avoided

Advantages of BFS

Guaranteed to find a solution if one exists in addition, finds optimal (shortest) solution
first

It will not get lost in a blind alley (i.e., does not require backtracking or cycle checking)

Can add cycle checking with very little cost

Conclusion: BFS and DFS algorithm is implemented for route finding problem.

ZEAL College of Engineering and Research

Вам также может понравиться