Uploaded by Bethany Kalember

CSCI323–UniformCostSearch

advertisement
CSCI323 – Modern
AI
Uniform Cost Search Example
Uniform Cost Search
(UCS)
• Uniform Cost Search (UCS) is a
search algorithm used to find
the least cost path from a
start node to a goal node in a
weighted graph.
• It is an uninformed algorithm,
that is, it does not have
prior information about the
path or nodes.
• it is optimal and complete,
that is, it will always find
the least cost path if one
Uniform Cost Search
(UCS)
UCS Algorithm Steps:
1.Initialize:
• Create a priority queue and
insert the start node with a
cost of 0.
• Create a set to keep track
of visited nodes.
Uniform Cost Search
(UCS)
2.Expand Nodes:
• While the priority queue is
not empty:
• Dequeue the node with the
lowest cumulative cost.
• If this node is the goal,
return the path and the
total cost.
Uniform Cost Search
(UCS)
• If this node has not been
visited:
• Mark it as visited.
• For each neighbouring
node, calculate the
cumulative cost and
enqueue the neighbour
with this cost if it has
not been visited or if a
lower cost path to it is
found.
Uniform Cost Search
(UCS)
3.Goal Check:
• If the goal node is reached,
the path and its cost are
returned.
• If the priority queue is
empty and the goal has not
been reached, there is no
path to the goal.
Example
Sta
rt
A 1
4
C
1
A
B
D
Candidat Frontier Visited
e
(Min
Heap)
2
B
C
5
D
E
E
Example
Candidate
𝐴
𝐡
𝐢
𝐷
𝐸
Frontier
(Min
Heap)
Visited
Example
Candidate
Frontier
(Min
Heap)
𝐴
𝐴0
𝐡
𝐢
𝐷
𝐸
Visited
Example
Candidate
Frontier
(Min
Heap)
𝐴
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐢4
𝐡1
Visited
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐡1
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐢4
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐡1
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐢4
𝐷3
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐷3
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐢4
𝐡1
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐷3
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐢4
𝐸8
𝐡1
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐢4
𝐴0
𝐡
𝐸8
𝐡1
𝐢
𝐷
𝐸
𝐷3
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐸8
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐡1
𝐢4
𝐷3
Example
Candidate
Frontier
(Min
Heap)
Visited
𝐴
𝐴0
𝐡
𝐢
𝐷
𝐸
𝐡1
𝐢4
𝐷3
𝐸8
Summarized
• Create and initialize priority
queue (Minimum heap): The
queue is initialized with a
start node with a cumulative
cost of 0.
• Node expansion: Expand root
node of the heap. For each
neighbouring node, if it has
not been visited, it is
enqueued. If it has been
visited, it is skipped.
Summarized
• Path update: The dequeued node
is added to the path.
• Goal check: Check if the goal
state is met or if the
priority queue is empty. If
the goal state is met, the
algorithm stops, and the path
and its costs are returned.
Download