Uniform-Cost Search Algorithm in the AI Environment
1,034 views
19 slides
Jan 29, 2025
Slide 1 of 19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
About This Presentation
Uniform Cost Search (UCS) is a popular search algorithm used in artificial intelligence (AI) for finding the least cost path in a graph.
Size: 514.62 KB
Language: en
Added: Jan 29, 2025
Slides: 19 pages
Slide Content
Uniform Cost Search for
Optimal Path
AI Lab
School of Computer Engineering
Dr Hitesh Mohapatra
Associate Professor
KIIT University
Assignment 4: Uniform Cost Search for
Optimal Path
•Objective:
•Implement Uniform Cost Search for a weighted graph.
•Problem Statement: Given a weighted graph (e.g., a transportation network with travel costs),
find the minimum-cost path between two nodes.
•Tasks:
1.Represent the graph as an adjacency list.
2.Implement Uniform Cost Search to find the optimal path.
3.Compare it with BFS for unweighted graphs.
How Does Uniform Cost Search Work?
1.Initialization:UCSstartswiththerootnode.Itisaddedtothepriorityqueuewitha
cumulativecostofzerosincenostepshavebeentakenyet.
2.NodeExpansion:Thenodewiththelowestpathcostisremovedfromthepriority
queue.Thisnodeisthenexpanded,anditsneighborsareexplored.
3.ExploringNeighbors:Foreachneighboroftheexpandednode,thealgorithmcalculates
thetotalcostfromthestartnodetotheneighborthroughthecurrentnode.Ifaneighbor
nodeisnotinthepriorityqueue,itisaddedtothequeuewiththecalculatedcost.Ifthe
neighborisalreadyinthequeuebutalowercostpathtothisneighborisfound,thecost
isupdatedinthequeue.
4.GoalCheck:Afterexpandinganode,thealgorithmchecksifithasreachedthegoal
node.Ifthegoalisreached,thealgorithmreturnsthetotalcosttoreachthisnodeand
thepathtaken.
5.Repetition:Thisprocessrepeatsuntilthepriorityqueueisemptyorthegoalisreached.
Algorithm
Implementation with
Python
Step 1: Import Required Libraries
Step 2: Define the Uniform Cost Search
Function
Step 3: Define the Path Reconstruction
Function
Step 4: Define the Visualization Function
Step 5: Define the Graph and Execute UCS
Applications of UCS in AI
UniformCostSearchiswidelyapplicableinvariousfieldswithinAI:
1.PathfindinginMaps:Determiningtheshortestroutebetweentwo
locationsonamap,consideringdifferentcostsfordifferentpaths.
2.NetworkRouting:Findingtheleast-costrouteinacommunicationor
datanetwork.
3.PuzzleSolving:Solvingpuzzleswhereeachmovehasacostassociated
withit,suchastheslidingtilespuzzle.
4.ResourceAllocation:Tasksthatinvolvedistributingresourcesefficiently,
wherecostsareassociatedwithdifferentallocationstrategies.
Advantages of Uniform Cost Search
•Optimality:UCSisguaranteedtofindtheleastcostpathtothe
goalstateifthecostofeachstepexceedszero.
•Completeness:Thisalgorithmiscomplete;itwillfinda
solutionifoneexists.
Challenges with UCS
•SpaceComplexity:ThemaindrawbackofUCSisitsspace
complexity.Thepriorityqueuecangrowsignificantly,
especiallyifmanynodesarebeingexpanded.
•TimeComplexity:Thetimeittakestofindtheleastcostpath
canbeconsiderable,especiallyifthestatespaceislarge.
Example
Complexity
•�??????�����??????�����??????�??????��=??????(�
�
)
•??????�������??????�����??????�??????��=??????(�
�
∗
/??????
)
Where,
•Branching Factor (b): The average number of successors per state.
•Depth of the Shallowest Goal Node (d): The depth at which the first goal state is found.
•Maximum Path Cost �
∗
The cost of the optimal solution path.
•Where ??????is the smallest step cost greater than zero