W05 - Introduction To Search in AI
W05 - Introduction To Search in AI
SEARCH IN AI
By: SEK SOCHEAT
Lecturer Artificial Intelligence
2023 – 2024
Mobile: 017 879 967 MSIT – AEU
Email: [email protected]
TABLE OF CONTENTS
Visualization and Data Tools
1. Introduction
2. Uninformed Search
3. Informed Search
4. Heuristic Functions
5. Applications
2
1. INTRODUCTION
1. INTRODUCTION
Problem-Solving Techniques:
4
evolutionary computations, knowledge representations, etc.
1. INTRODUCTION
• Implementation
5
1. INTRODUCTION
• Implementation
6
2. UNINFORMED SEARCH
2. UNINFORMED SEARCH ALGORITHMS
Search strategies that use no domain-specific knowledge beyond the problem definition.
BFS > The path of traversal is: DFS > The path of traversal is:
A —-> B —-> C —-> D —-> E —-> F A —-> B —-> D —-> E —-> C —-> F
8
2. UNINFORMED SEARCH ALGORITHMS
Breadth-First Search
It is of the most common search strategies. It generally starts from the root node and examines the
neighbor nodes and then moves to the next level. It uses First-in First-out (FIFO) strategy as it gives
the shortest path to achieving the solution.
BFS is used where the given problem is very small and space complexity is not considered.
2. BFS Function:
3. Example Usage: The BFS function is called with 'A' as the start
node and 'F' as the goal node. The result indicates whether a path exists.
10
2. UNINFORMED SEARCH ALGORITHMS
Depth-First Search
The depth-first search uses Last-in, First-out (LIFO) strategy and hence it can be implemented by using
stack. DFS uses backtracking. That is, it starts from the initial state and explores each path to its greatest
depth before it moves to the next path.
11
The path of traversal is: A —-> B —-> D —-> E —-> C —-> F
2. UNINFORMED SEARCH ALGORITHMS
Depth-First Search
The path of traversal is:
Explanation: A —-> B —-> D —-> E —-> C —-> F
1. Graph Representation: The graph is represented as an adjacency list
using a dictionary. Each node is a key, and its value is a list of adjacent nodes.
2. DFS Function:
• Check Goal: If the start node is the goal node, the function returns True.
• Recursive Call: The function recurses for all unvisited adjacent nodes. If
any recursive call returns True, the function returns True.
• Return False: If no path is found after exploring all adjacent nodes, the
function returns False.
3. Example Usage: The DFS function is called with 'A' as the start node and
'F' as the goal node. The result indicates whether a path exists.
12
2. INFORMED SEARCH ALGORITHMS
3. INFORMED SEARCH ALGORITHMS
Greedy best-first search uses the properties of both depth-first search and breadth-first search.
Greedy best-first search traverses the node by selecting the path which appears best at the
moment. The closest path is selected by using the heuristic function.
Explanation:
1. Graph Representation: The graph is represented as an adjacency list using a dictionary. Each node is a key, and its value is a list of
adjacent nodes.
2. Heuristics: The heuristic values are defined for each node, indicating the estimated cost to reach the goal.
3. GBFS Function:
• Initialization: A priority queue (min-heap) is initialized with the start node and its heuristic value.
• Loop: Nodes are dequeued in order of their heuristic values. If the current node is the goal, the function returns True. Otherwise, it
marks the node as visited and enqueues its unvisited neighbors with their heuristic values.
• Return False: If the loop completes without finding the goal, the function returns False.
4. Example Usage: The GBFS function is called with 'A' as the start node and 'H' as the goal node. The result indicates whether a path
exists.
17
3. INFORMED SEARCH ALGORITHMS
A* Search
A* search algorithm is a combination of both uninform cost search and greedy best-first search
algorithms. It uses the advantages of both with better memory usage. It uses a heuristic function to
find the shortest path. A* search algorithm uses the sum of both the cost and heuristic of the node
to find the best path.
A* Search
A* Search
Explanation:
1. Graph Representation: The graph is represented as an adjacency list with edge costs.
2. Heuristics: A dictionary is used to store heuristic values for each node. The heuristic values should be designed to estimate the cost
from any node to the goal node.
3. GBFS Function:
• Priority Queue: Uses a priority queue to select the node with the lowest estimated total cost (g(n) + h(n)).
• Cost and Path Tracking: Tracks the cost from the start to each node (g_costs) and the best path to reach each node (came_from).
• Reconstruct Path: When the goal node is reached, reconstructs the path by tracing back from the goal to the start.
4. Example Usage: The A* search function is called with 'A' as the start node and 'H' as the goal node. The result shows the path from
the start to the goal.
20
3. HEURISTIC FUNCTIONS
3. HEURISTIC FUNCTIONS
A heuristic function, often denoted as h(n), is used in informed search algorithms to estimate the
cost to reach the goal from a given node 𝑛𝑛. The quality of the heuristic greatly affects the efficiency
of the search algorithm.
Properties of a Good Heuristic: Example Heuristics:
A* Search
Output:
23
Path from A to T: ['A', 'F', 'K', 'P', 'Q', 'R', 'S', 'T']
3. HEURISTIC FUNCTIONS
A* Search
Explanation:
3. A* Search Function:
• Tracks the actual cost to reach each node (g_costs) and the
best path taken (came_from).
• Reconstructs the path from the goal back to the start once the
goal is reached.
Output:
24
Path from A to T: ['A', 'F', 'K', 'P', 'Q', 'R', 'S', 'T']
4. APPLICATIONS
4. APPLICATIONS