This is a high-level template that can be adapted for various search algorithms: function searchproblem: initialize open set _ with the initial state initialize closed_set is an empty se
Trang 1HO CHI MINH CITY NATIONAL UNIVERSITY
UNIVERSITY OF SCIENCE FACULTY OF INFORMATION TECHNOLOGY
- -
SUBJECT: INTRODUCTION TO ARTIFICIAL INTELLIGENCE
SEMESTER I (2023-2024)
HO CHI MINH CITY, OCTOBER 2023
Trang 2Table of contents
I Introduction 1
1 Search problem 1
2 Elements of a search problem 1
3 General pseudo code 2
4 Uninformed Search and Informed Search 2
4.1 Uninformed Search 2
4.2 Informed Search 2
4.3 Distinguishing Uninformed Search and Informed Search 2
II Search algorithms 3
1 Depth First Search 3
1.1 General idea 3
1.2 Pseudo code 4
1.3 Analysis algorithm 4
1.4 Illustrate the pseudo code 4
2 Breadth First Search 5
2.1 General idea 5
2.2 Pseudo code 5
2.3 Analysis algorithm 6
2.4 Illustrate the pseudo code 6
3 Uniform Cost Search 7
3.1 General idea 7
3.2 Pseudo code 7
3.3 Analysis algorithm 8
3.4 Illustrate the pseudo code 8
4 Greedy Best-first Search 9
Trang 3LAB 1 SEARCH IN GRAPH –
5.4 Illustrate the pseudo code 14
III Comparison 14
1 UCS, Greedy, A* 15
2 UCS, Dijstra 16
IV Implementation 18
1 Depth First Search 18
2 Breadth First Search 19
3 Uniform Cost Search 20
4 Greedy Best-first Search 21
5 A* Search 23
V References 24
Trang 4I Introduction 1 Search problem A search problem is a fundamental concept in artificial intelligence and computer science It involves finding a solution or a path from a starting state to a goal state within a problem space Search problems are commonly used in areas like pathfinding, game playing, planning, and more
2 Elements of a search problem A search problem can be defined formally as follows:
• A set of possible states that the environment can be in This is the state space • The initial state (start state) that the agent starts in
• A set of one or more goal states • The actions available to the agent Given a state s, Actions sreturns a finite set of ( )
actions that can be executed in s Each of these actions is applicable in s • A transition model, which describes what each action does Result s areturns the (, )
state that results from doing action a in state s • An action cost function, denote byAction - Cost(s,a,s when programming or ( , , ')c s a
when doing math, that gives the numeric cost of applying action a in state sto reach state '
s A sequence of actions forms a path, and a solution is a path from the initial state to a goal state We assume that action costs are additive; that is, the total cost of a path is the sum of the individual action costs An optimal solution has the lowest path cost among all solutions The state space can be represented as a graph in which the vertices are states and the directed edges between them are actions
Trang 5LAB 1 SEARCH IN GRAPH – INTRODUCTION TO ARTIFICIAL INTELLIGENCE
3 General pseudo code Here is a general pseudo code outline to solve a search problem without specifying a particular algorithm This is a high-level template that can be adapted for various search algorithms:
function search(problem): initialize open set _ with the initial state initialize closed_set is an empty set
while open_set is not empty: # Select a state based on the chosen strategy current_state = select_state( open_set )
if current_state is the goal state: # Function to trace back and generate the path
return reconstruct_path( current_state ) add current_state to closed_set
for each action in actions: # Generate the next state from the current state next_state = apply_action( current_state , action)
if next_state is not in closed_set : # Case next_state in open_set, update open_set if needs add next_state to open_set
# To keep track of the path set the parent of next_state = current_state return "No solution found"
4 Uninformed Search and Informed Search 4.1 Uninformed Search
Uninformed Search have no additional information on the goal node other than the one provided in the problem definition The plans to reach the goal state from the start state differ only by the order and/or length of actions Uninformed search is also called Blind search These algorithms can only generate the successors and differentiate between the goal state and non goal state 4.2 Informed Search
Informed search has information on the goal state, which uses a heuristic function (to estimate how close a given state is to the goal state) to solve a problem Informed search uses a generate and test approach; it is more efficient than uninformed search An informed search heuristic function is used as a model that will lead us to the goal state without traversing the search tree blindly It will consider the next node to traverse based on some evaluation function
(Heuristic Function) to reach the goal state from the current state 4.3 Distinguishing Uninformed Search and Informed Search
Trang 6Uninformed Search Informed Search Uninformed search algorithms do not have
information about the problem other than thproblem definition (i.e., the state space, initial state, goal state, and actions)
Informed search algorithms use additional information or heuristics to guide the searc
They explore the search space without usinheuristics or domain-specific knowledge
They make use of domain-specific knowledge to estimate which states are molikely to lead to a solution
Examples include depth-first search, breadfirst search, and uniform-cost search
Examples include A* search, and greedy best-first search
Uninformed search algorithms are generallcomplete (they will find a solution if one exists), but they may be less efficient in terms of the number of nodes expanded
Informed search algorithms can be more efficient in finding solutions because they prioritize nodes that are more promising according to the heuristic function However, the completeness of informed search algorithms may depend on the qualof the heuristic; some heuristics may not guarantee completeness
In summary, the distinction between uninformed and informed search lies in the use of additional information or heuristics to guide the search process Informed search methods can often find solutions more efficiently, but they may require a well-designed heuristic, while uninformed search methods are more general but may explore the search space exhaustively II Search algorithms
1 Depth First Search 1.1 General idea Depth First Search (DFS) is an uninformed search algorithm for traversing or searching tree or graph data structures by visiting a branch as deeply as possible before backtracking The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a
Trang 7LAB 1 SEARCH IN GRAPH – INTRODUCTION TO ARTIFICIAL INTELLIGENCE
graph) and explores as far as possible along each branch before backtracking DFS uses a last- -infirst-out strategy (LIFO) and hence it is implemented by using a stack or recursion to maintain the nodes to be explored
1.2 Pseudo code Here is the pseudo code for Depth First Search:
function DFS(graph, start, goal): open_set = create_empty_stack() open_set push(start)
closed_set = create_empty_set() while not open_set is_empty(): current_node = open_set pop() if current_node == goal: return reconstruct_path( current_node )
add current_node to closed_set
for neighbor in graph.get_neighbors( current_node ): if neighbor not in closed_set :
if neighbor in open_set :
open_set remove( neighbor ) open_set push( neighbor ) set the parent of neighbor = current_node
return "Goal not found"
the cost spent in reaching the solution might not be the shortest path • Time complexity: In the worst case, DFS can have exponential time complexity in terms
of the number of nodes and edges in the graph Equivalent to the number of nodes traversed in DFS T n( ) 1= +n2 +n3 + +nd =O n( )d
• Space complexity: Equivalent to how large can the fringe get S n( ) =O n d( ) 1.4 Illustrate the pseudo code
Trang 8Graph for DFS Open set, closed set and path for DFS
2.2 Pseudo code Here is the pseudo code for Breadth First Search:
Trang 9LAB 1 SEARCH IN GRAPH – INTRODUCTION TO ARTIFICIAL INTELLIGENCE
function BFS(graph, start, goal): open_set = create_empty_queue() open_set enqueue(start) closed_set = create_empty_set() while not open_set is_empty(): current_node = open_set dequeue() if current_node == goal: return reconstruct_path( current_node )
add current_node to closed_set
for neighbor in graph.get_neighbors( current_node ): if neighbor not in closed_set and neighbor not in open_set : open_set enqueue( neighbor )
set the parent of neighbor = current_node
return "Goal not found"
2.3 Analysis algorithm s= the depth of the shallowest solution
in = number of nodes in level i
• Completeness: BFS is guaranteed to be complete, meaning for a given search tree, BFS will come up with a solution if it exists because it explores all possible paths
systematically • Optimality: BFS is also optimal if the cost between nodes is uniform It will find the
shortest path to the goal, as it explores nodes level by level, and it encounters the goal at the shallowest possible level
• Time complexity: Equivalent to the number of nodes traversed in BFS until the shallowest solution T n( ) 1= +n2 +n3 + +ns =O n(s In the worst case, BFS has a time complexity that is exponential in the number of nodes and edges, but in practice, it is often more efficient than DFS for finding the shortest path
• Space complexity: Equivalent to how large can the fringe get S n( )=O n( )s
2.4 Illustrate the pseudo code
Trang 10Graph for BFSOpen set, closed set and path for BFS
5, visit G (goal node) B S, A, C, D, G S→C→G
3 Uniform Cost Search 3.1 General idea Uniform Cost Search (UCS) is an uninformed search algorithm that explores a graph by considering the cost associated with each path It starts at the initial node and explores nodes with the lowest path cost first UCS aims to find the lowest-cost path to the goal It is implemented using the priority queue To calculate the cost of every node, consider this equation:c m( ) c n( )= (+c n, ) where c m is the cost of the current node, ( ) c n is the cost of ( )the previous node, and C n mis the weight of the edge The successor can be removed if it is ( , )already in a queue with a higher cost
3.2 Pseudo code
Trang 11LAB 1 SEARCH IN GRAPH – INTRODUCTION TO ARTIFICIAL INTELLIGENCE
Here is the pseudo code for Uniform Cost Search:
function UCS(graph, start, goal): open_set = create_empty_priority_queue() open_set enqueue(start, 0) # Start node with a cost of 0 closed_set = create_empty_set()
while not open_set is_empty(): current_node , current_cost = open_set dequeue() if current_node == goal:
return reconstruct_path( current_node ) add current_node to closed_set
for neighbor in graph.get_neighbors( current_node ): if neighbor not in closed_set :
neighbor_cost = current_cost + cost( current_node , neighbor ) if neighbor not in open_set :
open_set enqueue( neighbor , neighbor_cost ) set the parent of neighbor = current_node
else if neighbor in open_set and neighbor_cost < previous_cost: update neighbor_cost and parent of neighbor in open_set
return "Goal not found"
3.3 Analysis algorithm • Completeness: UCS is guaranteed to be complete as long as states are finite and the cost
of every action is greater than a small positive value It will find a solution if one exists because it explores paths systematically based on cost
• Optimality: UCS is optimal, meaning it will find the lowest-cost path to the goal, given that the cost function is non-decreasing and the action costs are non-negative • The complexity of uniform-cost search is characterized in terms of C , the cost of the *
optimal solution, and e, a lower bound on the cost of each action, with 0e Then the algorithm’s worst-case time and space complexity is O b(1+ C e*/ ), which can be much greater than b When all action costs are equal, d b1 + C e */ is just b+ 1, and uniform-cost search is similar to breadth-first search
3.4 Illustrate the pseudo code
Trang 12Graph for UCS Open set, closed set and path for UCS
4 Greedy Best-first Search 4.1 General idea
Greedy Best-first Search (GBS) is an informed search algorithm that selects the node to expand based on a heuristic functionf n( ) =h n( ), which estimates how close a node is to the goal It prioritizes nodes that seem most promising according to the heuristic, aiming to reach the goal quickly However, it doesn't consider the cost of the path taken so far, which can lead to suboptimal solutions
Trang 13LAB 1 SEARCH IN GRAPH – INTRODUCTION TO ARTIFICIAL INTELLIGENCE
4.2 Pseudo code Here is the pseudo code for Greedy Best-first Search:
function GBS(graph, start, goal): open_set = create_empty_priority_queue() open_set enqueue(start, heuristic(start, goal)) closed_set = create_empty_set()
while not open_set is_empty(): current_node , current_cost = open_set dequeue() if current_node == goal:
return reconstruct_path( current_node ) add current_node to closed_set
for neighbor in graph.get_neighbors( current_node ): if neighbor not in closed_set :
neighbor_cost = heuristic( neighbor , goal) if neighbor not in open_set :
open_set.enqueue( neighbor , neighbor_cost ) set the parent of neighbor = current_node
else if neighbor in open_set and neighbor_cost < previous_cost: update neighbor_cost and parent of neighbor in open_set
return "Goal not found"
4.3 Analysis algorithm • Completeness: Greedy Best-first Search is complete if the graph is finite and all action
costs are non-negative It will eventually find a solution if one exists • Optimality: Greedy Best-first Search is not guaranteed to find the optimal solution
because it doesn't consider the cost of the path taken so far It prioritizes nodes solely based on the heuristic estimate
• The complexity of Greedy Best-first Search depends on the quality of the heuristic If the heuristic is well-designed and accurately predicts the distance to the goal, the algorithm can be very efficient The worst-case time and space complexity for the tree version is
O b , wherem is the maximum depth of the search space With a good heuristic function, however, the complexity can be reduced substantially The amount of the reduction depends on the particular problem and on the quality of the heuristic 4.4 Illustrate the pseudo code