Ai 04

You might also like

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

Artificial Intelligence

04
Dr. Eng. Yasser KHADRA
Associate Professor

2 May 2023 © Dr. Eng. Yasser KHADRA 1


Solving problems by searching

2 May 2023 © Dr. Eng. Yasser KHADRA 2


Problem-solving agents

• On holiday in Damascus;
currently in Latakia.
• Formulate goal: be in
Damascus
• Formulate problem:
• states: various cities
• actions: drive between
cities
• Find solution:
• sequence of cities, e.g.,
Latakia, Tartus, Homes,
Damascus

2 May 2023 © Dr. Eng. Yasser KHADRA 3


Search Algorithm Terminologies
➢ A search problem can have three main factors:
❖ Search Space: Search space represents a set of possible solutions, which a
system may have.
❖ Start State: It is a state from where agent begins the search.
❖ Goal test: It is a function which observe the current state and returns
whether the goal state is achieved or not.
❖ Path Cost: It is a function which assigns a numeric cost to each path.

➢ Search: Searching is a step by step procedure to solve a search-problem in


a given search space.

2 May 2023 © Dr. Eng. Yasser KHADRA 4


Example: vacuum world state space graph

• states? integer dirt and robot location


• actions? Left, Right, Suck
• goal test? no dirt at all locations
• path cost? 1 per action

2 May 2023 © Dr. Eng. Yasser KHADRA 5


Example: The 8-puzzle

• states? locations of tiles

• actions? move blank left, right, up, down

• goal test? = goal state (given)

• path cost? 1 per move

2 May 2023 © Dr. Eng. Yasser KHADRA 6


Search Algorithm Terminologies

➢ Search tree: A tree representation of search problem.


➢ The root of the search tree: is the root node which is corresponding to the
initial state.
➢ Actions: It gives the description of all the available actions to the agent.

2 May 2023 © Dr. Eng. Yasser KHADRA 7


Search Algorithm Terminologies

➢ Search tree: A tree representation of search problem.

2 May 2023 © Dr. Eng. Yasser KHADRA 8


Search Algorithm Terminologies

➢ Transition model: A description of what each action do, can be represented as a


transition model.
➢ Solution: It is an action sequence which leads from the start node to the goal
node.
➢ Optimal Solution: If a solution has the lowest cost among all solutions.

2 May 2023 © Dr. Eng. Yasser KHADRA 9


Properties of Search 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.
➢ 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.

2 May 2023 © Dr. Eng. Yasser KHADRA 10


Types of search algorithms

2 May 2023 © Dr. Eng. Yasser KHADRA 11


Types of search algorithms
Parameters Informed Search Uninformed Search
Known as Also known as Heuristic Search. Also known as Blind Search.
knowledge for the searching Doesn’t use knowledge for the
Using Knowledge
process. searching process.
Finds solution slow as compared to an
Performance Finds a solution more quickly.
informed search.
Completion May or may not be complete. Always complete.
Cost Factor Cost is low. Cost is high.
Consumes less time because of Consumes moderate time because of
Time
quick searching. slow searching.
There is a direction given about No suggestion is given regarding the
Direction
the solution. solution in it.
Implementation Less lengthy while implemented. More lengthy while implemented.

2 May 2023 © Dr. Eng. Yasser KHADRA 12


Types of search algorithms

Parameters Informed Search Uninformed Search


It is more efficient as efficiency takes It is comparatively less efficient as
into account cost and performance. incurred cost is more and the speed
Efficiency
The incurred cost is less and speed of of finding the Breadth-First solution
finding solutions is quick. is slow.
Computational Computational requirements are Comparatively higher
requirements lessened. computational requirements.
Having a wide scope in terms of Solving a massive search task is
Performance
handling large search problems. challenging.

2 May 2023 © Dr. Eng. Yasser KHADRA 13


Uniformed/Blind Search Algorithms

2 May 2023 © Dr. Eng. Yasser KHADRA 14


Breadth-first Search
Breadth-First Search is an algorithm that is used to graph data or
searching tree or traversing structures.

• Traversal: visit each node once.


• Search: Find a path between two nodes.

2 May 2023 © Dr. Eng. Yasser KHADRA 15


Breadth-first Search (Traversal)

• The algorithm efficiently visits and marks all the key nodes in a
graph in an accurate breadthwise fashion.
pop

FIFO

• This algorithm selects a single node (initial


or source point) in a graph and then visits all
the nodes adjacent to the selected node. Output: A B C D E F G

2 May 2023 © Dr. Eng. Yasser KHADRA 16


Breadth-first Search
Consider the graph shown below. Traverse the graph in BFS order

current
a

a b c

b c d e

c d e f g
d e f g
e f g
f g
g

2 May 2023 © Dr. Eng. Yasser KHADRA 17


Rules of BFS Algorithm
The important rules for using BFS algorithm:
➢ A queue (FIFO-First in First Out) data structure is used by BFS.
➢ You mark any node in the graph as root and start traversing the data from
it.
➢ BFS traverses all the nodes in the graph and keeps dropping them as
completed.
➢ BFS visits an adjacent unvisited node, marks it as done, and inserts it into a
queue.
➢ Removes the previous vertex from the queue in case no adjacent vertex is
found.
➢ BFS algorithm iterates until all the vertices in the graph are successfully
traversed and marked as completed.
➢ There are no loops caused by BFS during the traversing of data from any
node.

2 May 2023 © Dr. Eng. Yasser KHADRA 18


Example BFS Algorithm
Consider the graph shown below. Traverse the graph in BFS order

2 May 2023 © Dr. Eng. Yasser KHADRA 19


Example BFS Algorithm

2 May 2023 © Dr. Eng. Yasser KHADRA 20


Example BFS Algorithm
Consider the graph shown below. Traverse the S
current
graph in BFS order
S A B

A B C D

B C D G H
C D G H E F
D G H E F
G H E F I
H E F I
E F I K
F I K
I K
S→A→B→C→ D→G →H→ E→ F→I→K K

2 May 2023 © Dr. Eng. Yasser KHADRA 21


Breadth-first Search (BFS)
alphabetical order

pop

FIFO Goal

Start

Skip because It is
Output: [S → D → G] already visited (D)

2 May 2023 © Dr. Eng. Yasser KHADRA 22


Breadth-first Search (BFS)
Breath First Search to solve Eight puzzle
problem

2 May 2023 © Dr. Eng. Yasser KHADRA 23


Properties of BFS algorithm
Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions for a given problem, then BFS will
provide the minimal solution which requires the least number of steps.
Disadvantages:
• It requires lots of memory since each level of the tree must be saved into
memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.

2 May 2023 © Dr. Eng. Yasser KHADRA 24


Depth-first Search

➢ Depth-first search isa recursive


algorithm for traversing a tree
or graph data structure.
➢ It is called the depth-first search
because it starts from the root
node and follows each path to
its greatest depth node before
moving to the next path.
➢ DFS uses a stack data structure
for its implementation.

➢ The process of the DFS algorithm is similar to the BFS algorithm.

2 May 2023 © Dr. Eng. Yasser KHADRA 25


Depth-first Search (Traversal)

➢ The process of the DFT algorithm is similar to the BFT algorithm.

LIFO

Output: A D G C B F E

2 May 2023 © Dr. Eng. Yasser KHADRA 26


The DFS algorithm

2 May 2023 © Dr. Eng. Yasser KHADRA 27


The DFS algorithm

2 May 2023 © Dr. Eng. Yasser KHADRA 28


Depth-first Search (DFS)
alphabetical order
Goal

Start
pop
LIFO

2 May 2023 © Dr. Eng. Yasser KHADRA 29


Depth-first Search
Example: In the below search tree, we have shown the flow of
depth-first search
current stack
[S]
[S] [S,A] [S,H]
[S,H] [S,A] [S,H,I] [S,H,J]
[S,H,J] [S,A] [S,H,I]
[S,H,I] [S,A] [S,H,I, k]
[S,H,I, k] [S,A]
[S,A] [S, A, B] [S, A, C]
[S, A, C] [S, A, B] [S, A, C, G]
[S, A, C, G]
[S → A → C → G]

2 May 2023 © Dr. Eng. Yasser KHADRA 30


Depth-first Search

➢ Advantage:
➢ DFS requires very less memory as it only needs to store a stack of the
nodes on the path from root node to the current node.
➢ It takes less time to reach to the goal node than BFS algorithm (if it
traverses in the right path).

➢ Disadvantage:
➢ There is the possibility that many states keep re-occurring, and there is
no guarantee of finding the solution.
➢ DFS algorithm goes for deep down searching and sometime it may go to
the infinite loop.

2 May 2023 © Dr. Eng. Yasser KHADRA 31

You might also like