BFS, DFS, Best First Search

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

BFS,DFS,Best First Search

AI~(PEC-IT501B) Presentation
Name: Srinanda Das
Class Roll: CSE2020/062
University Roll: 11700120092
CSE B 3rd Year
BFS-Breadth First Search
★ Breadth-first search is a graph traversal algorithm.
★ It starts traversing the graph from the root node and explores all the neighboring
nodes.
★ Then, it selects the nearest node and explores all the unexplored nodes.
★ While using BFS for traversal, any node in the graph can be considered as the root node.
★ Queue data structure is used in the implementation of breadth first search.
★ Breadth first search (BFS), as the name implies, searches from the initial state
breadth-wise.
★ That is it searches all the states in the tree level by level.
★ Only after exploring all the states in one level it will jump to the next level.
★ Once the solution is found the search stops.
★ The breadth first search is guaranteed to find the solution if one exists.
BFS Algorithm
Input: Graph G = (V, E), either Idea: Send a wave out
directed or undirected, and from s.
source vertex s ∈ V. • First hits all vertices 1
edge from s.
Output: d[v] = distance (smallest • From there, hits all
# of edges) from s to v, for all v ∈ vertices 2 edges from s.
V. In book, also π[v] = u such that • Etc.
(u, v) is last edge on shortest path Use FIFO queue Q to
s → v. maintain wavefront.
● u is v’s predecessor.
• v ∈ Q if and only if
● set of edges {(π[v], v) : v != wave has hit v but has
s} forms a tree. not come out of v yet.
● Compute only d[v], not π
[v].
● Omitting colors of
vertices.
Example & Application
Applications of BFS algorithm
The applications of breadth-first-algorithm are given as follows -
● BFS can be used to find the neighboring locations
from a given source location.
● In a peer-to-peer network, BFS algorithm can be used
as a traversal method to find all the neighboring
nodes. Most torrent clients, such as BitTorrent,
uTorrent, etc. employ this process to find "seeds" and
"peers" in the network.
● BFS can be used in web crawlers to create web page
indexes. It is one of the main algorithms that can be
used to index web pages. It starts traversing from the
source page and follows the links associated with the
page. Here, every web page is considered as a node in
the graph.
● BFS is used to determine the shortest path and
minimum spanning tree.
Time & Space Complexity of BFS
Time complexity of BFS Space complexity of BFS

● In computer science, the Time complexity is the ● Space complexity is a measure of the amount of
computational complexity that describes the working storage an algorithm needs. That means how
amount of time it takes to run an algorithm. much memory, in the worst case, is needed at any
point in the algorithm.
● Since in the worst case breadth-first search has
● Since all of the nodes of a level must be saved until
to consider all paths to all possible nodes the
their child nodes in the next level have been
time complexity of breadth-first search is O(|E| generated, the space complexity is proportional to the
+ |V|) where |V| and |E| is the cardinality of set number of nodes at the deepest level.
of vertices and edges respectively. ● The space complexity can also be expressed as O(|V|)
● The complexity is this since every vertex and where |V| is the cardinality of the set of vertices.
every edge will be explored in the worst case. ● In the worst case scenario, the graph has a depth of 1
and all vertices must be stored.
DFS-Depth First Search
★ Depth first Search or Depth first traversal is a recursive algorithm for searching all
the vertices of a graph or tree data structure.
★ Traversal means visiting all the nodes of a graph.
★ Explore only one branch deeper till the solution is found or there is no new state to
explore and then start searching the adjacent level.
★ This technique is called depth first search (DFS).
★ By chance the solution exists in the first branch then depth first search can find the
solution quickly.
★ If the solution exists in some other branches farther away, there won't be much
difference between depth first search and breadth first search.
DFS Algorithm
A standard DFS implementation puts each vertex of the graph into one of two categories:

★ Visited
★ Not Visited

The purpose of the algorithm is to mark each vertex as visited while avoiding cycles.

The DFS algorithm works as follows:

❖ Start by putting any one of the graph's vertices on top of a stack.


❖ Take the top item of the stack and add it to the visited list.
❖ Create a list of that vertex's adjacent nodes. Add the ones which aren't in the visited list to the top of the
stack.
❖ Keep repeating steps 2 and 3 until the stack is empty.
DFS Algorithm
Example & Application
Time & Space Complexity of DFS
Time Complexity of DFS Space Complexity of DFS

● In computer science, the Time complexity is ● Space complexity is a measure of the amount of
the computational complexity that describes working storage an algorithm needs. That means
the amount of time it takes to run an how much memory, in the worst case, is needed at
algorithm. any point in the algorithm.
● Since in the worst case depth-first search has to ● Let the length of longest path be m. For each node,
you have to store its siblings so that when you have
consider all paths to all possible nodes, the time
visited all its children, and you come back to a
complexity of depth-first search is O(|E| + |V|)
parent node, you know which sibling to explore
where |V| and |E| is the cardinality of set of
next. For m nodes down the path, you will have to
vertices and edges respectively. store b nodes extra for each of the m nodes. So the
● The complexity is this since every vertex and space complexity here is O(bm).
every edge will be explored in the worst case.
Best First Search
★ If we consider searching as a form of traversal in a graph, an uninformed search algorithm would
blindly traverse to the next node in a given manner without considering the cost associated with that
step.
★ An informed search, like BFS, on the other hand, would use an evaluation function to decide which
among the various available nodes is the most promising (or ‘BEST’) before traversing to that node.
★ BFS uses the concept of a Priority queue and heuristic search.
★ To search the graph space, the BFS method uses two lists for tracking the traversal.
★ An ‘Open’ list that keeps track of the current ‘immediate’ nodes available for traversal and a ‘CLOSED’
list that keeps track of the nodes already traversed.
Best First Search Algorithm
● Create 2 empty lists: OPEN and CLOSED // Pseudocode for Best First Search
● Start from the initial node (say N) and put it in the ‘ordered’
Best-First-Search(Graph g, Node start)
OPEN list
● Repeat the next steps until the GOAL node is reached
1) Create an empty PriorityQueue
PriorityQueue pq;
2) Insert "start" in pq.
❖ If the OPEN list is empty, then EXIT the loop
returning ‘False’
pq.insert(start)
❖ Select the first/top node (say N) in the OPEN list and 3) Until PriorityQueue is empty
move it to the CLOSED list. Also, capture the u = PriorityQueue.DeleteMin
information of the parent node If u is the goal
❖ If N is a GOAL node, then move the node to the Exit
Closed list and exit the loop returning ‘True’. The
Else
solution can be found by backtracking the path
❖ If N is not the GOAL node, expand node N to
Foreach neighbor v of u
generate the ‘immediate’ next nodes linked to node N If v "Unvisited"
and add all those to the OPEN list Mark v "Visited"
❖ Reorder the nodes in the OPEN list in ascending pq.insert(v)
order according to an evaluation function f(n) Mark u "Examined"
End procedure
Variants of Best First Search
Greedy-best First Search A* Search Algorithm
Time Complexity: The worst case time complexity of Time Complexity: The time complexity of A* search algorithm
depends on heuristic function, and the number of nodes
Greedy best first search is O(bm).
expanded is exponential to the depth of solution d. So the time
complexity is O(b^d), where b is the branching factor.
Space Complexity: The worst case space complexity of
Space Complexity: The space complexity of A* search algorithm
Greedy best first search is O(bm). Where, m is the maximum is O(b^d)
depth of the search space. Complete: A* algorithm is complete as long as:
● Branching factor is finite.
Complete: Greedy best-first search is also incomplete, even
● Cost at every action is fixed.
if the given state space is finite.
Optimal: A* search algorithm is optimal if it follows below two
conditions:
Optimal: Greedy best first search algorithm is not optimal.
● Admissible: the first condition requires for optimality is
that h(n) should be an admissible heuristic for A* tree
search. An admissible heuristic is optimistic in nature.
● Consistency: Second required condition is consistency for
only A* graph-search.
Conclusion

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy