Uniform-Cost Search Algorithm in the AI Environment

1,034 views 19 slides Jan 29, 2025
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
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.


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.

Definition
•UniformCostSearch(UCS)isapopularsearchalgorithmused
inartificialintelligence(AI)forfindingtheleastcostpathina
graph.
•ItisavariantofDijkstra'salgorithmandisparticularlyuseful
whenalledgesofthegraphhavedifferentweights,andthe
goalistofindthepathwiththeminimumtotalcostfromastart
nodetoagoalnode.

Key Concepts of Uniform Cost Search
1.PriorityQueue:UCSusesapriorityqueuetostorenodes.Thenodewith
thelowestcumulativecostisexpandedfirst.Thisensuresthatthesearch
exploresthemostpromisingpathsfirst.
2.PathCost:Thecostassociatedwithreachingaparticularnodefromthe
startnode.UCScalculatesthecumulativecostfromthestartnodetothe
currentnodeandprioritizesnodeswithlowercosts.
3.Exploration:UCSexploresnodesbyexpandingtheleastcostlynode
first,continuingthisprocessuntilthegoalnodeisreached.Thepathto
thegoalnodeisguaranteedtobetheleastcostlyone.
4.Termination:Thealgorithmterminateswhenthegoalnodeisexpanded,
ensuringthatthefirsttimethegoalnodeisreached,thepathisthe
optimalone.

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

Conclusion
•UniformCostSearchisapowerfulalgorithminAIforsituations
wherepathshavedifferentcostsandthegoalistominimize
thetotalcost.
•Itsapplicationacrossvariousdomainsshowcasesitsversatility
andeffectiveness.
•However,understandingitscomputationalrequirementsis
crucialforpracticalimplementations,especiallyinscenarios
withlargedatasetsorlimitedcomputationalresources

https://github.com/hm18818/AI-with-
Python/blob/main/Uniform%20Cost%20Search%2
0(UCS)%20in%20AI