09/26/2024 Department of CSE (AI/ML) 1
22PCOAM11
INTROUCTION TO ARTIFICAL
INTELLIGENCE (R22 – II (Sem 3)
Department of computer science and
engineering (AIML)
Session 4
by
Asst.Prof.M.Gokilavani
GNITC
09/26/2024 Department of CSE (AI & ML)
TEXTBOOK:
• Artificial Intelligence A modern Approach, Third
Edition, Stuart Russell and Peter Norvig, Pearson
Education.
REFERENCES:
• Artificial Intelligence, 3rd
Edn, E. Rich and K.Knight
(TMH).
• Artificial Intelligence, 3rd
Edn, Patrick Henny Winston,
Pearson Education.
• Artificial Intelligence, Shivani Goel, Pearson Education.
• Artificial Intelligence and Expert Systems- Patterson,
Pearson Education.
2
Topics covered in Unit 1
09/26/2024 Department of CSE (AI & ML) 3
• Introduction to AI : Intelligent Agent
• Problem-Solving Agents
• Searching for Solutions : Breadth-first search
• Depth-first search
• Hill-climbing search
• Simulated annealing search
• Local Search in Continuous Spaces.
09/26/2024 Department of CSE (AI/ML)
Properties of Search Algorithms
• Following are the four essential properties of search algorithms to compare the efficiency of
these algorithms:
• Completeness: A search algorithm is said to be complete if it guarantees to return a solution if
at least any solution exists for any random input.
• Optimality: If a solution found for an algorithm is guaranteed to be the best solution (lowest
path cost) among all other solutions, then such a solution for is said to be an optimal solution.
• Time Complexity: Time complexity is a measure of time for an algorithm to complete its task.
• Space Complexity: It is the maximum storage space required at any point during the search, as
the complexity of the problem.
4
09/26/2024 Department of CSE (AI/ML)
Types of search algorithms
Based on the search problems we can classify
the search algorithms into
– uninformed (Blind search) search and
– informed search (Heuristic search) algorithms.
5
09/26/2024 Department of CSE (AI/ML) 6
09/26/2024 Department of CSE (AI/ML)
Uninformed/Blind Search
•Uninformed search applies a way in which search tree is searched
without any information about the search space like initial state
operators and test for the goal, so it is also called blind search.
•Don’t have any domain knowledge .
•It examines each node of the tree until it achieves the goal node.
It can be divided into five main types:
– Breadth-first search
– Uniform cost search
– Depth-first search
– Iterative deepening depth-first search
– Bidirectional Search
7
09/26/2024 Department of CSE (AI/ML)
Informed Search
• Informed search algorithms use domain knowledge. In an
informed search, problem information is available which can
guide the search.
• Informed search strategies can find a solution more efficiently
than an uninformed search strategy. Informed search is also
called a Heuristic search.
• A heuristic is a way which might not always be guaranteed for
best solutions but guaranteed to find a good solution in
reasonable time.
• An example of informed search algorithms is a traveling
salesman problem.
• Greedy Search
• A* Search
8
09/26/2024 Department of CSE (AI/ML) 9
Breadth-first Search
• Breadth-first search is the most common search strategy
for traversing a tree or graph. This algorithm searches
breadthwise in a tree or graph, so it is called breadth-
first search.
• BFS algorithm starts searching from the root node of the
tree and expands all successor node at the current level
before moving to nodes of next level.
• The breadth-first search algorithm is an example of a
general-graph search algorithm.
• Breadth-first search implemented using FIFO queue
data structure.
09/26/2024 Department of CSE (AI/ML)
Algorithm
• Step 1: SET STATUS = 1 (ready state) for each node in G
• Step 2: Enqueue the starting node A and set its STATUS =
2 (waiting state)
• Step 3: Repeat Steps 4 and 5 until QUEUE is empty
• Step 4: Dequeue a node N. Process it and set its STATUS
= 3 (processed state).
• Step 5: Enqueue all the neighbours of N that are in the
ready state (whose STATUS = 1) and set
their STATUS = 2
(waiting state)
[END OF LOOP]
• Step 6: EXIT
10
09/26/2024 Department of CSE (AI/ML)
Example 1
11
09/26/2024 Department of CSE (AI/ML)
• In the example given below, there is a directed
graph having 7 vertices.
• In the above graph, minimum path 'P' can be
found by using the BFS that will start from Node
A and end at Node E.
• The algorithm uses two queues, namely
QUEUE1 and QUEUE2.
• QUEUE1 holds all the nodes that are to be
processed, while QUEUE2 holds all the nodes
that are processed and deleted fromQUEUE1.
12
09/26/2024 Department of CSE (AI/ML) 13
09/26/2024 Department of CSE (AI/ML)
Example 2
14
09/26/2024 Department of CSE (AI/ML)
Practice Problem
15
09/26/2024 Department of CSE (AI/ML)
Applications of BFS algorithm
• BFS can be used to find the neighboring
locations from a given source location.
• BFS can be used in web crawlers to create web
page indexes.
• BFS is used to determine the shortest path and
minimum spanning tree.
16
09/26/2024 Department of CSE (AI/ML)
DFS (Depth First Search) algorithm
• It is a recursive algorithm to search all the vertices of
a tree data structure or a graph.
• The depth-first search (DFS) algorithm starts with the
initial node of graph G and goes deeper until we find
the goal node or the node with no children.
• Because of the recursive nature, stack data structure
can be used to implement the DFS algorithm.
• The process of implementing the DFS is similar to
the BFS algorithm.
17
09/26/2024 Department of CSE (AI/ML)
The step by step process to implement the DFS traversal is
given as follows -
• First, create a stack with the total number of vertices in the
graph.
• Now, choose any vertex as the starting point of traversal,
and push that vertex into the stack.
• After that, push a non-visited vertex (adjacent to the vertex
on the top of the stack) to the top of the stack.
• Now, repeat steps 3 and 4 until no vertices are left to visit
from the vertex on the stack's top.
• If no vertex is left, go back and pop a vertex from the stack.
• Repeat steps 2, 3, and 4 until the stack is empty.
18
09/26/2024 Department of CSE (AI/ML)
Algorithm
• Step 1: SET STATUS = 1 (ready state) for each node in G
• Step 2: Push the starting node A on the stack and set its STATUS =
2 (waiting state)
• Step 3: Repeat Steps 4 and 5 until STACK is empty
• Step 4: Pop the top node N. Process it and set its STATUS = 3
(processed state)
• Step 5: Push on the stack all the neighbors of N that are in the ready
state (whose STATUS = 1) and set their STATUS = 2 (waiting state)
• [END OF LOOP]
• Step 6: EXIT
19
09/26/2024 Department of CSE (AI/ML) 20
09/26/2024 Department of CSE (AI/ML)
Example
21
09/26/2024 Department of CSE (AI/ML)
Applications of DFS
• DFS algorithm can be used to implement the
topological sorting.
• It can be used to find the paths between two
vertices.
• It can also be used to detect cycles in the graph.
• DFS algorithm is also used for one solution
puzzles.
• DFS is used to determine if a graph is bipartite or
not.
22
09/26/2024 Department of CSE (AI/ML) 23
09/26/2024 Department of CSE (AI/ML) 24
09/26/2024 Department of CSE (AI/ML)
Topics to be covered in next session 5
• Hill climbing Search
25
Thank you!!!

22PCOAM11 Unit 1: Session 4 BFS & DFS Alg

  • 1.
    09/26/2024 Department ofCSE (AI/ML) 1 22PCOAM11 INTROUCTION TO ARTIFICAL INTELLIGENCE (R22 – II (Sem 3) Department of computer science and engineering (AIML) Session 4 by Asst.Prof.M.Gokilavani GNITC
  • 2.
    09/26/2024 Department ofCSE (AI & ML) TEXTBOOK: • Artificial Intelligence A modern Approach, Third Edition, Stuart Russell and Peter Norvig, Pearson Education. REFERENCES: • Artificial Intelligence, 3rd Edn, E. Rich and K.Knight (TMH). • Artificial Intelligence, 3rd Edn, Patrick Henny Winston, Pearson Education. • Artificial Intelligence, Shivani Goel, Pearson Education. • Artificial Intelligence and Expert Systems- Patterson, Pearson Education. 2
  • 3.
    Topics covered inUnit 1 09/26/2024 Department of CSE (AI & ML) 3 • Introduction to AI : Intelligent Agent • Problem-Solving Agents • Searching for Solutions : Breadth-first search • Depth-first search • Hill-climbing search • Simulated annealing search • Local Search in Continuous Spaces.
  • 4.
    09/26/2024 Department ofCSE (AI/ML) Properties of Search Algorithms • Following are the four essential properties of search algorithms to compare the efficiency of these algorithms: • Completeness: A search algorithm is said to be complete if it guarantees to return a solution if at least any solution exists for any random input. • Optimality: If a solution found for an algorithm is guaranteed to be the best solution (lowest path cost) among all other solutions, then such a solution for is said to be an optimal solution. • Time Complexity: Time complexity is a measure of time for an algorithm to complete its task. • Space Complexity: It is the maximum storage space required at any point during the search, as the complexity of the problem. 4
  • 5.
    09/26/2024 Department ofCSE (AI/ML) Types of search algorithms Based on the search problems we can classify the search algorithms into – uninformed (Blind search) search and – informed search (Heuristic search) algorithms. 5
  • 6.
  • 7.
    09/26/2024 Department ofCSE (AI/ML) Uninformed/Blind Search •Uninformed search applies a way in which search tree is searched without any information about the search space like initial state operators and test for the goal, so it is also called blind search. •Don’t have any domain knowledge . •It examines each node of the tree until it achieves the goal node. It can be divided into five main types: – Breadth-first search – Uniform cost search – Depth-first search – Iterative deepening depth-first search – Bidirectional Search 7
  • 8.
    09/26/2024 Department ofCSE (AI/ML) Informed Search • Informed search algorithms use domain knowledge. In an informed search, problem information is available which can guide the search. • Informed search strategies can find a solution more efficiently than an uninformed search strategy. Informed search is also called a Heuristic search. • A heuristic is a way which might not always be guaranteed for best solutions but guaranteed to find a good solution in reasonable time. • An example of informed search algorithms is a traveling salesman problem. • Greedy Search • A* Search 8
  • 9.
    09/26/2024 Department ofCSE (AI/ML) 9 Breadth-first Search • Breadth-first search is the most common search strategy for traversing a tree or graph. This algorithm searches breadthwise in a tree or graph, so it is called breadth- first search. • BFS algorithm starts searching from the root node of the tree and expands all successor node at the current level before moving to nodes of next level. • The breadth-first search algorithm is an example of a general-graph search algorithm. • Breadth-first search implemented using FIFO queue data structure.
  • 10.
    09/26/2024 Department ofCSE (AI/ML) Algorithm • Step 1: SET STATUS = 1 (ready state) for each node in G • Step 2: Enqueue the starting node A and set its STATUS = 2 (waiting state) • Step 3: Repeat Steps 4 and 5 until QUEUE is empty • Step 4: Dequeue a node N. Process it and set its STATUS = 3 (processed state). • Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) [END OF LOOP] • Step 6: EXIT 10
  • 11.
    09/26/2024 Department ofCSE (AI/ML) Example 1 11
  • 12.
    09/26/2024 Department ofCSE (AI/ML) • In the example given below, there is a directed graph having 7 vertices. • In the above graph, minimum path 'P' can be found by using the BFS that will start from Node A and end at Node E. • The algorithm uses two queues, namely QUEUE1 and QUEUE2. • QUEUE1 holds all the nodes that are to be processed, while QUEUE2 holds all the nodes that are processed and deleted fromQUEUE1. 12
  • 13.
  • 14.
    09/26/2024 Department ofCSE (AI/ML) Example 2 14
  • 15.
    09/26/2024 Department ofCSE (AI/ML) Practice Problem 15
  • 16.
    09/26/2024 Department ofCSE (AI/ML) Applications of BFS algorithm • BFS can be used to find the neighboring locations from a given source location. • BFS can be used in web crawlers to create web page indexes. • BFS is used to determine the shortest path and minimum spanning tree. 16
  • 17.
    09/26/2024 Department ofCSE (AI/ML) DFS (Depth First Search) algorithm • It is a recursive algorithm to search all the vertices of a tree data structure or a graph. • The depth-first search (DFS) algorithm starts with the initial node of graph G and goes deeper until we find the goal node or the node with no children. • Because of the recursive nature, stack data structure can be used to implement the DFS algorithm. • The process of implementing the DFS is similar to the BFS algorithm. 17
  • 18.
    09/26/2024 Department ofCSE (AI/ML) The step by step process to implement the DFS traversal is given as follows - • First, create a stack with the total number of vertices in the graph. • Now, choose any vertex as the starting point of traversal, and push that vertex into the stack. • After that, push a non-visited vertex (adjacent to the vertex on the top of the stack) to the top of the stack. • Now, repeat steps 3 and 4 until no vertices are left to visit from the vertex on the stack's top. • If no vertex is left, go back and pop a vertex from the stack. • Repeat steps 2, 3, and 4 until the stack is empty. 18
  • 19.
    09/26/2024 Department ofCSE (AI/ML) Algorithm • Step 1: SET STATUS = 1 (ready state) for each node in G • Step 2: Push the starting node A on the stack and set its STATUS = 2 (waiting state) • Step 3: Repeat Steps 4 and 5 until STACK is empty • Step 4: Pop the top node N. Process it and set its STATUS = 3 (processed state) • Step 5: Push on the stack all the neighbors of N that are in the ready state (whose STATUS = 1) and set their STATUS = 2 (waiting state) • [END OF LOOP] • Step 6: EXIT 19
  • 20.
  • 21.
    09/26/2024 Department ofCSE (AI/ML) Example 21
  • 22.
    09/26/2024 Department ofCSE (AI/ML) Applications of DFS • DFS algorithm can be used to implement the topological sorting. • It can be used to find the paths between two vertices. • It can also be used to detect cycles in the graph. • DFS algorithm is also used for one solution puzzles. • DFS is used to determine if a graph is bipartite or not. 22
  • 23.
  • 24.
  • 25.
    09/26/2024 Department ofCSE (AI/ML) Topics to be covered in next session 5 • Hill climbing Search 25 Thank you!!!