CS 3491- AI & ML
Unit I
Problem Solving
Introduction to AI - AI Applications - Problem solving
agents – search algorithms – uninformed search
strategies – Heuristic search strategies – Local search
and optimization problems – adversarial search –
constraint satisfaction problems (CSP)
INTRODUCTION TO ARTIFICIAL INTELLIGENCE
• Artificial Intelligence is a branch of computer science that deals with
developing intelligent machines which can behave like human, think like
human, and has ability to take decisions by their own.
• Artificial Intelligence is a combination of two words Artificial and
Intelligence, which refers to man-made intelligence.
• Therefore, when machines are equipped with man-made intelligence to
perform intelligent tasks similar to humans, it is known as Artificial
Intelligence.
• It is all about developing intelligent machines that can simulate the human
brain and work & behave like human beings
History of AI
• Concept of intelligent machines was found in Greek Mythology.
• Below are some keystones in the development of AI:
• In the year 1943, Warren McCulloch and Walter pits proposed a model of
Artificial neurons.
• In the year 1950, Alan Turing published a "Computer Machinery and
Intelligence" paper in which he introduced a test, known as a Turing Test. This
test is used to determine intelligence in machines by checking if the machine is
capable of thinking or not.
• In the year 1956, for the first time, the term Artificial Intelligence was coined by
the American Computer scientist John Mc Carthy at the Dartmouth Conference.
John McCarthy is also known as the Father of Al.
• In the year 1972, the first full-scale intelligent humanoid robot,
WABOTI, was created in Japan.
• In the year 1980, AI came with the evolution of Expert Systems. These
systems are computer programs, which are designed to solve complex
problems.
• In the year 1997, IBM Deep Blue beat world chess champion Gary
Kasparov and became the first computer to defeat a world chess
champion.
• In the year 2006, AI came into the business world. World's top
companies like Facebook, Twitter, and Netflix also started using Al in
their applications.
Recent Developments (2010s-Present)
• Advancements in NLP and GPT: Natural language processing (NLP)
has made huge strides, with models like OpenAI’s GPT (Generative pre-
trained Transformer) series and BERT (Bidirectional Encoder
Representations from Transformer) transforming how machines
understand and generate human language.
• AI in Everyday Life: AI has become integrated into everyday products,
from recommendation systems (like Netflix or Amazon) to autonomous
vehicles and healthcare diagnostics.
• Ethical and Societal Considerations: With the rise of AI, there have been
growing concerns about its impact on jobs, privacy, security, and ethics.
The development of AI governance, fairness, and transparency is
becoming an important area of research.
TYPES OF ARTIFICIAL INTELLIGENCE
• Artificial Intelligence can be divided in various types, there are mainly
two types of main categorization which are
• Based on capabilities and
• Based on functionality
ARTIFICIAL INTELLIGENCE TYPE-1: BASED ON
CAPABILITIES
1. Weak Al or Narrow Al:
• Narrow AI refers to AI systems that are designed and trained to perform specific tasks.
These systems are highly specialized in one area but cannot perform tasks beyond their
design. Most of the AI in use today falls into this category.
Examples:
• Speech Recognition: Voice assistants like Siri or Alexa can recognize and respond to
speech but cannot perform any task outside of their predefined abilities.
• Image Recognition: AI systems used in medical imaging can identify certain diseases or
conditions from scans, but they can't generalize to other tasks like playing chess or driving
a car.
• Recommendation Systems: AI in platforms like Netflix or Amazon that suggests content
based on user behavior and preferences is narrow AI.
• Autonomous Vehicles: AI in self-driving cars that handles navigation and driving in
specific environments is another example of narrow AI.
Key Characteristics:
• Task-specific
• Performs at human or superhuman level in one area
• Cannot generalize beyond its defined task
• Most common form of AI today
2. General Al:
• General AI refers to an AI system that has the ability to understand,
learn, and apply knowledge across a wide range of tasks in much the
same way that humans can.
• It is capable of generalizing its learning to solve problems that it wasn’t
specifically trained for.
Examples:
• Hypothetical AGI Systems: These do not exist yet, but an AGI system
would be able to do things like read books, learn new skills, solve
scientific problems, design new technologies, and engage in creative
work all without requiring task-specific training. It would perform at or
above human cognitive levels across different domains.
Key Characteristics:
• Can understand, learn, and apply knowledge across many domains
Similar to human-level intelligence
• Capable of performing a broad range of tasks without being
reprogrammed for each new task
• Currently theoretical and not yet achieved
3. Super Al:
• Superintelligent AI would surpass human intelligence in every aspect:
creativity, problem-solving, social interaction, and even emotional
intelligence. This type of AI would have the ability to solve problems that are
currently unsolvable and could outperform humans in virtually all fields of
expertise.
Examples:
• Hypothetical Superintelligent Systems: These might not exist today, but if
created, they could revolutionize everything from science to governance. For
instance, a superintelligent AI might be able to solve global issues like
climate change, poverty, and disease with unprecedented speed and
efficiency.
Key Characteristics:
• Surpasses human intelligence in all domains
• Can solve problems beyond human comprehension
• Theoretical and often discussed in the context of the potential future
risks of AI development
• Could be self-improving, meaning it could rapidly enhance its own
capabilities, making it a significant challenge for control and oversight
ARTIFICIAL INTELLIGENCE TYPE-2: BASED ON
FUNCTIONALITY
1. Reactive Machines:
These AI systems only react to the environment and do not store
or use past experiences to influence decisions. They perform a specific
task based on current inputs.
Example:
IBM's Deep Blue, which beat the world chess champion, is a
reactive machine. It evaluates millions of possible chess moves but has
no memory of past games.
2. Limited Memory
Limited Memory: These AI systems can use historical data to make
decisions. They store past experiences and use them to inform future
actions. This is the type of AI most commonly found in self-driving
cars.
Example:
Self-driving cars that monitor and adjust their route based on past
experiences, like avoiding a traffic jam based on historical data from
other vehicles.
3. Theory of Mind
Theory of Mind:
• This is a more advanced form of AI that is still under research.
• It would require the AI to understand human emotions, beliefs, and
intentions, allowing for better interaction with humans.
• This type of AI could have more human-like social and emotional
intelligence.
Example:
Hypothetical AI systems capable of understanding the mental states
of humans, such as a robot therapist that can empathize and adjust its
responses based on your emotional state.
4. Self-Awareness
Self-Aware AI:
• The most advanced form of AI that would not only understand human
emotions but also be aware of its own existence and have its own
desires, goals, and emotions.
• This remains a theoretical concept.
Example:
• AI systems that are aware of their own capabilities, limitations, and
possibly even their own existence, akin to how humans are aware of
themselves.
A.I Approaches
• The definitions of Al according to some text books are categorized
into four approaches and are summarized in the table below:
• Systems that think like human
• Systems that act like human
• Systems that think rationally
• Systems that act rationally
Thinking humanly: The cognitive modeling approach
• These AI systems are designed to simulate human-like thought processes. The
goal is to replicate the way humans reason, perceive, understand, and make
decisions. Cognitive AI typically includes learning, reasoning, memory,
perception, and language processing.
Key Characteristics:
• Cognitive Models: These AI systems model human cognition and thought
processes.
• Perception: They simulate how humans perceive the world, process sensory
data, and interpret context.
• Learning: These systems can learn from experience and adapt their behavior,
similar to how humans do through trial and error or feedback.
• Memory and Attention: Cognitive AI systems have memory that allows them to
store experiences and knowledge, much like how humans recall past experiences.
Examples:
• IBM Watson: Watson was designed to process natural language,
understand context, and simulate human reasoning. It was famously
able to win the game show Jeopardy! by using cognitive computing
techniques.
• Cognitive Computing in Healthcare: AI systems in healthcare, like
PathAI, attempt to "think" in a way that mimics human doctors,
analyzing medical images and data to diagnose diseases and provide
treatment suggestions.
Acting humanly: The Turing Test approach
• This approach aims to replicate human behavior, often focusing on producing
outputs and actions that seem human-like.
• These systems may not necessarily think in the same way humans do, but
they are programmed to interact with the world or users in ways that seem
rational, empathetic, and socially aware.
Key Characteristics:
• Human Interaction: AI systems are designed to interact in a way that feels
natural or human-like, such as using natural language and facial expressions.
• Social Behavior: These AI systems are capable of understanding and
responding to social cues (e.g., tone of voice, gestures, or facial expressions).
• Emotional Intelligence: Some systems may simulate emotions or empathy,
helping them to act more human-like in interactions.
Examples:
• Social Robots (e.g., SoftBank's Pepper): These robots are designed
to interact with people in a human-like manner, interpreting human
emotions and responding accordingly. Pepper can understand speech,
recognize faces, and simulate empathy to interact naturally with
humans.
• Virtual Assistants (e.g., Siri, Alexa, and Google Assistant): While
not truly human-like in terms of cognition, these virtual assistants
respond to voice commands and can simulate human conversation
with natural language processing and contextual understanding.
Thinking rationally: The "laws of thought" approach
• The concept of "right thinking" was proposed by Aristotle.
• His syllogisms provided patterns for argument structures that always yielded
correct conclusions when given correct premises.
• Syllogisms: A logical structure involving a major premise, a minor premise, and
a conclusion. For example: "All humans are mortal" (major premise), "Socrates
is a human" (minor premise), "Therefore, Socrates is mortal" (conclusion).
• Patterns for argument structures: These are the specific ways or methods of
arranging logical premises and conclusions to ensure that reasoning is clear and
valid.
• Correct conclusions from correct premises: If the initial facts (premises) are
true, the conclusion reached through the syllogism will also be true.
• These laws of thought were supposed to govern the operation of the mind; their
study initiated the field called logic.
Acting rationally: The rational agent approach
• "Acting rationally" refers to making decisions or taking actions based on
logic, reason, and the goal of achieving the best possible outcome, given the
available information.
• Rational agent: An entity that takes actions to achieve a particular objective,
using reasoning and knowledge to make decisions. It tries to select the best
possible choice based on the information at hand.
• Acting rationally: This means making decisions that are logically sound,
consistent with the agent's goals, and based on available evidence and
reasoning.
Examples:
• Autonomous Vehicles: Self-driving cars, like those developed by Tesla
or Waymo, are rational agents. They use sensors and algorithms to make
real-time decisions about navigation, speed, and safety to reach their
destination efficiently and safely.
• Robotic Process Automation (RPA): RPA systems are used to automate
repetitive tasks in business operations. These systems act rationally by
performing tasks based on predefined rules and decision-making logic.
• Reinforcement Learning Agents: In reinforcement learning, AI agents
like AlphaGo (developed by DeepMind) learn to make rational decisions
by receiving rewards or penalties based on the actions they take. These
agents maximize their rewards over time by learning optimal strategies.
APPLICATIONS OF AI
• 1. Game Playing: Al is widely used in Gaming. Different strategic
games such as Chess, where the machine needs to think logically, and
video games to provide real-time experiences use
Artificial Intelligence.
• 2. Robotics: Artificial Intelligence is commonly used in the field of
Robotics to develop intelligent robots. Al implemented robots use real-
time updates to sense any obstacle in their path and can change the
path instantly. Al robots can be used for carrying goods in hospitals
and industries and can also be used for other different purposes.
• 3. Healthcare: In the healthcare sector, Al has diverse uses. In this
field, Al can be used to detect diseases and cancer cells. It also helps in
finding new drugs with the use of historical data and medical
intelligence.
• 4. Computer Vision: Computer vision enables the computer system to
understand and derive meaningful information from digital images,
video, and other visual input with the help of AI.
• 5. Agriculture: Al is now widely used in Agriculture; for example,
with the help of Al, we can easily identify defects and nutrient
absences in the soil. To identify these defects, Al robots can be
utilized. Al bots can also be used in crop harvesting at a higher speed
than human workers
• 6. E-commerce: Al is one of the widely used and demanding
technologies in the E-commerce industry. With Al, e-commerce
businesses are gaining more profit and grow in business by
recommending products as per the user requirement.
• 7. Social Media: Different social media websites such as Facebook,
Instagram, Twitter, etc., use Al to make the user experiences much
better by providing different features. For example, Twitter uses Al to
recommend tweets as per the user interest and search history.
Advantages of Artificial Intelligence
• High Accuracy with less errors
• High Speed
• High Reliability
• Useful for risky areas
• Digital assistant
• Useful as a public utility
Disadvantages of Artificial Intelligence
• High cost
• Cant think out of box
• No feeling and emotion
• Increase dependency on machines
• No original creativity
Problem-solving agents
• Intelligent agent in artificial intelligence (AI) designed to solve specific
problems by taking actions to transform the current state into a desired goal
state.
• These agents use systematic methods and strategies to find solutions to
problems.
• Sensor: Sensor is a device which detects the change in the environment
and sends the information to other electronic devices. An agent observes its
environment through sensors.
• Actuators: Actuators are the component of machines that converts energy
into motion. The actuators are only responsible for moving and controlling
a system. An actuator can be an electric motor, gears, rails, etc.
• Examples of agents
Human agent
Sensor: eye, ears etc.
Actuators: hands, legs
Robotic agent
Sensor: camera
Actuators: brakes, motors
Software agent
Sensor: Keyboard, file content
Actuator: monitor, printer
Problem Solving Agents
Agents and Environments
• An agent is anything that
• Perceives its environment through sensors and
• Acts upon that environment through actuators.
• Example : Vacuum cleaner agent
Goal: To clean up the whole area
Environment: Square A and B
Percepts: locations and status e.g [A, Dirty]
Actions: Left, Right, Suck, No Operation
Percept: Term that refer to agent’s inputs at any given instant
Percept Sequence: An agent’s percept sequence is the complete
history of everything the agent has ever perceived,
Agent function: Maps given percept sequence to an action
Agent Program:
• Agent function for an artificial agent will be implemented by an agent
program.
• The agent function is an abstract mathematical description; the agent
program is a concrete implementation, running on the agent
architecture.
• Rational Agent
• A rational agent is one that does the right thing.
• It maximizes the performance measure which makes an
agent more successful
• Rationality of an agent depends on the following
• The performance measure that defines the criterion of
success.
• Example: vacuum-cleaner amount of dirt cleaned up,
amount of electricity consumed, amount of noise
generated, etc..
• The agent’s prior knowledge of the environment.
• The actions that the agent can perform.
• The agent’s percept sequence to date.
Key Components of Problem-Solving Agents:
• Agent: An agent is an entity that perceives its environment through
sensors and acts upon it with actuators to achieve specific goals.
• Problem: A problem is defined by an initial state, a set of possible
actions (or operators), and a goal state that the agent seeks to achieve.
Components of a Problem-Solving Process:
• Initial State: This is the state of the world (or environment) when the
problem-solving process begins. The agent starts here.
• Actions: These are the possible moves or operations that the agent can
perform in order to transition from one state to another.
• State Space: The set of all possible states the agent can reach, starting from
the initial state and applying a sequence of actions.
• Goal State: This is the desired state or the outcome that the agent is trying to
reach. Solving the problem means finding a path from the initial state to the
goal state.
• Path to the Goal: This is a sequence of actions (or steps) taken by the agent
from the initial state to reach the goal state.
Types of Agents
• Simple Reflex agent
• Model-Based Reflex agent
• Goal-Based Agent
• Utility-Based Agent
• Learning Agent
Simple Reflex Agent
• They choose actions only based on the current situation ignoring
the history of perceptions.
• They will work only if the environment is fully observable
• Does not consider any part of percept history during their
decision and action process.
• Example:
• A thermostat that turns on/off based on temperature.
• A motion-sensing light that turns on when movement is
detected.
Model-Based Reflex Agent
• A model based-reflex agent is an intelligent agent that uses percept
history and internal memory to make decisions about the “model” of
the world around it.
• The model-based agent can work on a partially observable
environment
• Model- knowledge about “how the things happen in the world”
• Internal State- It is a representation of the current state depending on
percept history.
• Example:
• A self-driving car using maps and sensors to navigate roads.
• A chatbot that remembers past user inputs for better responses.
Goal Based Agent
• They choose their actions in order to achieve goals
• This allows the agent a way to choose among multiple possibilities,
selecting the one which reaches a goal state
• They usually require search and planning
• Example:
• A GPS system finding a path to certain destination
• Chess-playing AI that plans moves to checkmate an opponent.
Utility Based Agent
• A utility based agent is an agent that acts based not only on what the
goal is, but the best way to reach the goal.
• They choose actions based on a preference (utility) for each state
• Example:
• A GPS finding a shortest / fastest/ safer to certain destination
• Stock market prediction algorithms that optimize profit.
• Ride-sharing apps that match drivers and passengers efficiently.
Learning Agents
• A learning agent in AI is the type of agent which can learn from its past
experiences, or it has learning capabilities.
• A learning agent has mainly four conceptual components, which are:
• Learning element: It is responsible for making improvements by
learning from environment
• Critic: Learning element takes feedback from critic which describes
that how well the agent is doing with respect to a fixed performance
standard.
• Performance element: It is responsible for selecting external
action.
• Problem generator: This component is responsible for suggesting
actions that will lead to new and informative experiences.
• Hence, learning agents are able to learn, analyze performance, and
look for new ways to improve the performance.
• Example:
• AI-based virtual assistants (e.g., Siri, Alexa) that improve responses
over time.
• Recommendation systems (e.g., Netflix, YouTube) that suggest
content based on user preferences.
PEAS Representation
• The PEAS framework is a way to describe the structure of intelligent
agents. PEAS stands for Performance measure, Environment,
Actuators, and Sensors. This representation helps in defining the task
environment of an agent in a clear and structured manner.
• 1. Performance Measure (P)
• The performance measure is a quantitative criterion that defines how
well an agent is achieving its goals. It represents the success of the
agent's actions and behavior.
• It is used to evaluate the agent’s performance and determine if the
goals are being achieved effectively.
• It could be based on various factors like speed, accuracy, efficiency, cost,
or satisfaction.
• Examples:
• For a self-driving car, the performance measure might include safe
navigation, fuel efficiency, and travel time.
• For a chess-playing agent, the performance measure might be the
number of games won or the quality of moves.
• 2. Environment (E)
• The environment refers to the external context or setting in which the
agent operates. It includes everything that the agent interacts with and
needs to respond to, such as other agents, objects, and obstacles.
• The environment can be static or dynamic, observable or partially
observable, deterministic or stochastic, etc.
• The environment defines the boundaries within which the agent must
operate.
• Examples:
• In a self-driving car, the environment includes the roads, traffic
signals, pedestrians, other vehicles, and weather conditions.
• In a chess game, the environment is the chessboard and the pieces.
• 3. Actuators (A)
• The actuators are the mechanisms or components through which an
agent takes actions to affect the environment. These are the tools or
devices that allow the agent to perform physical or digital actions.
• Actuators allow the agent to take actions that change the state of the
environment or move the agent toward achieving its goals.
• These can include motors, wheels, robotic arms, or software-based
actions like sending a message or updating a system.
• Examples:
• In a self-driving car, actuators include the steering wheel, accelerator,
brakes, and signals.
• In a robot, actuators include motors that control its limbs and sensors
that allow it to interact with objects.
• 4. Sensors (S)
• Sensors are the components that allow the agent to perceive its
environment. They gather information from the external world that the
agent uses to make decisions.
• Sensors allow the agent to receive feedback about the current state of
the environment, enabling it to adjust its actions accordingly.
• They can include cameras, temperature sensors, microphones, or
specialized sensors like infrared sensors.
• Examples:
• In a self-driving car, sensors include cameras, radar, Lidar, GPS, and
ultrasonic sensors.
• In a robot, sensors could include touch sensors, temperature sensors, or
cameras that help the robot understand its surroundings.
PEAS Representation Example: Self-Driving Car
Component Description
Performance Measure - Safe navigation (avoiding accidents)
- Fuel efficiency
- Travel time (minimizing delays)
- Passenger comfort (smooth driving)
- Compliance with traffic laws
Environment - Roads and highways
- Traffic signals and signs
- Other vehicles
- Pedestrians
- Weather conditions
- Obstacles (e.g., potholes, construction)
Actuators - Steering wheel motors
- Braking system
- Accelerators
- Turn signals
- Horn
- Lights
Sensors - Cameras
- Lidar (Light Detection and Ranging)
- Radar
- Ultrasonic sensors
- GPS
- Inertial measurement units (IMUs)
- Environmental sensors
PEAS Representation Example: Chess Agent
Component Description
Performance Measure - Number of games won
- Speed of decision-making
- Quality of moves (e.g., winning a game or achieving checkmate)
Environment - The chessboard
- Chess pieces (pawns, rooks, knights, etc.)
- The opponent (human or AI player)
Actuators - Move pieces on the chessboard
Sensors - The board configuration (location of pieces)
- State of the game (whose turn it is, captured pieces, etc.)
SEARCH ALGORITHM
• Search algorithms are one of the most important areas of Artificial
Intelligence.
• In Artificial Intelligence, Search techniques are universal problem-
solving methods.
• Rational agents or Problem-solving agents in Al mostly used these
search strategies or algorithms to solve a specific problem and provide
the best result.
• Problem-solving agents are the goal-based agents and use atomic
representation.
SEARCH ALGORITHM
TERMINOLOGIES
• Search: Searching is a step by step procedure to solve a search-
problem in a given search space. A search problem can have
three main factors:
1. Search Space: Search space represents a set of possible solutions,
which a system may have.
2. Start State: It is a state from where agent begins the search.
3. Goal Test: It is a function which observe the current state and returns
whether the goal state is achieved or not.
• Search tree: A tree representation of search problem is called Search
tree. The root of the search tree is the root node which is corresponding
to the initial state.
• Actions: It gives the description of all the available actions to the agent.
• Transition model: A description of what each action do, can be
represented as a transition model.
• Path Cost: It is a function which assigns a numeric cost to each path.
• Solution: It is an action sequence which leads from the start node to the
goal node.
• Optimal Solution: If a solution has the lowest cost among all solutions.
PROPERTIES OF SEARCH ALGORITHMS
• Following are the four essential properties of search algorithms to
compare the efficiency of these algorithms:
1.Completeness: A search algorithm is said to be complete if it guarantees
to return a solution if at least any solution exists for any random input.
2.Optimality: If a solution found for an algorithm is guaranteed to be the
best solution (lowest path cost) among all other solutions, then such a
solution for is said to be an optimal solution.
3.Time Complexity: Time complexity is a measure of time for an algorithm
to complete its task.
4.Space Complexity: It is the maximum storage space required at any point
during the search, as the complexity of the problem.
Search Algorithms: Types
Uninformed Search Strategies
• Uninformed search algorithms have no additional information on the
goal node other than the one provided in the problem definition.
• Uninformed search in called blind search
• Types
1. Breadth First Search
2. Uniform Cost Search
3. Depth First Search
4. Depth Limited Search
5. Iterative deepening depth-first search
6. Bidirectional search
Breadth First Search
• In breadth- first search the tree or graph is traversed breadthwise.
• It starts from the root node, explores the neighboring nodes first and
moves towards the next level neighbors.
• It is implemented using the queue data structure that works on the
concept of first in first out (FIFO).
Breadth First Search Key Points:
1. Queued
2. Visited
Algorithm of Breadth- First Search
Step 1: Consider the graph you want to navigate.
Step 2: Select any vertex in your graph (say v1), from which you want to
traverse the graph.
Step 3: Utilize the following two data structures for traversing the graph
• Visited array (Explored)
• Queue data structure
Step 4: Add the starting vertex to the visited array, and afterward, you add
v1’s adjacent vertices to the queue data structure
Step 5: Now using FIFO concept, remove the first element from the queue,
put it into the visited array, and then add the adjacent vertices of the
removed element to the queue.
Step 6: Repeat step 5 until the queue is not empty and no vertex is left to be
visited (goal is reached).
• Advantages:
• BFS will provide a solution if any solution exists.
• If there are more than one solutions for a given problem, then BFS
will provide the minimal solution which requires the least number of
steps.
• Disadvantages
• It requires lots of memory since each level of the tree must be saved
into memory to expand the next level.
• BFS needs lots of time if the solution is far away from the root node.
Key Points:
2. Uniform Cost Search 1. Minimum Cumulative Cost
2. Goal Test
A
2 3
B C
1 3 1 2
2
D E F G H
3 1
2 3 1
I J K L M
• Uniform Cost Search is a searching algorithm used for
traversing a weighted tree or graph.
• The primary goal of uniform-cost search is to find a path
to the goal node which has the lowest cumulative cost.
• Uniform-cost search expands nodes according to their path
costs from the root node.
• It can be used to solve any graph/tree where the optimal
cost is in demand.
• A uniform-cost search algorithm is implemented by the
priority queue.
• It gives maximum priority to the lowest cumulative cost.
Algorithm
• Step 1: Insert the root node into the priority queue
• Step 2: Remove the element with the highest priority. (min.
cumulative cost)
• If the removed node is the destination (goal), print total
cost and stop the algorithm
• Else if, Check if the node is in the visited list (explored
set)
• Step 3: Else Enqueue all the children of the current node to
the priority queue, with their cumulative cost from the root
as priority and the current node to the visited list.
• Advantages:
• Uniform cost search is optimal because at every state the path
with the least cost is chosen.
• Disadvantages:
• It does not care about the number of steps involve in searching
and only about path cost. Due to which this algorithm may be
stuck in an infinite loop.
DEPTH-FIRST SEARCH
• Depth-first search is a recursive algorithm for traversing a tree or
graph data structure.
• It is called the depth-first search because it starts from the root node
and follows each path to its greatest depth node before moving to the
next path.
• DFS uses a stack data structure for its implementation.
• The process of the DFS algorithm is similar to the BFS algorithm.
• Depth First Search Algorithm Key Points:
1. Stack(Depth Wise)
2. Goal Test (Backtrack)
Step 1: create a set or array to keep track of visited nodes.
Step 2: choose the starting node.
Step 3: create an empty stack and push the starting node on the stack.
Step 4: Mark the starting node as visited.
Step 5: while the stack is not empty do the following:
1. Pop a node from he stack.
2. Process or perform any necessary operations on the popped node.
3. Get all the adjacent neighbors of the popped node.
4. For each adjacent neighbor, if it has not been visited do the following:
• mark the neighbor as visited.
• Push the neighbor onto the stack
Step 6: Repeat step 5 until stack is empty
• Advantage:
• DFS requires very less memory as it only needs to store a stack of the
nodes on the path from root node to the current node.
• It takes less time to reach to the goal node than BFS algorithm (if it
traverses in the right path).
• Disadvantage:
• There is the possibility that many states keep re-occurring, and there is
no guarantee of finding the solution.
• DFS algorithm goes for deep down searching and sometime it may go to
the infinite loop.
Depth Limited search Algorithm
Key-points
• A depth-limited Search algorithm is similar to depth-
first search with a predetermined limit.
• Depth-limited search can solve the drawback of infinite
path in the Depth-first search.
• Depth-limited search can be terminated with two
conditions of failure:
• Standard failure value: It indicates that problem does
not have any solution
• Cutoff failure value: It defines no solution for the
problem within a given depth limit.
• Advantages
• Depth-limited search is Memory efficient.
• Disadvantages
• Depth-limited search also has a disadvantage of
incompleteness.
• It may not be optimal if the problem has more than one
solution.
Key-points
1. Iterative deepening search (or iterative deepening depth-
first search) is a general strategy, often used in combination
with depth-first tree search, that finds the best depth limit. It
does this by gradually increasing the limit—first 0, then 1,
then 2, and so on—until a goal is found.
2. This will occur when the depth limit reaches d, the depth of
the shallowest goal node.
3. Iterative deepening combines the benefits of depth-first
and breadth-first search.
• The idea behind bidirectional search is to run two simultaneous
searches—one forward from the initial state and the other backward
from the goal—hoping that the two searches meet in the middle.
• Bidirectional search is implemented by replacing the goal test with a
check to see whether the frontiers of the two searches intersect; if
they do, a solution has been found.
• It is important to realize that the first such solution found may not be
optimal, even if the two searches are both breadth-first; some
additional search is required to make sure there isn’t another short-cut
across the gap.
• The reduction in time complexity makes bidirectional search
attractive,
Informed Search Strategies (Heuristic)
• Informed Search algorithm contains an additional
knowledge about the problem that helps direct search to
more promising paths.
• This knowledge helps in more efficient searching
• Informed Search is also called as Heuristic search
1. Greedy Best First Search
2. A* Search
3. Local search Algorithm
Best First Search
• Best First Search algorithm always selects the path which appears best
at that moment.
• The aim is to reach the goal from initial state via the shortest path.
Heuristic
• A heuristic is an approximate measure of how close you are to the
target.
• Must be zero if node represents a goal node.
Greedy Best First Search
• Combination of depth-first search and breadth- first
search
• In the greedy best first search algorithm, we expand the node
which is closest to the goal node which is estimated by
heuristic function h(n)
f(n)=h(n)
Where
h(n) = estimated cost from node n to the goal.
An example of the best-first search algorithm is below graph, suppose we have to find the path from A to G
1) We are starting from A , so from A there are direct path to node B( with
heuristics value of 32 ) , from A to C ( with heuristics value of 25 ) and from A to
D( with heuristics value of 35 ) .
2) So as per best first search algorithm choose the path with lowest heuristics
value , currently C has lowest value among above node . So we will go from A to
C.
3) Now from C we have direct paths as C to F( with heuristics value of 17 ) and
C to E( with heuristics value of 19) , so we will go from C to F.
4) Now from F we have direct path to go to the goal node G ( with heuristics
value of 0 ) , so we will go from F to G.
5) So now the goal node G has been reached and the path we
will follow is A->C->F->G .
Best Search Algorithm
• Step 1: Place the starting node into the OPEN list.
• Step 2: If the OPEN list is empty, Stop and return failure.
• Step 3: Remove the node n from the OPEN list, which has the lowest
value of h(n) and places it in the CLOSED list.
• Step 4: Expand the node n, and generate the successors of node n.
• Step 5: Check each successor of node n, and find whether any node is a
goal node or not. If any successor node is the goal node, then return
success and stop the search, else continue to next step.
• Step 6: For each successor node, the algorithm checks for evaluation
function f(n) and then check if the node has been in either OPEN or
CLOSED list. If the node has not been in both lists, then add it to the
OPEN list.
• Step 7: Return to Step 2.
Advantages:
• 1. Greedy best first search switch between BFS and DFS by gaining
the advantages of both the algorithms.
• This algorithm is more efficient than BFS and DFS.
Disadvantages:
• 1. it can stuck in a loop as DFS.
• This algorithm is not optimal.
A* Search
• In A* search algorithm, we use search heuristic h(n) as well as the
cost to reach the node g(n). Hence we can combine both costs as
following, and this sum is called as a fitness number f(n).
• It has combined features of uniform cost search and greedy best-first
search.
f(n)= g(n)+h(n)
Where,
• g(n)- cost of the path from start node to node n
• h(n)- cost of the path from node n to goal state (heuristic function)
State H(n)
B
2 5 S 5
D A 3
A
1 3
B 4
C
6 C 2
1 4
D 6
10 G 0
S G
• 1) We are starting from S , so from A there are direct path to node A
having g(n) value 1 and with h(n) value of 3 so f(n)=1+4=5 , from S
to G having g(n) value 10 and with h(n) value of 0 so f(n)=10+0=10 .
• So as per search algorithm choose the path with lowest value ,
currently A has lowest value among above node . So we will go from S
to A.
• 2) Now from S to A there are direct path to node B having g(n) value 3
and with h(n) value of 4 so f(n)=3+4=7 , from S to A to C having g(n)
value 2 and with h(n) value of 2 so f(n)=2+2=4.
• So as per search algorithm choose the path with lowest value ,
currently C has lowest value among above node . So we will go from S
to A to C.
• Now from S to A to C there are direct path to node D having g(n) value
5 and with h(n) value of 6 so f(n)=5+6=11 , from S to A to C to G
having g(n) value 6 and with h(n) value of 0 so f(n)=6+0=6.
• So as per search algorithm choose the path with lowest value ,
currently G has lowest value among above node . So we will go from S
to A to C to G.
• So now the goal node G has been reached and the path we will follow
is S->A->C->G
• Algorithm of A search:
• Step 1: Place the starting node in the OPEN list.
• Step 2: Check if the OPEN list is empty or not. If the list is empty, then return
failure and stops.
• Step 3: Select the node from the OPEN list which has the smallest value of
the evaluation function (g + h) If node n is the goal node, then return success
and stop, otherwise.
• Step 4: Expand node n and generate all of its successors, and put into the
closed list. For each successor n' check whether n' is already in the OPEN or
CLOSED list. If not, then compute the evaluation function for n' and place it
into the Open list.
• Step 5:Else, if node n' is already in OPEN and CLOSED, then it should be
attached to the back pointer which reflects the lowest g(n') value.
• Step 6:Return to Step 2.
Advantages:
• A* search algorithm is the best algorithm than other search algorithms.
• A* search algorithm is optimal and complete.
• This algorithm can solve very complex problems.
Disadvantages:
• It does not always produce the shortest path as it mostly based on
heuristics and approximation.
• A* search algorithm has some complexity issues.
• The main drawback of A* is memory requirement as it keeps all
generated nodes in the memory, so it is not practical for various
large-scale problems.
Local Search Algorithm
• The local search algorithm searches only the final state, no the path
to get there.
• For example, 8 in the 8- queens problem,
• We care only about finding a valid final configuration of 8 queens (8
queens arranged on chess board, and no queen can attack other
queens) and not the path from initial state to final state.
• Local search algorithms operate by searching from start state to
neighboring states,
• Without keeping track of the paths, nor set of states that have been
reached
Local Search Algorithm
• They are not systematic-
• They might never explore a portion of the search space where
a solution actually resides.
• They search only the final state
1. Hill-climbing
2. Simulated Annealing
3. Local Beam Search
Hill- climbing search algorithm
• Hill-climbing algorithm is a Heuristic search
algorithm which continuously moves in the
direction of increasing value to find the peak of
the mountain or best solution to the
problem.
• It keeps track of one current state and on each
iteration moves to the neighboring state with
highest value- that is, its heads in the direction
that provides the steepest ascent.
Different regions in the state space landscape:
Local Maximum : Local maximum is a state which is better
than its neighbor states , but there is also another state which is
higher than it.
Global Maximum: Global maximum is the best possible state
of state space. It has the highest value of the object function.
Current state: It is a state in a landscape diagram where an
agent is present
Flat local maximum: It is a flat space in the landscape where
all the neighbor states of current states have the same value.
Shoulder : It is a plateau region which has an uphill edge
Key points
• Hill climbing algorithm is a local search algorithm which continuously
moves in the direction of increasing elevation/value to find the peak
of the mountain or best solution to the problem. It terminates when
it reaches a peak value where no neighbor has a higher value.
• Hill climbing algorithm is a technique which is used for optimizing the
mathematical problems. One of the widely discussed examples of Hill
climbing algorithm is Traveling-salesman Problem in which we need
to minimize the distance traveled by the salesman.
• It is also called greedy local search as it only looks to its good
immediate neighbor state and not beyond that.
• A node of hill climbing algorithm has two components which are state
and value.
• Algorithm for simple Hill climbing search.
• Step 1: Evaluate the initial state. If it is the goal state, then return
success and Stop.
• Step2: Loop Until a solution is found or there is no new operator left
to apply.
• Step 3:Select and apply an operator to the current state.
• Step 4:Check new state: If it is a goal state, then return to success and
quit. Else if it is better than the current state, then assign a new state as
a current state. Else if not better than the current state, then return to
step 2.
• Step 5: Exit.
Key points
• Hill Climbing is mostly used when a good heuristic is available.
• In this algorithm, we don't need to maintain and handle the search tree
or graph as it only keeps a single current state.
2. Simulated Annealing
• Never makes “downhill”
• Get stuck on a local maximum
• Purely Random Walk- Successor chosen uniformly at random from
the set of successors – is complete but extremely inefficient
• Simulated annealing combines hill climbing with random walk in
some way it yields both efficiency and completeness
• In metallurgy annealing is the process used to temper or harden metals
and glass by heating them to a high temperature and then gradually
cooling them, thus allowing the material to reach a low energy
crystalline state.
3. Local Beam Search
• Storing the single state value in memory
• Keeps tracks of k states rather than one.
• Begins with k randomly generated states
• At each step all successors of all k states are generated. If any one is a goal,
the algorithm halts.
• Otherwise, it selects the k best successors from the complete list and
repeats. (If goal not found repeat)
• In a random restart search every single search activity run
independently of others
• To implement local search, threads are used. The k parallel threads carry
useful information
• Limitation: concentrate on small area of the state space become more
expensive
Stochastic Beam Search
• Concentrate on random selection of k successor instead of selecting k
best successor
Adversarial Search
• In previous topics, we have studied the search strategies which are
only associated with a single agent that aims to find the solution
which often expressed in the form of a sequence of actions.
• But, there might be some situations where more than one agent is
searching for the solution in the same search space, and this
situation usually occurs in game playing.
• The environment with more than one agent is termed as multi-agent
environment, in which each agent is an opponent of other agent and
playing against each other.
Game theory
• Searches in which two or more players with conflicting goals are
trying to explore the same search for the solution are called adversial
searches often called games
• Games are modelled as a search problem and heuristics evaluation
function, and these two are the main factors which help to modela and
solve games in AI
• Types of games in AI:
Deterministic Chance moves
Perfect Information Chess, checkers, Go Backgammon, monopoly
Imperfect Information Battleship, blind, tic-tac-toe Poker, Dice
• Perfect information: A game with the perfect information is that in which agents
can look into the complete board. Agents have all the information about the
game, and they can see each other moves also. Examples are Chess, Checkers,
Go, etc.
• Imperfect information: If in a game agents do not have all information about the
game and not aware with what's going on, such type of games are called the
game with imperfect information, such as tic-tac-toe, Battleship, blind, Bridge,
etc.
• Deterministic games: Deterministic games are those games which follow a strict
pattern and set of rules for the games, and there is no randomness associated
with them. Examples are chess, Checkers, Go, tic-tac-toe, etc.
• Non-deterministic games: Non-deterministic are those games which have
various unpredictable events and has a factor of chance or luck. This factor of
chance or luck is introduced by either dice or cards. These are random, and each
action response is not fixed. Such games are also called as stochastic games.
Example: Backgammon, Monopoly, Poker, etc.
• Zero-Sum Game: Zero-sum games are adversarial search which
involves pure competition. In Zero-sum game each agent's gain or loss
of utility is exactly balanced by the losses or gains of utility of another
agent.
• One player of the game try to maximize one single value, while other
player tries to minimize it.
• Each move by one player in the game is called as ply.
• Chess and tic-tac-toe are examples of a Zero-sum game.
Zero-sum game: Embedded thinking
• The Zero-sum game involved embedded thinking in which one agent
or player is trying to figure out:
• 1. What to do.
• 2. How to decide the move
• 3. Needs to think about his opponent as well
• 4. The opponent also thinks what to do
• Each of the players is trying to find out the response of his opponent to
their actions. This requires embedded thinking or backward reasoning
to solve the game problems in Al.
Formalization of the problem:
• A game can be defined as a type of search in Al which can be formalized of the
following elements:
• Initial state: It specifies how the game is set up at the start.
• Player(s): It specifies which player has moved in the state space.
• Action(s): It returns the set of legal moves in state space.
• Result(s, a): It is the transition model, which specifies the result of moves in the
state space.
• Terminal-Test(s): Terminal test is true if the game is over, else it is false at any
case. The state where the game ends is called terminal states.
• Utility(s, p): A utility function gives the final numeric value for a game that ends
in terminal states s for player p. It is also called payoff function. For Chess, the
outcomes are a win, loss, or draw and its payoff values are +1, 0, 1/2. And for tic-
tac-toe, utility values are +1, -1, and 0.
Game tree:
• A game tree is a tree where nodes of the tree are the game states and
Edges of the tree are the moves by players. Game tree involves initial
state, actions function, and result Function.
• Example: Tic-Tac-Toe game tree:
• The following figure is showing part of the game-tree for tic-tac-toe game.
• Following are some key points of the game:
• 1. There are two players MAX and MIN.
• 2. Players have an alternate turn and start with MAX.
• 3. MAX maximizes the result of the game tree
• 4. MIN minimizes the result.
Example Explanation:
• From the initial state, MAX has 9 possible moves as he starts first. MAX place x and
MIN place o, and both player plays alternatively until we reach a leaf node where one
player has three in a row or all squares are filled.
• Both players will compute each node, minimax, the minimax value which is the best
achievable utility against an optimal adversary.
• Suppose both the players are well aware of the tic-tac-toe and playing the best play.
Each player is doing his best to prevent another one from winning. MIN is acting
against Max in the game.
• So in the game tree, we have a layer of Max, a layer of MIN, and each layer is called
as Ply. Max place x, then MIN puts o to prevent Max from winning, and this game
continues until the terminal node.
• In this either MIN wins, MAX wins, or it's a draw. This game-tree is the whole search
space of possibilities that MIN and MAX are playing tic-tac-toe and taking turns
alternately
Adversarial Search for the minimax procedure works as follows:
• It aims to find the optimal strategy for MAX to win the game.
• It follows the approach of Depth-first search.
• In the game tree, optimal leaf node could appear at any depth of the
tree.
• Propagate the minimax values up to the tree until the terminal node
discovered.
In a given game tree, the optimal strategy can be determined from the minimax
value of each node, which can be written as MINIMAX(n). MAX prefer to
move to a state of maximum value and MIN prefer to move to a state of
minimum value then:
For a state S MINIMAX(s)
(UTILITY(s) IF TERMINAL-TEST(s)
max a€ Actions(s) MINIMAX(RESULT(s, a)) If PLAYER(s) = MAX
min a€ Actions(s) MINIMAX(RESULT(s, a)) If PLAYER(s) = MIN
Constraint Satisfaction Problem
• Constraint programming or constraint solving is about finding values
for variables such that they satisfy a constraint (conditions).
• CSP = { V,D,C}
• Variables : V={V1,…,Vn}
• Domain: D={D1,D2,….,Dn}
• Constraints: C={C1,…Ck}
• Examples:
• Map Coloring Problem
• Crypt-Arithmetic Problem
• Crossword puzzle
1) Assignment: Assigning a value to a single variable
2) Consistent : assignment that does not violate any constraint
3) Complete Assignment : All variables are assigned.
Map Coloring
• Two adjacent regions cannot have the same color no matter whatever
color we choose
• The goal is to assign colors to each region so that no neighboring
regions have the same color
Map Coloring –Example
• Color the following map using red, green and blue such that adjacent
regions have different colors
• Variables : {WA, NT, Q, NSW, V, SA, T}
• Domains : {red, green , blue}
• Constraints: adjacent regions must have different colors
• Eg WA ≠ NT
Variations on CSP Formulation
Domains
• Discrete : Fixed Values Eg: In coloring problem three colors
• Continuous : Eg: Weather sensor
• Finite: {red, green,blue}
• Infinite:
• A discrete domain can be infinite, such as the set of integers or strings. (If we
didn’t put a deadline on the job-scheduling problem, there would be an
infinite number of start times for each variable.)
Types of constraints:
Unary: single variable
Binary variable :two variables
Ternary : Three variables
Global : More than three variables
Preference: According to preference allocating. This is called constraint
optimization problem.
Inference in CSP
• Node consistency
• Arc Consistency
• Path consistency
• K-Consistency
• Global Consistency
• A single variable (corresponding to a node in the CSP network) is
node-consistent if all the values in the variable’s domain satisfy the
variable’s unary constraints
• For example, in the variant of the Australia map-coloring problem
where South Australians dislike green, the variable SA starts with
domain {red, green, blue},and we can make it node consistent by
eliminating green, leaving SA with the reduced domain {red, blue}.
• It is always possible to eliminate all the unary constraints in a CSP
by running node consistency
• Arc Consistency
• A variable in a CSP is arc-consistent if every value in its domain satisfies the
variable’s binary constraints. More formally, Xi is arc-consistent with respect
to another variable Xj if for every value in the current domain Di there is some
value in the domain Dj that satisfies the binary constraint on the arc (Xi,Xj).
Path Consistency
K- Consistency
• A CSP is K-consistent for any set of k − 1 variables and for any
consistent assignment to those variables, a consistent value can always
be assigned to any kth variable.
• 1 –consistency (node consistency )
• 2- arc consistency
• 3- path consistency
• Now suppose we have a CSP with n nodes and make it strongly
n-consistent (i.e., strongly k-consistent for k = n). We can then solve
the problem as follows: First, we choose a consistent value for X1. We
are then guaranteed to be able to choose a value for X2 because the
graph is 2-consistent, for X3 because it is 3-consistent, and so on
Constraint Graph
• Constraint Graph : nodes are variables, arcs are constraints
Crypt-Arithmetic Problem
• Crypt-Arithmetic problem is a type of encryption problem in which
the written message in an alphabetical form which is easily readable
and understandable is converted into a numeric form which is neither
easily readable nor understandable
Readable plaintext Non
Readable cipher text
Constraints
• Every character must have a unique value
• Digits should be from 0 to 9 only
• Starting character of number can not be zero
• Crypt arithmetic problem will have only one solution
• Addition of number with itself is always even
• In case of addition of two numbers, it there is carry to next step
then, the carry can only be 1