BFS, DFS, Ucs

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Simple Search

Algorithms
Random Search
Algorithm
Terminologies:
Open List / Frontier:
The set of all leaf nodes available for expansion at any given point
is called the frontier.

Closed List / Explored set:


The set of all the expanded nodes.
Search with Open List
Algorithm:
Def Search_open_list()
Open = initial node
While Open != Empty() then
Pick some node ‘N’ from Open
Open = Open – {N}
If GOALTEST(N) = True then
Return N
Else
Open = Open U {MOVEGEN(N)}
Return FAILURE
Example:
Example:
Search with Closed List
Def Search_closed_list()
Open = initial node
Closed = {}
While Open!=Empty() then
Pick some node ‘N’ from Open
Open = Open – {N}
Closed = Closed U {N}
If GOALTEST = True Then
Return N
Else
Open = Open U {MOVEGEN(N) – Closed}
Return FAILURE
Types of searches
1. Uninformed Search/Blind Search :The search strategy, which
has no information about the number of steps or path cost
from the current state to goal state, but can only distinguish
a goal state from a nongoal state is known as Uninformed
search strategy or blind search strategy.

2. Informed Search: Strategies that know whether one non-goal


state is more promising than the other is called as heuristic/
informed search techniques. The name means that this kind
of search strategies are aware whether the search process is
moving in the direction of goal or in the opposite direction of
goal.
Informed Search vs. Uninformed Search
Parameters Informed Search Uninformed Search
Known as It is also known as Heuristic Search. It is also known as Blind Search.

It uses knowledge for the searching It doesn’t use knowledge for the
Using Knowledge
process. searching process.

It finds solution slow as compared to


Performance It finds a solution more quickly.
an informed search.

Completion It may or may not be complete. It is always complete.


Cost Factor Cost is low. Cost is high.
•Greedy Search
•Depth First Search (DFS)
•A* Search
Examples of Algorithms •Breadth First Search (BFS)
•AO* Search
•Branch and Bound
•Hill Climbing Algorithm
It consumes less time because of It consumes moderate time
Time
quick searching. because of slow searching.

It is more lengthy while


Implementation It is less lengthy while implemented.
implemented.

It is more efficient as efficiency takes


It is comparatively less efficient as
into account cost and performance.
Efficiency incurred cost is more and the speed
The incurred cost is less and speed of
of finding the solution is slow.
finding solutions is quick.

Computational Computational requirements are Comparatively higher


requirements lessened. computational requirements.

Having a wide scope in terms of Solving a massive search task is


Size of search problems
handling large search problems. challenging.
Uninformed Search
• The uniformed search is also known as Blind Search. Therefore, we
can also say that this type of search implies that these provide no
extra information reading the states except information given by the
definition of problem. Also, blind search might generate successor
states distinguishing between a goal state and a non-goal state.

• Blind search or brute force is a uniformed exploration of the search


space, which does not take into account either execution efficiency or
planning efficiency. Blind search is also known as uniform search.
Breadth first search
• A basic technique of traversing a graph is breadth-first search
or BFS. It first finds the shortest path always; however, this
might also lead to using of more memory. The state space is
shown in the form of a tree in this type of search.
• One can obtain the solution by traversing through the tree.
The nodes of the tree display the start value or starting state,
various intermediate states and the final state.
• Each node in the search tree is developed breadth-wise at
each level.
Breadth first search ALGORITHM
Begin
Initially, OPEN has the root node and CLOSE is EMPTY OPEN=[start]
CLOSE=[ ]
The loop is continued till OPEN list is not EMPTY While OPEN [] do
Begin
Remove the leftmost state from OPEN and let it be X
If X is GOAL then Return SUCCESS Else
Begin
Generate children of x
Substitute x on close.
Discard the children of x if already on OPEN or CLOSE.
Place the remaining children on the right side of the OPEN.
(For BFS, we place the children to the right side of OPEN End)
End
Breadth-First Strategy
1 1

2 3 2 3

4 5 6 7 4 5 6 7

FRINGE = (1) FRINGE = (2, 3)


FRINGE = (3, 4, 5) FRINGE = (4, 5, 6, 7)
Question:
Find the spanning tree of the following state space graph using BFS
(i) Without closed list
(ii) With closed list
Also mention the traversing path.
ADVANTAGES
• In this procedure, at any way it will find the GOAL.
• It does not follow a single unfruitful path for a long time.
• It finds the minimal solution in case of multiple paths.
DISADVANTAGES
1. Large memory space is consumed by BFS.
2. Time complexity is more.
3. It also has long pathways, when all the paths to a destination are on
approximately the same search depth.
Depth first search
ALGORITHM
Begin
Initially, OPEN has the root node and CLOSE is EMPTY
OPEN=[start] CLOSE=[]
The loop is continued till OPEN list is not EMPTY
While OPEN!=[] Begin
The leftmost state is removed from OPEN and is called as X If X=GOAL then
Return SUCCESS Else
Begin
Children of X is generated.
X is placed on close.
The children of X is discarded in case already on OPEN or CLOSE.
The remaining children are placed on the left side of OPEN.
End Return Fall End
Depth first search Strategy
Question:
Find the spanning tree of the following state space graph using DFS
(i) Without closed list
(ii) With closed list
Also mention the traversing path.
Question:
Find the spanning tree of the following state space graph using DFS
With closed list. Also mention the traversing path.
Difference between BFS and DFS
Dijkstra’s Algorithm for shortest path
Uniform cost search
When all step costs are equal, breadth-first search is optimal
because it always expands the shallowest unexpanded node.
By a simple extension, we can find an algorithm that is optimal
with any step-cost function.
Instead of expanding the shallowest node, uniform-cost
search expands the node n with the lowest path cost g(n).
This is done by storing the frontier as a priority queue ordered
by g.
Uniform cost search
▪ Each arc has some cost c   > 0
▪ The cost of the path to each fringe node N is
g(N) =  costs of arcs
▪ The goal is to generate a solution path of minimal
cost
▪ The queue FRINGE is sorted in increasing cost
Uniform cost search
• We factor in the cost of each step (e.g., distance form current state to
the neighbors). Assumption: costs are non-negative.
g(n) = cost so far to reach n

• Queue → ordered by cost

• If all the steps are the same → breadth-first search is optimal since it
always expands the shallowest (least cost)!

• Uniform-cost search → expand first the nodes with lowest cost


(instead of depth).
Uniform cost search (Example )
Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL
Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}


Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}


Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}

4. {(A-D-E, 5), (A-D-F, 5), (A-B-C, 6)} E {A, D, B}


Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}

4. {(A-D-E, 5), (A-D-F, 5), (A-B-C, 6)} E {A, D, B}

5. {(A-D-F, 5), (A-B-C, 6), (A-D-E-B, F {A, D, B, E}


9)}
*here B is already explored
Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}

4. {(A-D-E, 5), (A-D-F, 5), (A-B-C, 6)} E {A, D, B}

5. {(A-D-F, 5), (A-B-C, 6), (A-D-E-B, F {A, D, B, E}


9)}
*here B is already explored

6. {(A-B-C, 6), (A-D-F-G,8)} C {A, D, B, E, F}


Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}

4. {(A-D-E, 5), (A-D-F, 5), (A-B-C, 6)} E {A, D, B}

5. {(A-D-F, 5), (A-B-C, 6), (A-D-E-B, F {A, D, B, E}


9)}
*here B is already explored

6. {(A-B-C, 6), (A-D-F-G,8)} C {A, D, B, E, F}

7. {(A-D-F-G,8), (A-B-C-E,12), (A-B- G {A, D, B, E, F, C}


C-G, 14)}
*here E is already explored
Question:
Find the cheapest path from A to G
Frontier List Expand List Explored List
1. {(A,0)} A NULL

2. {(A-D, 3), (A-B, 5)} D {A}

3. {(A-B, 5), (A-D-E, 5), (A-D-F, 5)} B {A, D}

4. {(A-D-E, 5), (A-D-F, 5), (A-B-C, 6)} E {A, D, B}

5. {(A-D-F, 5), (A-B-C, 6), (A-D-E-B, F {A, D, B, E}


9)}
*here B is already explored

6. {(A-B-C, 6), (A-D-F-G,8)} C {A, D, B, E, F}

7. {(A-D-F-G,8), (A-B-C-E,12), (A-B- G {A, D, B, E, F, C}


C-G, 14)}
*here E is already explored

8. {(A-D-F-G,8)} NULL {A, D, B, E, F, C, G}


# GOAL Found!
BFS vs DSF vs UCS
Factors BFS DFS UCS
BFS is complete if the UCS is complete if the
DFS is complete if the search
Completeness branching factor b is branching factor b is
tree is finite
finite. finite.
The time complexity of Equivalent to the number of The time complexity of
Time Complexity UCS is exponential O(bd) nodes traversed in DFS i.e. UCS is exponential
Where d is the depth O(bd) O(b(1+C/ε))
The space complexity is
Equivalent to number of Equivalent to how large can
Space Complexity also exponential
vertices O(bd) the fringe get. O(b * d)
O(b(1+C/ε))

BFS gives the optimal


DFS is not optimal, meaning
solution or path to the
the number of steps in UCS gives the optimal
goal node only if weights
Optimality reaching the solution, or the solution or path to the
of each path is equal
cost spent in reaching it is goal node
otherwise it is not
high.
optimal.

You might also like